Skip to content

When using name 't' for variable exception is throwing  #57168

Closed

Description

@olegdunkan

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

compiler, core

Is this a regression?

No

Description

import {
  Component,
  Inject,
  Injectable,
  InjectionToken,
  forwardRef,
} from '@angular/core';
import { CommonModule } from '@angular/common';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js'

export const t = new InjectionToken<number[]>('addresses'); //bug, if we change the name of variable to t1, for example, it works


@Component({
  standalone: true,
  selector: 'a-comp',
  template: `A`,
  providers: [
    { provide: t, useValue: [1], multi: true },
  ],
})
export class A {
  constructor(@Inject(t) ns: number[]) {
    console.log(ns);
  }
}


@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule, A],
  template: `<a-comp />`
})
export class AppComponent {}

bootstrapApplication(AppComponent);

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-6h67j1?file=src%2Fmain.ts

Please provide the exception or error you saw

Error: ASSERTION ERROR: token must be defined [Expected=> null != undefined <=Actual]
    at throwError2 (chunk-TNFBICRJ.js?v=aebd17e5:4511:9)
    at assertDefined (chunk-TNFBICRJ.js?v=aebd17e5:4507:5)
    at bloomHashBitOrFactory (chunk-TNFBICRJ.js?v=aebd17e5:7731:16)
    at lookupTokenUsingNodeInjector (chunk-TNFBICRJ.js?v=aebd17e5:7562:21)
    at getOrCreateInjectable (chunk-TNFBICRJ.js?v=aebd17e5:7554:19)
    at Module.ɵɵdirectiveInject (chunk-TNFBICRJ.js?v=aebd17e5:10585:17)
    at NodeInjectorFactory.A_Factory [as factory] (main.ts:22:15)
    at getNodeInjectable (chunk-TNFBICRJ.js?v=aebd17e5:7714:38)
    at instantiateAllDirectives (chunk-TNFBICRJ.js?v=aebd17e5:11162:23)
    at createDirectivesInstances (chunk-TNFBICRJ.js?v=aebd17e5:10761:3)

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

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 18.1.2
Node: 20.16.0
Package Manager: npm 10.8.1
OS: darwin x64

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

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1801.2
@angular-devkit/build-angular   18.1.2
@angular-devkit/core            18.1.2
@angular-devkit/schematics      18.1.2
@schematics/angular             18.1.2
rxjs                            7.8.1
typescript                      5.5.4
zone.js                         0.14.8

Anything else?

No response

Activity

  1. JeanMeche commented on Jul 28, 2024

    @JeanMeche
    Member

    This is a fascinating bug !
    It look like that the t variable name has a colision with code generated by the angular compiler.

    Just so you know, using the inject() function works fine.

      ns = inject(t); 
      constructor() {
        console.log(this.ns);
      }
    

    Culprit code :

    let factoryFn: o.Expression = o.fn(
    [new o.FnParam('t', o.DYNAMIC_TYPE)],
    body,
    o.INFERRED_TYPE,
    undefined,
    `${meta.name}_Factory`,
    );

  2. added
    area: compiler
    on Jul 28, 2024
  3. added this to the needsTriage milestone on Jul 28, 2024
  4. crisbeto commented on Jul 29, 2024

    @crisbeto
    Member

    Will be fixed in #57181. It's worth noting that you can also work around it by using the inject function instead of @Inject.

  5. self-assigned this
    on Jul 29, 2024
  6. added a commit that references this issue on Jul 29, 2024
    31dea06
  7. added a commit that references this issue on Jul 29, 2024
    67e0940
  8. crisbeto commented on Aug 3, 2024

    @crisbeto
    Member

    Reopening since we had to roll back the fix.

  9. added a commit that references this issue on Aug 7, 2024
    6a99f83
  10. angular-automatic-lock-bot commented on Sep 7, 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.

  11. locked and limited conversation to collaborators on Sep 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: compilerworkaround2: non-obvious

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions