Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/schematics/angular/library/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ function addLibToWorkspaceFile(
workspace.projects.add({
name: projectName,
root: projectRoot,
sourceRoot: `${projectRoot}/src`,
// TODO(alanagius): consider re-adding this when we move away from ng-packagr.
// sourceRoot: `${projectRoot}/src`,
sourceRoot: projectRoot,
Comment on lines +101 to +103

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Setting sourceRoot to projectRoot introduces a directory structure inconsistency when generating subsequent schematic elements (like components, services, etc.) within the library.

The Issue

  1. The library's initial files and entry point are generated under ${projectRoot}/src/lib/.
  2. However, buildDefaultPath (in packages/schematics/angular/utility/workspace.ts) resolves the default path using project.sourceRoot:
    const root = project.sourceRoot ? `/${project.sourceRoot}/` : `/${project.root}/src/`;
    Since sourceRoot is now projectRoot, buildDefaultPath will resolve to ${projectRoot}/lib/ instead of ${projectRoot}/src/lib/.
  3. This causes subsequent components to be generated in ${projectRoot}/lib/ (as seen in the updated test assertion in index_spec.ts line 331), which is inconsistent with the initial library structure and will not be resolved/compiled correctly by default.

Suggested Fix

To support setting sourceRoot to projectRoot for test discovery while maintaining consistent generation paths, buildDefaultPath in packages/schematics/angular/utility/workspace.ts should be updated to handle libraries where sourceRoot is equal to root:

export function buildDefaultPath(project: workspaces.ProjectDefinition): string {
  const projectType = project.extensions['projectType'];
  let root = project.sourceRoot ? `/${project.sourceRoot}/` : `/${project.root}/src/`;
  if (projectType === ProjectType.Library && project.sourceRoot === project.root) {
    root = `/${project.root}/src/`;
  }
  const projectDirName = projectType === ProjectType.Application ? 'app' : 'lib';

  return `${root}${projectDirName}`;
}

projectType: ProjectType.Library,
prefix: options.prefix,
targets: {
Expand Down
2 changes: 1 addition & 1 deletion packages/schematics/angular/library/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ describe('Library Schematic', () => {
project: 'foo',
};
tree = await schematicRunner.runSchematic('component', componentOptions, tree);
expect(tree.exists('/projects/foo/src/lib/comp/comp.ts')).toBe(true);
expect(tree.exists('/projects/foo/lib/comp/comp.ts')).toBe(true);
});

it(`should support creating scoped libraries`, async () => {
Expand Down
Loading