Skip to content

A silent error is thrown in AddCommandModule.builder, when passing a version to the command ng add @my/lib@<version> #27766

Closed
@Platonn

Description

@Platonn

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

  1. Create a fresh angular 17 app

  2. In VSCode add a console.error({error}) in the AngularCLI's file node_modules/@angular/cli/src/commands/add/cli.js in line 80 (inside this catch block):

    } catch (error) {
    // During `ng add` prior to the downloading of the package
    // we are not able to resolve and create a collection.
    // Or when the collection value is a path to a tarball.
    }

    see example:
    image

  3. (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)

  4. run ng add @my/library@<with-version-here>

  5. 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions