Skip to content

fix(compiler-cli): emit ɵsetClassMetadata in static blocks in ngtsc - #72136

Draft
mattrbeck wants to merge 3 commits into
angular:mainfrom
mattrbeck:fix/ngtsc-set-class-metadata-static-block
Draft

mattrbeck wants to merge 3 commits into
angular:mainfrom
mattrbeck:fix/ngtsc-set-class-metadata-static-block

Conversation

@mattrbeck

@mattrbeck mattrbeck commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Alternative to #72101: instead of moving ɵsetClassMetadata / ɵsetClassMetadataAsync out of static {} in ngp whenever a class has surviving member or method-parameter decorators, this PR updates ngtsc to emit ɵsetClassMetadata / ɵsetClassMetadataAsync inside a static { ... } block on the class whenever the class is not wrapped by Cls = __decorate(..., Cls) (i.e. when there are no surviving non-Angular class or constructor-parameter decorators).

When surviving non-Angular class or constructor-parameter decorators are present, both ngtsc and ngp emit ɵsetClassMetadata as a trailing IIFE after __decorate (ng-analyze adds has_non_angular_ctor_param_decorators so ngp matches ngtsc for constructor-parameter decorators). Because ngtsc and ngp now agree on static {} placement, this also removes the ctx.complianceMode special case for ɵsetClassMetadata in ngp.

Split into 3 commits for reviewability:

  1. ngtsc & ngp implementation + unit specs (transform.ts, processor.ts, ng-analyze, ngtsc_spec.ts)
  2. @angular/build oxc-transform static-block metadata elision (temporary pnpm patch corresponding to upstream PR fix(@angular/build): elide Angular class metadata in static blocks in oxc-transform angular-cli#34770 so full-mode @angular/core classes with static {} metadata continue to be pure-wrapped and tree-shaken)
  3. Compliance .js golden updates (expect_emit.ts + 591 compliance goldens)

Minimal failure example

import {Component} from '@angular/core';

function Log(): MethodDecorator {
  return (_t, _k, d) => d;
}

@Component({selector: 'my-cmp', template: ''})
export class MyCmp {
  @Log()
  greet() {}
}

Before (main ngtsc):

export class MyCmp {
  greet() {}
  static ɵfac = function MyCmp_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || MyCmp)(); };
  static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: MyCmp, selectors: [["my-cmp"]], decls: 0, vars: 0, template: function MyCmp_Template(rf, ctx) {} });
}
__decorate([
  Log()
], MyCmp.prototype, "greet", null);
(() => {
  (typeof ngDevMode === "undefined" || ngDevMode) &&
    i0.ɵsetClassMetadata(MyCmp, [{ type: Component, args: [{ selector: 'my-cmp', template: '' }] }], null, null);
})();
// On pre-ES2022 targets (e.g. ES2020), `ngp` downleveled `static {}` into an IIFE *before*
// `__decorate([Log()], MyCmp.prototype, ...)`, whereas `ngtsc` emitted its IIFE *after* `__decorate`.

After (ngtsc & ngp):

export class MyCmp {
  greet() {}
  static ɵfac = function MyCmp_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || MyCmp)(); };
  static ɵcmp = /*@__PURE__*/ i0.ɵɵdefineComponent({ type: MyCmp, selectors: [["my-cmp"]], decls: 0, vars: 0, template: function MyCmp_Template(rf, ctx) {} });
  static {
    (typeof ngDevMode === "undefined" || ngDevMode) &&
      i0.ɵsetClassMetadata(MyCmp, [{ type: Component, args: [{ selector: 'my-cmp', template: '' }] }], null, null);
  }
}
__decorate([
  Log()
], MyCmp.prototype, "greet", null);
// On pre-ES2022 targets, TypeScript downlevels `static {}` into an IIFE immediately after `MyCmp.ɵcmp`
// and *before* `MyCmp.prototype` `__decorate` in both `ngtsc` and `ngp`. Keeping `ɵsetClassMetadata`
// inside `static {}` also keeps any instance `#private` field references in decorator metadata
// lexically inside the class body instead of producing a JS `SyntaxError`.

Reservations (vs. #72101)

  1. Requires @angular/build (oxc-transform) update first (fix(@angular/build): elide Angular class metadata in static blocks in oxc-transform angular-cli#34770): Commit 2 temporarily patches @angular/build via patchedDependencies so oxc-transform removes ngDevMode/ngJitMode-guarded (ɵ)setClassMetadata* StaticBlocks and wraps pure static Angular classes in @angular/core/fesm2022/core.mjs (//packages/core/test/bundling/* and integration/standalone-bootstrap); before landing, fix(@angular/build): elide Angular class metadata in static blocks in oxc-transform angular-cli#34770 should release and roll in normally (which ngp also needs before packages/core switches to ngp).
  2. Compliance golden churn & partial-linker divergence: Updating ngtsc modifies 591 compliance .js goldens (and shifts ES5 downleveled ɵsetClassMetadata inside the ES5 class closure right before return Cls;). Partial compilation (GOLDEN_PARTIAL.js) keeps ɵɵngDeclareClassMetadata as a top-level statement after the class to preserve the v12.0.0 partial-declaration format, so linked output still emits a trailing IIFE (normalized in expect_emit.ts via relocateStaticClassMetadataForLinked).
  3. Execution order relative to member / static __decorate: In dev / TestBed (ngDevMode = true), ɵsetClassMetadata inside static {} now executes before tslib.__decorate runs on prototype or static members. If an Angular decorator expression re-emitted in ɵsetClassMetadata eagerly calls a @Decorated() static method on the same class, or if a custom member decorator writes directly to target.constructor.propDecorators, ɵsetClassMetadata runs before that decorator rather than after it.
  4. Classes wrapped by __decorate still emit outside static {}: While this eliminates the ~600-line OutsideMetadataScope @ts-ignore visibility-tracking machinery needed by fix(compiler-cli): emit ɵsetClassMetadata outside classes with member decorators #72101 for member and method-parameter decorators, classes with surviving custom class or constructor-parameter decorators still emit ɵsetClassMetadata outside the class after Cls = __decorate([...], Cls).

@angular-robot angular-robot Bot added the area: compiler Issues related to `ngc`, Angular's template compiler label Nov 20, 2026
@ngbot ngbot Bot added this to the Backlog milestone Nov 20, 2026
@crisbeto

Copy link
Copy Markdown
Member

I don't know if it's still the case, but I think the CLI had a transformer specifically to tree shake the old shape. We might have to double check that it won't break.

@mattrbeck
mattrbeck force-pushed the fix/ngtsc-set-class-metadata-static-block branch from ba52915 to 7167b0d Compare November 20, 2026 11:59
@mattrbeck
mattrbeck force-pushed the fix/ngtsc-set-class-metadata-static-block branch from 7167b0d to ddb5d5c Compare November 20, 2026 16:58
@angular-robot angular-robot Bot added the area: build & ci Related the build and CI infrastructure of the project label Nov 20, 2026
Update IvyTransformationVisitor to unwrap ɵsetClassMetadata and
ɵsetClassMetadataAsync IIFEs into a class static block whenever the class has
no surviving non-Angular class or constructor-parameter decorators.

Align ng-analyze via has_non_angular_ctor_param_decorators so ngp also emits
ɵsetClassMetadata outside the class when custom constructor-parameter
decorators wrap the class, and remove the complianceMode special case for
ɵsetClassMetadata in ngp.
@mattrbeck
mattrbeck force-pushed the fix/ngtsc-set-class-metadata-static-block branch from ddb5d5c to 809da3c Compare November 20, 2026 17:06
Temporarily patch @angular/build oxc-transform (matching upstream PR
angular/angular-cli#34770) so Angular class metadata inside ES2022 static
blocks (including ngDevMode-guarded internal setClassMetadata calls in
@angular/core FESM bundles) is elided and pure static Angular classes continue
to be wrapped for tree-shaking.
Update full and local compliance .js goldens for static-block ɵsetClassMetadata
emission in ngtsc, and normalize linked partial declarations in expectEmit
since partial compilation preserves top-level ɵɵngDeclareClassMetadata.
@mattrbeck
mattrbeck force-pushed the fix/ngtsc-set-class-metadata-static-block branch from 809da3c to 418a90a Compare November 20, 2026 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: build & ci Related the build and CI infrastructure of the project area: compiler Issues related to `ngc`, Angular's template compiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants