Skip to content

Control flow schematic: @for (document of <collection>; track document) results in template not compiling #53293

Closed

Description

@hamfastgamgee

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

compiler-cli, core

Is this a regression?

No

Description

We had a component processed by the migration that resulted in our iteration variable and our track variable both being named "document", but the template parser choked on this and said that all the HTML inside the @for did not have a "createElement" method. It seems clear that the parser got confused with the global "document" object.

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)

No response

Anything else?

No response

Activity

  1. AndrewKushnir commented on Nov 30, 2023

    @AndrewKushnir
    Contributor

    @hamfastgamgee could you please provide an example of a part of a template that is not migrated correctly and an error message that you see? Please also make sure you are using the most recent version of Angular packages (17.0.5), since there were a number of improvements merged recently. Thank you.

  2. added
    needs reproduction
    area: core
    area: migrations
    core: control flow
    on Nov 30, 2023
  3. added this to the Backlog milestone on Nov 30, 2023
  4. changed the title [-]Control flow - @for (document of <collection>; track document) results in template not compiling[/-] [+]Control flow schematic: @for (document of <collection>; track document) results in template not compiling[/+] on Nov 30, 2023
  5. hamfastgamgee commented on Dec 1, 2023

    @hamfastgamgee
    Author

    Honestly, I just renamed "document" to "historicalDocument" in my case and moved on to the next dozen files that needed TLC. The migration did exactly like I expected, changing *ngFor="let document of history" to @for (document of history; track document), but it seemed like the template parser choked on the variable being named "document" in both my IDE and a build, complaining that the div elements inside the @for lacked a .create Element() method or something to that effect. From the behavior, I suspect that my local "document" variable was scoped such that the global document object needed for DOM construction was being overridden. I imagine it would be replicable in any template where the variable inside @for is named "document". (I have no idea if any other global properties would be similarly overridden.)

  6. crisbeto commented on Dec 1, 2023

    @crisbeto
    Member

    I'm guessing the document property is assigned to the actual document, because this test passes for me both in our testing environment and in Material's development app:

    fit('', () => {
      @Component({
        template: '@for (document of document; track document) {{{document}}|}',
      })
      class TestComponent {
        document = [1, 2, 3];
      }
    
      const fixture = TestBed.createComponent(TestComponent);
      fixture.detectChanges();
      expect(fixture.nativeElement.textContent).toBe('1|2|3|');
    
      fixture.componentInstance.document.pop();
      fixture.detectChanges();
      expect(fixture.nativeElement.textContent).toBe('1|2|');
    
      fixture.componentInstance.document.push(3);
      fixture.detectChanges();
      expect(fixture.nativeElement.textContent).toBe('1|2|3|');
    
      fixture.componentInstance.document[0] = 3;
      fixture.componentInstance.document[2] = 1;
      fixture.detectChanges();
      expect(fixture.nativeElement.textContent).toBe('3|2|1|');
    });
  7. hamfastgamgee commented on Dec 1, 2023

    @hamfastgamgee
    Author

    Does it do the same if the contents are HTML elements? My case had at least one div element and maybe a Material component in there. The only thing that was named "document" was the variable used for each item in the collection, not the collection itself, FWIW. Would love to find out this was just a "me" problem, especially since my workaround of renaming the variable was foolproof. And to answer an earlier question I missed: I was definitely running on 7.0.5 when this happened.

  8. crisbeto commented on Dec 1, 2023

    @crisbeto
    Member

    How did you get a hold of the elements? Was it an array you constructed or through something like querySelectorAll?

  9. hamfastgamgee commented on Dec 1, 2023

    @hamfastgamgee
    Author

    Looking back at the component in question, it was an array of strongly-typed objects brought in via @input. However, I can reproduce it without that, at least locally on 7.0.5.

    This is the most minimal template I have tried (trying to rip out everything specific to our codebase):

    import { Component } from '@angular/core';
    
    @Component({
        selector: 'upload-history',
        standalone: true,
        templateUrl: './upload-history.component.html',
    })
    export class UploadHistoryComponent {
        history: { fileSize: number }[];
    }
    @for (document of history; track document) {
        <div>
        </div>
    }

    In VSCode for me, this lights up the inner div element as an error with the following message:

    Property 'createElement' does not exist on type '{ fileSize: number; }'.ngtsc(2339)
    upload-history.component.ts(4, 4): Error occurs in the template of component UploadHistoryComponent.

  10. JoostK commented on Dec 1, 2023

    @JoostK
    Member

    We do use document.createElement for DOM-based type-checking (e.g. for native event listeners), it appears that template variables may be emitted as-is in the TCB such that the ambient document global becomes aliased. This would be a bug in the TCB generation, not the control flow migration.

  11. crisbeto commented on Dec 1, 2023

    @crisbeto
    Member

    @JoostK we do emit the variables as is, but I would expect the local variables to take precedence over the global document. For reference, we emit something like this:

    for (const document of this.document) {
      var _t1 = document;
      // do things with _t1...
    }
  12. crisbeto commented on Dec 1, 2023

    @crisbeto
    Member

    Ah, I see now. If we have other document.createElement calls further down inside the loop, they'll refer to the local variable as well.

  13. 1 remaining item

  14. added
    P3
    and removed
    needs reproduction
    on Dec 1, 2023
  15. self-assigned this
    on Dec 2, 2023
  16. added a commit that references this issue on Dec 2, 2023
    1b62464
  17. alxhub commented on Dec 4, 2023

    @alxhub
    Member

    We do use document.createElement for DOM-based type-checking (e.g. for native event listeners), it appears that template variables may be emitted as-is in the TCB such that the ambient document global becomes aliased. This would be a bug in the TCB generation, not the control flow migration.

    Agreed - we should never use the user's variable name in the TCB because of the risk of collisions (imagine variables named _t1 for example).

  18. added a commit that references this issue on Dec 5, 2023
    6ec7a42
  19. angular-automatic-lock-bot commented on Jan 5, 2024

    @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 Jan 5, 2024
  21. added a commit that references this issue on Jan 23, 2024
    b65539e
  22. added a commit that references this issue on Jan 26, 2024
    0b81c96
  23. added a commit that references this issue on Jan 26, 2024
    d46cf80
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3area: corearea: migrationscompiler: template type-checkingcore: control flowstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions