Skip to content

new library public-api.ts file not using component type declared on angular.json's schematics #26981

Closed as not planned

Description

@johalternate

Which @angular/* package(s) are the source of the bug?

Don't known / other

Is this a regression?

No

Description

When I create a new library the default component inside of the lib folder takes its type from the component schematic declared on angular.json. If for example my angular.json looks like this:

{
    //...
    "newProjectRoot": "projects",
    //...
    "schematics": {
        "@schematics/angular:application": {
          "style": "scss",
          "inlineTemplate": true,
          "inlineStyle": true,
          "ssr": false
        },
        "@schematics/angular:component": {
          "changeDetection": "OnPush",
          "type": "Cmp",
          "inlineStyle": true,
          "inlineTemplate": true,
          "style": "scss"
        }
    }
}

After creating a library using ng generate library foo-library, the new library includes an empty dummy FooLibraryCmp inside the projects/foo-library/src/lib/foo-library.cmp.ts. However, the contents of projects/foo-library/src/public-api.ts look like this:

/*
 * Public API Surface of foo-library
 */

export * from './lib/foo-library.service';
export * from './lib/foo-library.component'; // uses .component instead of .cmp

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 17.1.1
Node: 18.18.0
Package Manager: npm 9.6.6
OS: win32 x64

Angular: 17.1.1
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, platform-server
... router, ssr

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1701.1
@angular-devkit/build-angular   17.1.1
@angular-devkit/core            17.1.1
@angular-devkit/schematics      17.1.1
@schematics/angular             17.1.1
ng-packagr                      17.1.2
rxjs                            7.8.1
typescript                      5.2.2
zone.js                         0.14.3

Anything else?

No response

Activity

  1. transferred this issue fromangular/angularon Jan 28, 2024
  2. johalternate commented on Jan 25, 2025

    @johalternate
    Author

    I would love to take a shot at this issue for my first contribution to angular, in fact, I already have a somewhat working solution on my fork (johalternate@ee48a80).

    But Im unable to write a test for it because I cant find a way of providing default schematics config to a workspace tree created using SchematicTestRunner.runSchematic('workspace', <some-options>).

    Any idea about how can I test my solution?

  3. MeAkib commented on Oct 18, 2026

    @MeAkib
    Contributor

    @alan-agius4 This looks fixed on main by 661609e: the library schematic now passes type: '' to the component schematic, so a workspace-level type no longer leaks into the generated file or public-api.ts.

    Can this be closed?

  4. ChristopherPHolder commented on Oct 26, 2026

    @ChristopherPHolder

    Agree with @MeAkib, this bug was fixed in 661609e

    It did not add a test case, and for this i think an e2e case would be necessary, not sure it merits it. Do something like this deserve an e2e test case?

    Quick reproduction

    The bug can be reproduced by running:

    npx -y @angular/cli@19.2.19 new ws --skip-install --skip-git --defaults
    cd ws
    npx -y @angular/cli@19.2.19 config schematics.@schematics/angular:component.type Cmp
    npx -y @angular/cli@19.2.19 generate library foo-library --skip-install
    cat projects/foo-library/src/public-api.ts && ls projects/foo-library/src/lib
    

    In the output you can see the component was generated as foo-library.cmp.ts, but public-api.ts still exports ./lib/foo-library.component:

    export * from './lib/foo-library.service';
    export * from './lib/foo-library.component';
    
    foo-library.cmp.spec.ts
    foo-library.cmp.ts
    foo-library.service.spec.ts
    foo-library.service.ts
    

    ng build foo-library then fails with:

    error TS2307: Cannot find module './lib/foo-library.component' or its corresponding type declarations.
    

    Quick validation

    When we run the same with v20 or latest it’s not present:

    npx -y @angular/cli@latest new ws --skip-install --skip-git --defaults
    cd ws
    npx -y @angular/cli@latest config schematics.@schematics/angular:component.type Cmp
    npx -y @angular/cli@latest generate library foo-library --skip-install
    cat projects/foo-library/src/public-api.ts && ls projects/foo-library/src/lib
    

    the output type is what we expect:

    export * from './lib/foo-library';
    
    foo-library.spec.ts
    foo-library.ts
    
  5. alan-agius4 commented on Oct 27, 2026

    @alan-agius4
    Collaborator

    Thanks for taking a look at this, @MeAkib and @ChristopherPHolder! An E2E test feels a bit like overkill here, especially given how time-consuming they can be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions