Repository navigation
Closed
Labels
Milestone
Description
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
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
Reacted by Bojan Kogoj
Activity
JeanMeche commentedon Jul 28, 2024
This is a fascinating bug !
It look like that the
tvariable name has a colision with code generated by the angular compiler.Just so you know, using the
inject()function works fine.Culprit code :
angular/packages/compiler/src/render3/r3_factory.ts
Lines 186 to 192 in ca8bd5b
crisbeto commentedon Jul 29, 2024
Will be fixed in #57181. It's worth noting that you can also work around it by using the
injectfunction instead of@Inject.fix(compiler): reduce chance of conflicts between generated factory a…
fix(compiler): reduce chance of conflicts between generated factory a…
crisbeto commentedon Aug 3, 2024
Reopening since we had to roll back the fix.
fix(compiler): reduce chance of conflicts between generated factory a…
angular-automatic-lock-bot commentedon Sep 7, 2024
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.