From 3aad86ea9da6c54bbf52bbf05701b9b902a36ea0 Mon Sep 17 00:00:00 2001 From: Shuaib Hasan Akib Date: Tue, 17 Nov 2026 00:21:51 +0600 Subject: [PATCH 1/2] fix(@schematics/angular): report invalid entry point names with a descriptive error The entry-point schematic accepts any name that is a valid npm export subpath, but the final path segment is also used to name the generated class, selector, and source files. A segment that cannot be used that way fails deep inside the component schematic with an error about something the user never provided: `ng generate entry-point 3d` fails with 'Selector "lib-3d" is invalid.' and `ng generate entry-point svg~icons` fails with a raw schema validation dump of the internal component options. Validate the final path segment up front and report the entry point name together with the allowed shape. Dots remain supported, as `classify` removes them when deriving the class name ('testing.v2' generates the class `TestingV2`); a test now covers that case to keep it working. --- .../schematics/angular/entry-point/index.ts | 14 ++++++++ .../angular/entry-point/index_spec.ts | 33 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/packages/schematics/angular/entry-point/index.ts b/packages/schematics/angular/entry-point/index.ts index 13976fc9f5fe..98f5a83410ac 100644 --- a/packages/schematics/angular/entry-point/index.ts +++ b/packages/schematics/angular/entry-point/index.ts @@ -58,6 +58,20 @@ export default createProjectSchematic((options, { project, tr throw new SchematicsException('Entry point name "package.json" is reserved.'); } + // The final segment names the generated class, selector, and files. Without this check, + // a name that is a valid export subpath but not usable as an identifier fails later with + // an error about a class or selector the user never provided. + if ( + options.name !== '.' && + !/^[a-zA-Z][a-zA-Z0-9_-]*(?:\.[a-zA-Z0-9_-]+)*$/.test(basename(options.name)) + ) { + throw new SchematicsException( + `Entry point name "${options.name}" is invalid. The final path segment is used to name ` + + 'the generated source files and must start with a letter and contain only ' + + 'alphanumeric characters, dots, dashes, and underscores.', + ); + } + const libDir = project.root; const packageJsonPath = join(libDir, 'package.json'); if (!tree.exists(packageJsonPath)) { diff --git a/packages/schematics/angular/entry-point/index_spec.ts b/packages/schematics/angular/entry-point/index_spec.ts index 834a770a532d..48735849df8a 100644 --- a/packages/schematics/angular/entry-point/index_spec.ts +++ b/packages/schematics/angular/entry-point/index_spec.ts @@ -473,6 +473,39 @@ describe('EntryPoint Schematic', () => { } }); + it('should throw a descriptive error when the final name segment cannot name source files', async () => { + for (const invalidName of ['3d', 'feature/3d', 'svg~icons']) { + await expectAsync( + schematicRunner.runSchematic( + 'entry-point', + { + name: invalidName, + project: 'foo', + }, + workspaceTree, + ), + ).toBeRejectedWithError( + `Entry point name "${invalidName}" is invalid. The final path segment is used to name ` + + 'the generated source files and must start with a letter and contain only ' + + 'alphanumeric characters, dots, dashes, and underscores.', + ); + } + }); + + it('should allow a final name segment containing dots', async () => { + const tree = await schematicRunner.runSchematic( + 'entry-point', + { + name: 'testing.v2', + project: 'foo', + }, + workspaceTree, + ); + + const packageJson = getJsonFileContent(tree, '/projects/foo/package.json'); + expect(packageJson.exports['./testing.v2']).toBeDefined(); + }); + it('should throw if the entry point already exists in package.json exports', async () => { const tree = await schematicRunner.runSchematic('entry-point', defaultOptions, workspaceTree); From c0d833eaf50019797d95836174b018cadcdcbc6a Mon Sep 17 00:00:00 2001 From: Shuaib Hasan Akib Date: Tue, 17 Nov 2026 21:58:27 +0600 Subject: [PATCH 2/2] fixup! fix(@schematics/angular): report invalid entry point names with a descriptive error --- .../schematics/angular/entry-point/index.ts | 45 ++++++---- .../angular/entry-point/index_spec.ts | 90 +++++++++++++++---- 2 files changed, 102 insertions(+), 33 deletions(-) diff --git a/packages/schematics/angular/entry-point/index.ts b/packages/schematics/angular/entry-point/index.ts index 98f5a83410ac..7f1eb022b89b 100644 --- a/packages/schematics/angular/entry-point/index.ts +++ b/packages/schematics/angular/entry-point/index.ts @@ -40,6 +40,28 @@ function updateTsConfig(packageName: string, ...paths: string[]): Rule { }; } +/** + * Creates a name usable for the generated class, selector, and file names from an + * entry-point name. Entry-point names are npm export subpaths and may contain characters + * (such as `~`) or digit-led words (such as `3d`) that cannot appear in a class name or + * in a selector segment. Returns null when the result would not start with a letter. + */ +function toArtifactName(name: string): string | null { + const artifactName = name + // `~` and `/` cannot appear in a class name, selector, or file name. + .replace(/[~/]/g, '-') + // A word that starts with digits but contains more than digits cannot start a class + // name or a selector segment (`lib-foo-3d` is an invalid selector); merging it into + // the preceding word (`foo3d`) keeps both valid. All-digit words stay separate, as + // trailing digit segments are valid in a selector (`lib-foo-2`). + .replace(/[-_]+(?=\d+[a-zA-Z.])/g, '') + // Separator runs and leading or trailing separators produce empty selector segments. + .replace(/[-_]{2,}/g, '-') + .replace(/^[-_.]+|[-_.]+$/g, ''); + + return /^[a-zA-Z]/.test(artifactName) ? artifactName : null; +} + export default createProjectSchematic((options, { project, tree }) => { if (project.extensions['projectType'] !== ProjectType.Library) { throw new SchematicsException(`Project "${options.project}" is not a library project.`); @@ -58,20 +80,6 @@ export default createProjectSchematic((options, { project, tr throw new SchematicsException('Entry point name "package.json" is reserved.'); } - // The final segment names the generated class, selector, and files. Without this check, - // a name that is a valid export subpath but not usable as an identifier fails later with - // an error about a class or selector the user never provided. - if ( - options.name !== '.' && - !/^[a-zA-Z][a-zA-Z0-9_-]*(?:\.[a-zA-Z0-9_-]+)*$/.test(basename(options.name)) - ) { - throw new SchematicsException( - `Entry point name "${options.name}" is invalid. The final path segment is used to name ` + - 'the generated source files and must start with a letter and contain only ' + - 'alphanumeric characters, dots, dashes, and underscores.', - ); - } - const libDir = project.root; const packageJsonPath = join(libDir, 'package.json'); if (!tree.exists(packageJsonPath)) { @@ -128,11 +136,16 @@ export default createProjectSchematic((options, { project, tr const prefix = (options.prefix ?? project.prefix) || 'lib'; const sourceDir = isPrimary ? join(entryPointDir, 'lib') : entryPointDir; - const artifactName = isPrimary + const baseName = isPrimary ? /^@.*\/.*/.test(options.project) ? options.project.split('/')[1] : options.project - : basename(options.name); + : options.name; + const artifactName = + toArtifactName(basename(baseName)) ?? + toArtifactName(baseName) ?? + toArtifactName(`${prefix}-${baseName}`) ?? + 'entry'; const buildOptions = project.targets.get('build')?.options; const distRoot = diff --git a/packages/schematics/angular/entry-point/index_spec.ts b/packages/schematics/angular/entry-point/index_spec.ts index 48735849df8a..be78a3c534fa 100644 --- a/packages/schematics/angular/entry-point/index_spec.ts +++ b/packages/schematics/angular/entry-point/index_spec.ts @@ -473,23 +473,79 @@ describe('EntryPoint Schematic', () => { } }); - it('should throw a descriptive error when the final name segment cannot name source files', async () => { - for (const invalidName of ['3d', 'feature/3d', 'svg~icons']) { - await expectAsync( - schematicRunner.runSchematic( - 'entry-point', - { - name: invalidName, - project: 'foo', - }, - workspaceTree, - ), - ).toBeRejectedWithError( - `Entry point name "${invalidName}" is invalid. The final path segment is used to name ` + - 'the generated source files and must start with a letter and contain only ' + - 'alphanumeric characters, dots, dashes, and underscores.', - ); - } + it('should create a usable class and selector when the name starts with digits', async () => { + const tree = await schematicRunner.runSchematic( + 'entry-point', + { + name: '3d', + project: 'foo', + }, + workspaceTree, + ); + + const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json'); + expect(pkgJson.exports['./3d']).toBe('./3d/src/public-api.ts'); + + const componentContent = tree.readContent('/projects/foo/3d/src/lib3d.ts'); + expect(componentContent).toContain('export class Lib3d'); + expect(componentContent).toContain("selector: 'lib-lib3d'"); + expect(tree.readContent('/projects/foo/3d/src/public-api.ts')).toContain( + "export * from './lib3d';", + ); + }); + + it('should name artifacts from the parent segments when the final segment starts with digits', async () => { + const tree = await schematicRunner.runSchematic( + 'entry-point', + { + name: 'feature/3d', + project: 'foo', + }, + workspaceTree, + ); + + const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json'); + expect(pkgJson.exports['./feature/3d']).toBe('./feature/3d/src/public-api.ts'); + + const componentContent = tree.readContent('/projects/foo/feature/3d/src/feature3d.ts'); + expect(componentContent).toContain('export class Feature3d'); + expect(componentContent).toContain("selector: 'lib-feature3d'"); + }); + + it('should merge a digit-led word into the preceding word', async () => { + const tree = await schematicRunner.runSchematic( + 'entry-point', + { + name: 'my-3d', + project: 'foo', + }, + workspaceTree, + ); + + const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json'); + expect(pkgJson.exports['./my-3d']).toBe('./my-3d/src/public-api.ts'); + + const componentContent = tree.readContent('/projects/foo/my-3d/src/my3d.ts'); + expect(componentContent).toContain('export class My3d'); + expect(componentContent).toContain("selector: 'lib-my3d'"); + }); + + it('should replace characters that cannot appear in a class name or selector', async () => { + const tree = await schematicRunner.runSchematic( + 'entry-point', + { + name: 'svg~icons', + project: 'foo', + }, + workspaceTree, + ); + + const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json'); + expect(pkgJson.exports['./svg~icons']).toBe('./svg~icons/src/public-api.ts'); + + const componentContent = tree.readContent('/projects/foo/svg~icons/src/svg-icons.ts'); + expect(componentContent).toContain('export class SvgIcons'); + expect(componentContent).toContain("selector: 'lib-svg-icons'"); }); it('should allow a final name segment containing dots', async () => {