Skip to content

Commit c4028e8

Browse files
alan-agius4clydin
authored andcommitted
fix(@schematics/angular): reject ng-packagr builders in entry-point schematic
Validate that a library project is not configured with either `@angular/build:ng-packagr` or `@angular-devkit/build-angular:ng-packagr` instead of strictly requiring `@angular/build:library`. This prevents running the `entry-point` schematic on legacy `ng-packagr` projects while allowing custom library builders or workspaces without a `build` target.
1 parent 3123cf2 commit c4028e8

2 files changed

Lines changed: 41 additions & 11 deletions

File tree

‎packages/schematics/angular/entry-point/index.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,13 @@ export default createProjectSchematic<EntryPointOptions>((options, { project, tr
4545
throw new SchematicsException(`Project "${options.project}" is not a library project.`);
4646
}
4747

48-
const buildTarget = project.targets.get('build');
49-
if (buildTarget?.builder !== Builders.BuildLibrary) {
50-
throw new SchematicsException(
51-
`Project "${options.project}" does not use the "${Builders.BuildLibrary}" builder.`,
52-
);
48+
for (const target of project.targets.values()) {
49+
if (target.builder === Builders.NgPackagr || target.builder === Builders.BuildNgPackagr) {
50+
throw new SchematicsException(
51+
`Project "${options.project}" is using the "${target.builder}" builder, which is not supported by the entry-point schematic. ` +
52+
`Please migrate to "${Builders.BuildLibrary}" before generating entry points.`,
53+
);
54+
}
5355
}
5456

5557
if (options.name === 'package.json') {
@@ -118,7 +120,7 @@ export default createProjectSchematic<EntryPointOptions>((options, { project, tr
118120
: options.project
119121
: basename(options.name);
120122

121-
const buildOptions = buildTarget.options;
123+
const buildOptions = project.targets.get('build')?.options;
122124
const distRoot =
123125
typeof buildOptions?.['outputPath'] === 'string'
124126
? buildOptions['outputPath']

‎packages/schematics/angular/entry-point/index_spec.ts‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -399,14 +399,42 @@ describe('EntryPoint Schematic', () => {
399399
).toBeRejectedWithError('Project "my-app" is not a library project.');
400400
});
401401

402-
it('should throw if the library project does not use @angular/build:library', async () => {
402+
for (const builder of [
403+
'@angular/build:ng-packagr',
404+
'@angular-devkit/build-angular:ng-packagr',
405+
]) {
406+
it(`should throw if the library project uses ${builder}`, async () => {
407+
const angularJson = getJsonFileContent(workspaceTree, '/angular.json');
408+
angularJson.projects.foo.architect.build.builder = builder;
409+
workspaceTree.overwrite('/angular.json', JSON.stringify(angularJson, null, 2));
410+
411+
await expectAsync(
412+
schematicRunner.runSchematic('entry-point', defaultOptions, workspaceTree),
413+
).toBeRejectedWithError(
414+
`Project "foo" is using the "${builder}" builder, which is not supported by the entry-point schematic. ` +
415+
'Please migrate to "@angular/build:library" before generating entry points.',
416+
);
417+
});
418+
}
419+
420+
it('should allow library projects using a custom builder', async () => {
403421
const angularJson = getJsonFileContent(workspaceTree, '/angular.json');
404-
angularJson.projects.foo.architect.build.builder = '@angular/build:ng-packagr';
422+
angularJson.projects.foo.architect.build.builder = '@custom/builders:library';
405423
workspaceTree.overwrite('/angular.json', JSON.stringify(angularJson, null, 2));
406424

407-
await expectAsync(
408-
schematicRunner.runSchematic('entry-point', defaultOptions, workspaceTree),
409-
).toBeRejectedWithError('Project "foo" does not use the "@angular/build:library" builder.');
425+
const tree = await schematicRunner.runSchematic('entry-point', defaultOptions, workspaceTree);
426+
expect(tree.files).toContain('/projects/foo/secondary/src/public-api.ts');
427+
});
428+
429+
it('should allow library projects with no build target', async () => {
430+
const angularJson = getJsonFileContent(workspaceTree, '/angular.json');
431+
delete angularJson.projects.foo.architect.build;
432+
workspaceTree.overwrite('/angular.json', JSON.stringify(angularJson, null, 2));
433+
434+
const tree = await schematicRunner.runSchematic('entry-point', defaultOptions, workspaceTree);
435+
expect(tree.files).toContain('/projects/foo/secondary/src/public-api.ts');
436+
const tsConfigJson = getJsonFileContent(tree, '/tsconfig.json');
437+
expect(tsConfigJson.compilerOptions.paths['foo/*']).toEqual(['./dist/foo/*']);
410438
});
411439

412440
it('should throw if the library package.json is missing', async () => {

0 commit comments

Comments
 (0)