Skip to content

fix(@schematics/angular): configure library sourceRoot to project root - #34205

Closed
alan-agius4 wants to merge 1 commit into
angular:mainfrom
alan-agius4:feat/lib-sourceroot
Closed

alan-agius4 wants to merge 1 commit into
angular:mainfrom
alan-agius4:feat/lib-sourceroot

Conversation

@alan-agius4

Copy link
Copy Markdown
Collaborator

Sets sourceRoot for generated libraries to the project root instead of ${projectRoot}/src.

This ensures that test discovery can properly discover test files across secondary entry points without needing parent directory traversal.

Fixes #33467

This ensures that test discovery can properly discover test files across secondary entry points without needing path traversal.
@alan-agius4
alan-agius4 deleted the feat/lib-sourceroot branch September 29, 2026 14:31

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request changes the sourceRoot of generated libraries from ${projectRoot}/src to projectRoot. However, this introduces a directory structure inconsistency where subsequent schematic elements (like components) are generated under ${projectRoot}/lib/ instead of ${projectRoot}/src/lib/. To resolve this, it is recommended to update buildDefaultPath to correctly handle libraries where sourceRoot is equal to projectRoot.

Comment on lines +101 to +103
// TODO(alanagius): consider re-adding this when we move away from ng-packagr.
// sourceRoot: `${projectRoot}/src`,
sourceRoot: projectRoot,

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}`;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

unit-test builder resolves include paths relative to sourceRoot instead of project root, breaking secondary entry points tests

1 participant