Skip to content

Standalone migration omits top level component import when pruning NgModules #63945

Closed

Description

@mattlewis92

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

core

Is this a regression?

No

Description

When the migration prunes NgModules, the imports are removed e.g.

This component:

import { Component } from '@angular/core';
import { OtherModule } from './other';

@Component({
    selector: 'my-comp',
    template: '<other-modules-component />',
    imports: [OtherModule]
})
export class MyComponent {}

Is incorrectly transformed to this: (import { OtherComponent } from './other'; is missing)

import { Component } from '@angular/core';

@Component({
    selector: 'my-comp',
    template: '<other-modules-component />',
    imports: [OtherComponent]
})
export class MyComponent {}

Here's a minimal test case that fails in packages/core/schematics/test/standalone_migration_spec.ts:

it('should convert NgModule imports to component imports', async () => {
  writeFile(
    'comp.ts',
    `
    import { Component } from '@angular/core';
    import { OtherModule } from './other';

    @Component({
        selector: 'my-comp',
        template: '<other />',
        imports: [OtherModule]
    })
    export class MyComponent {}
  `,
  );

  writeFile(
    'other.ts',
    `
    import { Component, NgModule } from '@angular/core';

    @Component({
        selector: 'other',
        template: 'other'
    })
    export class OtherComponent {}

    @NgModule({
        imports: [OtherComponent],
        exports: [OtherComponent]
    })
    export class OtherModule {}
    `,
  );

  await runMigration('prune-ng-modules');

  expect(stripWhitespace(tree.readContent('comp.ts'))).toBe(
    stripWhitespace(`
      import { Component } from '@angular/core';
      import { OtherComponent } from './other';

      @Component({
          selector: 'my-comp',
          template: '<other />',
          imports: [OtherComponent]
      })
      export class MyComponent {}
  `),
  );
});

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw


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

Angular CLI: 20.3.2
Node: 22.14.0
Package Manager: npm 10.9.2
OS: darwin arm64
    

Angular: 20.3.1
... common, compiler, compiler-cli, core, forms
... platform-browser, router

Package                      Version
------------------------------------
@angular-devkit/architect    0.2003.2
@angular-devkit/core         20.3.2
@angular-devkit/schematics   20.3.2
@angular/build               20.3.2
@angular/cli                 20.3.2
@schematics/angular          20.3.2
rxjs                         7.8.2
typescript                   5.9.2
zone.js                      0.15.1

Anything else?

No response

Activity

  1. added
    area: migrations
    on Sep 19, 2025
  2. added this to the needsTriage milestone on Sep 19, 2025
  3. added
    open for contributions
    on Sep 19, 2025
  4. mattlewis92 commented on Sep 19, 2025

    @mattlewis92
    ContributorAuthor

    Another similar issue exists where unused NgModule imports are preserved in the migrated component imports:

    it('should remove unused NgModule imports', async () => {
        writeFile(
          'comp.ts',
          `
          import { Component } from '@angular/core';
          import { OtherModule } from './other';
    
          @Component({
              selector: 'my-comp',
              template: 'test',
              imports: [OtherModule]
          })
          export class MyComponent {}
        `,
        );
    
        writeFile(
          'other.ts',
          `
          import { Component, NgModule } from '@angular/core';
    
          @Component({
              selector: 'other',
              template: 'other'
          })
          export class OtherComponent {}
    
          @NgModule({
              imports: [OtherComponent],
              exports: [OtherComponent]
          })
          export class OtherModule {}
          `,
        );
    
        await runMigration('prune-ng-modules');
    
        expect(stripWhitespace(tree.readContent('comp.ts'))).toBe(
          stripWhitespace(`
            import { Component } from '@angular/core';
    
            @Component({
                selector: 'my-comp',
                template: 'test',
                imports: []
            })
            export class MyComponent {}
        `),
        );
      });
    
  5. aparzi commented on Oct 9, 2025

    @aparzi
    Contributor

    @JeanMeche Could this issue be closed? Given that the related PR has been merged?

  6. JeanMeche commented on Oct 10, 2025

    @JeanMeche
    Member

    @mattlewis92, you were mentionning in #64186, that you think you need a more accurate repro for your issue.

    Can you provide one or should we close this issue ?

  7. mattlewis92 commented on Oct 10, 2025

    @mattlewis92
    ContributorAuthor

    Unfortunately, I wasn't able to find an easy reproduction, but we've managed to work around it by just converting all components to standalone first before pruning ng modules.

  8. angular-automatic-lock-bot commented on Nov 10, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  9. locked and limited conversation to collaborators on Nov 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: migrationsopen for contributions

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions