fix(metadata): `setClassMetadata` names `Inject` / `Optional` / tokens whose imports OXC removed (ReferenceError under TestBed overrides)
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 76/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- angular, rust, typescript
- 领域
- compilers
调研方向
Start in crates/oxc_angular_compiler/src/component/import_elision.rs, then trace class_metadata/builders.rs, especially the constructor-parameter import collection and metadata builder named in the issue. Run the supplied TestBed override reproduction with metadata enabled; done means the generated module retains imports needed by ɵsetClassMetadata and the override no longer raises ReferenceError.
由索引模型根据 Issue 内容生成。
描述
OXC removes the imports of Angular's constructor parameter decorators (Inject, Optional, Self, SkipSelf, Host, Attribute) and of tokens used only inside @Inject(...). The factory doesn't need them (it uses i0. / i1. namespace imports). But ɵsetClassMetadata, which is emitted by default, still names them as bare identifiers in its ctorParameters callback. Those names no longer exist in the module.
The callback is lazy, so a normal app works. It throws as soon as Angular reads ctorParameters, for example when TestBed.overrideComponent / overrideDirective / overridePipe / overrideModule recompiles the class in JIT (reflectDependencies → ReflectionCapabilities.parameters → ctorParameters()).
ngtsc keeps these imports.
Repro
// tokens.ts
import {InjectionToken} from '@angular/core';
export const TOKEN = new InjectionToken<unknown>('TOKEN');
// test.ts
import {Component, Inject, Optional} from '@angular/core';
import {TOKEN} from './tokens';
@Component({selector: 'c', template: ''})
export class C {
constructor(@Optional() @Inject(TOKEN) x: unknown) {}
}
// spec (plain node, @angular/core 22.1.7)
TestBed.initTestEnvironment(BrowserTestingModule, platformBrowserTesting());
TestBed.configureTestingModule({imports: [C], providers: [{provide: TOKEN, useValue: 1}, C]});
TestBed.overrideComponent(C, {set: {template: 'overridden'}});
TestBed.inject(C);
OXC (#504 tip and main, same output)
import { Component } from "@angular/core"; // Inject, Optional: gone
import * as i0 from '@angular/core';
import * as i1 from './tokens'; // `import {TOKEN}`: gone
export class C {
static ɵfac = function C_Factory(__ngFactoryType__) {
return new (__ngFactoryType__ || C)(i0.ɵɵdirectiveInject(i1.TOKEN,8));
};
...
}
(() =>{
(((typeof ngDevMode === "undefined") || ngDevMode) && i0.ɵsetClassMetadata(C,[{type:Component,
args:[{selector:"c",template:""}]}],() =>[{type:undefined,decorators:[{type:Optional},
{type:Inject,args:[TOKEN]}]}],null));
})();
Spec result: ReferenceError: Optional is not defined (without the overrideComponent line: ok).
ngtsc 22.1.7
import { Component, Inject, Optional } from '@angular/core';
import { TOKEN } from './tokens';
import * as i0 from "@angular/core";
...
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(C, [{
type: Component,
args: [{ selector: 'c', template: '' }]
}], () => [{ type: undefined, decorators: [{
type: Optional
}, {
type: Inject,
args: [TOKEN]
}] }], null); })();
Spec result: ok.
The same happens in an @NgModule / @Directive / @Pipe / @Injectable constructor, and with a token declared in the same file (then only Inject / Optional dangle).
When it runs
emitClassMetadatadefaults totruein the Vite plugin (vite-plugin/index.ts~L124 / L255) and in the NAPI options (src/lib.rs~L262).- The call is behind
(typeof ngDevMode === "undefined" || ngDevMode). The Vite plugin setsngDevMode: 'false'only for production builds (angular-build-optimizer-plugin.ts, which also stripsɵsetClassMetadata). So it runs in dev serve and in tests (vitest + TestBed).
Where
crates/oxc_angular_compiler/src/component/import_elision.rs: PARAM_DECORATORS (~L53) and collect_ctor_param_decorator_only_imports / collect_uses_from_constructor_params (tip ~L331 / ~L512, main ~L329 / ~L500) mark these imports for removal without checking whether class metadata will still reference them. The metadata is built in class_metadata/builders.rs (build_ctor_params_metadata, ~L323) with the original bare names.
On main?
Yes.
Direction
Keep these imports whenever class metadata is emitted (emit_class_metadata && !advanced_optimizations), like ngtsc. Only remove them when no setClassMetadata is emitted.
- 主要语言
- Rust
- 星标
- 232
- 派生
- 21
- 平均合并
- 2 天 14 小时
- 30 天内合并 PR
- 30
环境准备
这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
voidzero-dev/oxc-angular-compiler 的其他 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 85/100
voidzero-dev/oxc-angular-compiler#511 ·
维护者通常 1 天内回复
-
bug
难度 3/5 1-2 天 新手友好度 78/100
voidzero-dev/oxc-angular-compiler#522 ·
维护者通常 1 天内回复
-
question
难度 5/5 一周以上 新手友好度 35/100
voidzero-dev/oxc-angular-compiler#521 ·
维护者通常 1 天内回复
-
bug
难度 3/5 1-2 天 新手友好度 76/100
voidzero-dev/oxc-angular-compiler#519 ·
维护者通常 1 天内回复
-
enhancement
难度 5/5 一周以上 新手友好度 35/100
voidzero-dev/oxc-angular-compiler#518 ·
维护者通常 1 天内回复
查看 voidzero-dev/oxc-angular-compiler 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 85/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
install: root SSH tmpfiles.d drop-in is labeled etc_runtime_t instead of etc_t可能已有人在做 @andrewdunndev 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
[Misdetection] `text/tab-separated-values` file misdetected as `text/tsv`可能已有人在做 @bact 今天认领。 未关闭misdetection needs triage
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 1 天内回复
-
C-bug
难度 2/5 1-3 小时 新手友好度 72/100
维护者通常 2 天内回复