Skip to content

@if block inside the regular HTML element with i18n attribute breaks compilation #52767

Closed

Description

@nyannyacha

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

core

Is this a regression?

Yes

Description

Consider the template below.

app.component.html

<!-- OK -->
<span i18n>
  meow
  <span *ngIf="true; else elseBlock">meow?</span>
  <ng-template #elseBlock>
    <span>meow!</span>
  </ng-template>
</span>

<!-- Not OK -->
<span i18n>
  meow
  @if(true) {
    <span>meow?</span>
  } @else {
    <span>meow!</span>
  }
</span>

When the @if block is inside a regular HTML element having an i18n attribute, it will abort compilation with the following error message:

✘ [ERROR] Angular compilation emit failed. [plugin angular-compiler]

  TypeError: Cannot read properties of undefined (reading 'startName')
      at file:///workspaces/barebones-nodejs/ng17/node_modules/@angular/compiler/fesm2022/compiler.mjs:26452:37
      at Array.forEach (<anonymous>)
      at I18nContext.reconcileChildContext (file:///workspaces/barebones-nodejs/ng17/node_modules/@angular/compiler/fesm2022/compiler.mjs:26451:28)
      at TemplateDefinitionBuilder.i18nEnd (file:///workspaces/barebones-nodejs/ng17/node_modules/@angular/compiler/fesm2022/compiler.mjs:26948:30)
      at TemplateDefinitionBuilder.buildTemplateFunction (file:///workspaces/barebones-nodejs/ng17/node_modules/@angular/compiler/fesm2022/compiler.mjs:26748:18)
      at file:///workspaces/barebones-nodejs/ng17/node_modules/@angular/compiler/fesm2022/compiler.mjs:27275:50
      at file:///workspaces/barebones-nodejs/ng17/node_modules/@angular/compiler/fesm2022/compiler.mjs:26729:60
      at Array.forEach (<anonymous>)
      at TemplateDefinitionBuilder.buildTemplateFunction (file:///workspaces/barebones-nodejs/ng17/node_modules/@angular/compiler/fesm2022/compiler.mjs:26729:33)
      at compileComponentFromMetadata (file:///workspaces/barebones-nodejs/ng17/node_modules/@angular/compiler/fesm2022/compiler.mjs:28917:60)

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.0.0
Node: 18.18.2
Package Manager: npm 9.8.1
OS: linux x64

Angular: 17.0.2
... animations, common, compiler, compiler-cli, core, forms
... localize, platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1700.0
@angular-devkit/build-angular   17.0.0
@angular-devkit/core            17.0.0
@angular-devkit/schematics      17.0.0
@angular/cli                    17.0.0
@schematics/angular             17.0.0
rxjs                            7.8.1
typescript                      5.2.2
zone.js                         0.14.2

Anything else?

No response

Activity

  1. added
    area: compiler
    core: control flow
    on Nov 10, 2023
  2. modified the milestone: needsTriage on Nov 10, 2023
  3. nyannyacha commented on Nov 10, 2023

    @nyannyacha
    Author

    Looking at TemplateDefinitionBuilder, It is looking good until drilled down to the visitIfBlock.

    But here

    const templateIndex = this.createEmbeddedTemplateFn(
    tagName, children, '_Conditional', sourceSpan, variables, attrsExprs);

    const visitor = new TemplateDefinitionBuilder(
    this.constantPool, this._bindingScope, this.level + 1, contextName, this.i18n, index, name,
    this._namespace, this.fileBasedI18nSuffix, this.i18nUseExternalIds, this.deferBlocks,
    this.elementLocations, this._constants);
    // Nested templates must not be visited until after their parent templates have completed
    // processing, so they are queued here until after the initial pass. Otherwise, we wouldn't
    // be able to support bindings in nested templates to local refs that occur after the
    // template definition. e.g. <div *ngIf="showing">{{ foo }}</div> <div #foo></div>
    this._nestedTemplateFns.push(() => {
    const templateFunctionExpr = visitor.buildTemplateFunction(
    children, variables, this._ngContentReservedSlots.length + this._ngContentSelectorsOffset,
    i18n);

    and again here

    if (initI18nContext) {
    this.i18nStart(null, i18n!, selfClosingI18nInstruction);

    If my understanding is correct, visitIfBlock eventually makes a child builder, and the parent builder had its i18n context, but it invoked the createEmbeddedTemplateFn method without any of the i18n meta variables.

    I think this could be a problem 🧐
    What do you think about it? 😋

  4. pkozlowski-opensource commented on Nov 10, 2023

    @pkozlowski-opensource
    Member
  5. self-assigned this
    on Nov 10, 2023
  6. zip-fa commented on Nov 12, 2023

    @zip-fa

    #52540

    this is duplicate of my issue

  7. added a commit that references this issue on Nov 20, 2023
    291deac
  8. angular-automatic-lock-bot commented on Dec 21, 2023

    @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 Dec 21, 2023
  10. added a commit that references this issue on Jan 23, 2024
    22d8da4
  11. added 2 commits that reference this issue on Jan 26, 2024
    406049b
    1a86095
  12. added a commit that references this issue on Jan 26, 2024
    9485b93
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: compilercore: control flowstate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions