Skip to content

Migration to Control Flow Syntax Removes CommonModule While Still Needed #59846

Closed

Description

@tomer953

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

core

Is this a regression?

Yes

Description

When running the ng generate @angular/core:control-flow schematic on a large project, the migration tool incorrectly removes CommonModule from the component's imports: [] array, even though directives or pipes like async, date, and ngTemplateOutlet are still in use. This leads to runtime errors where these directives and pipes are no longer recognized in the template, but not fails the build

Please provide a link to a minimal reproduction of the bug

#59846 (comment)

Please provide the exception or error you saw


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

Angular CLI: 18.2.11
Node: 20.17.0
Package Manager: npm 10.8.2
OS: darwin arm64

Angular: undefined
... 

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1802.11
@angular-devkit/build-angular   18.2.11
@angular-devkit/core            18.2.11
@angular-devkit/schematics      18.2.11
@angular/cli                    18.2.11
@angular/compiler               18.2.10
@angular/compiler-cli           18.2.10
@angular/language-service       18.2.10
@schematics/angular             18.2.11
typescript                      5.5.4

Anything else?

few notes:

  • this bug only related to standalone components (our project is mixed)
  • I suspect It might related to components were the template isn't changed (meaning already using the new control flow) - but has CommonModule in the imports array - maybe in this edge case, the migration removes the CommonModule without check if other directives are used

Activity

  1. added
    needs reproduction
    on Feb 4, 2025
  2. JeanMeche commented on Feb 4, 2025

    @JeanMeche
    Member

    Hi, we might still need a reproduction to understand how often this problem can arise.

  3. tomer953 commented on Feb 4, 2025

    @tomer953
    ContributorAuthor

    @JeanMeche please

    import { CommonModule } from '@angular/common';
    import { ChangeDetectionStrategy, Component, Input } from '@angular/core';
    import { BehaviorSubject } from 'rxjs';
    
    @Component({
      selector: 'app-count',
      standalone: true,
      imports: [CommonModule],
      template: `
      @if (count > 3) {
        <p>{{ msg | async }}</p>
      }`,
      styleUrl: './count.component.css',
      changeDetection: ChangeDetectionStrategy.OnPush,
    })
    export class CountComponent {
      @Input() count = 0;
      msg = new BehaviorSubject('ok');
    }

    migrate this, you'll see the error

    control-flow-reproduce.mp4
  4. added
    area: migrations
    on Feb 4, 2025
  5. added this to the Backlog milestone on Feb 4, 2025
  6. removed
    needs reproduction
    on Feb 4, 2025
  7. modified the milestones: Backlog, needsTriage on Feb 4, 2025
  8. JeanMeche commented on Feb 4, 2025

    @JeanMeche
    Member

    Okay so it happens when the only usage is inside another view, here the @if.

  9. added
    cross-cutting: standalone
    on Feb 4, 2025
  10. tomer953 commented on Feb 4, 2025

    @tomer953
    ContributorAuthor

    seems you're right...

    maybe related to here

    export class CommonCollector extends RecursiveVisitor {

  11. thePunderWoman commented on Feb 5, 2025

    @thePunderWoman
    Contributor

    Yeah, I suspect if you already have new control flow in, the visitor may skip over checking inside that portion of the template to see if common module symbols are being used.

  12. crisbeto commented on Feb 5, 2025

    @crisbeto
    Member

    I think this may have been fixed in a newer version. I just tried it against our unit tests and couldn't reproduce it.

  13. added a commit that references this issue on Feb 5, 2025
    13f64ec
  14. crisbeto commented on Feb 5, 2025

    @crisbeto
    Member

    Ah nevermind, my test wasn't set up correctly. Here's a fix for this issue and one more: #59861.

  15. self-assigned this
    on Feb 5, 2025
  16. tomer953 commented on Feb 5, 2025

    @tomer953
    ContributorAuthor

    Thanks for the quick fix!

    Any way to use the fixed code without update our angular version? (Even as a workaround to replace some file)

  17. added a commit that references this issue on Feb 12, 2025
    46f36a5
  18. added a commit that references this issue on Feb 12, 2025
    1119f85
  19. angular-automatic-lock-bot commented on Mar 15, 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.

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

Metadata

Metadata

Assignees

Labels

area: migrationscross-cutting: standalonestate: has PR

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions