Skip to content

fix(metadata): wrap signal query predicates in forwardRef in setClassMetadata - #602

Open
Rush wants to merge 1 commit into
voidzero-dev:mainfrom
encharm:fix/class-metadata-forward-ref
Open

Rush wants to merge 1 commit into
voidzero-dev:mainfrom
encharm:fix/class-metadata-forward-ref

Conversation

@Rush

@Rush Rush commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

A signal query whose predicate is a class declared later in the file:

@Component({ selector: 'd-parent', template: '<ng-content />' })
export class DParent {
  panes = contentChildren(DPane);
}
@Component({ selector: 'd-pane', template: '' })
export class DPane {}

emitted setClassMetadata(DParent, ..., { panes: [{ type: i0.ContentChildren, args: [DPane, { isSignal: true }] }] }). setClassMetadata runs as soon as the class is defined, so in development builds DPane was read before its initialization: ReferenceError: Cannot access 'DPane' before initialization, which stops the app. (The query initializer itself is fine: it runs later.)

ngtsc's memberMetadataFromSignalQuery wraps the predicate in forwardRef unless it is a string or a call expression, for this reason; this does the same:

panes: [{ type: i0.ContentChildren, args: [i0.forwardRef(() => DPane), { isSignal: true }] }]

It applies whatever the declaration order, as in ngtsc. A string predicate (viewChildren('ref')) and decorator queries are left as written. One deliberate difference: a spread first argument (viewChild(...args)) isn't wrapped, since forwardRef(() => ...args) isn't valid JavaScript.

tests/signal_query_metadata_forward_ref_test.rs checks both declaration orders, an options object ({ ...{ read: ElementRef }, isSignal: true }), a string predicate and decorator queries against @angular/compiler-cli 21.0.1's output.

…Metadata

`contentChildren(Pane)` in a class declared before `Pane` emitted
`setClassMetadata(..., { panes: [{ type: i0.ContentChildren, args: [Pane, ...] }] })`,
which reads `Pane` as soon as the class is defined: a TDZ ReferenceError in
development builds. Like ngtsc's `memberMetadataFromSignalQuery`, a predicate
other than a string or a call is now `i0.forwardRef(() => Pane)`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant