Description
Command
add
Is this a regression?
- Yes, this behavior used to work in the previous version
The previous version in which this bug was not present was
No response
Description
Error
An exception is thrown during the method createCollection(), when the collectionName
contains a @<version>
, e.g. ng add @my/lib@1.2.3
.
Negative consequences
The consequence is that the custom schematics' schema is not applied for parsing, i.e. the method addSchemaOptionsToCommand() is skipped, becasue of the thrown exception
And btw. the later this exception is ignored here.
Why it happens (internal Angular CLI code)
The error happens becasue eventually this line throws an error, becasue there is no such a directory like node_modules/@my/lib@1.2.3
, but only node_modules/@my/lib
Because an error is thrown, a custom schema.json is not applied for parsing the arguments then.
Idea of a fix:
Before passing the collectionName
as an argument to the createCollection()
method, we could first extract any versions from it, e.g. by passing it through npa from 'npm-package-arg'
, e.g.:
private async getCollectionName(): Promise<string> {
const [, collection] = this.context.args.positional;
const packageIdentifier = npa(collection);
return packageIdentifier.name;
}
Additional notes
- ❌
ng add @my/lib@1.2.3
- causes error - ❌
ng add @my/lib@
(with just@
) - also causes error - ✅
ng add @my/lib
(nothing after the package's name) - doesn't cause error
Minimal Reproduction
-
Create a fresh angular 17 app
-
In VSCode add a
console.error({error})
in the AngularCLI's filenode_modules/@angular/cli/src/commands/add/cli.js
in line 80 (inside thiscatch
block):
angular-cli/packages/angular/cli/src/commands/add/cli.ts
Lines 102 to 106 in ea843eb
see example:
-
(run
npm install @my/lib
- to mitigate the bug Schematic with multi select x-promt errors when using command line parameter: 'path ".foo" should be array' #16320 - but ideally this step shouldn't be needed when this other bug is fixed) -
run
ng add @my/library@<with-version-here>
-
See an error in the console
error: CollectionCannotBeResolvedException [Error]: Collection "@my/lib@1.2.3" cannot be resolved.
Your Environment
Angular CLI: 17.3.8
Node: 20.11.0
Package Manager: npm 10.2.4
OS: darwin arm64
Angular: 17.3.10
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
... service-worker
Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1703.8
@angular-devkit/build-angular 17.3.8
@angular-devkit/core 17.3.8
@angular-devkit/schematics 17.3.8
@angular/cli 17.3.8
@schematics/angular 17.3.8
rxjs 7.8.1
typescript 5.2.2
zone.js 0.14.6
Anything else relevant?
No response