fix(@schematics/angular): configure library sourceRoot to project root - #34205
alan-agius4 wants to merge 1 commit into
Conversation
This ensures that test discovery can properly discover test files across secondary entry points without needing path traversal.
There was a problem hiding this comment.
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.
| // TODO(alanagius): consider re-adding this when we move away from ng-packagr. | ||
| // sourceRoot: `${projectRoot}/src`, | ||
| sourceRoot: projectRoot, |
There was a problem hiding this comment.
Setting sourceRoot to projectRoot introduces a directory structure inconsistency when generating subsequent schematic elements (like components, services, etc.) within the library.
The Issue
- The library's initial files and entry point are generated under
${projectRoot}/src/lib/. - However,
buildDefaultPath(inpackages/schematics/angular/utility/workspace.ts) resolves the default path usingproject.sourceRoot:Sinceconst root = project.sourceRoot ? `/${project.sourceRoot}/` : `/${project.root}/src/`;
sourceRootis nowprojectRoot,buildDefaultPathwill resolve to${projectRoot}/lib/instead of${projectRoot}/src/lib/. - This causes subsequent components to be generated in
${projectRoot}/lib/(as seen in the updated test assertion inindex_spec.tsline 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}`;
}
Sets
sourceRootfor 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