Skip to content

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

@mattlewis92

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: migrationsIssues related to `ng update`/`ng generate` migrationshelp wantedAn issue that is suitable for a community contributor (based on its complexity/scope).

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions