Skip to content

HMR breaks when using a const enum value inside decorator metadata #59800

@antur84

Description

@antur84

Command

serve, build

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

When using const enum values inside decorators, in this case in the @Component providers array, you'll get a crash when a hmr change is being applied, likely as it tries to reference it, but it's already been converted to a number and no longer exists.

Assigning it to a variable above the decorator and referencing it works fine.
Using a regular (non const) enum works fine.

We're using tsconfig "preserveConstEnums": false,, but changing it to "preserveConstEnums": true, has no effect on the above.

Minimal Reproduction

import { ChangeDetectionStrategy, Component } from '@angular/core';
import { UI_COMPONENT } from '@app/shared/utils/injection-tokens/ui-component.token';

export const enum Foo {
    bar = 0,
    baz = 1,
}

const definingTheValueOutsideDecoratorWillWork = Foo.baz;

@Component({
    selector: 'app-search-page',
    templateUrl: './search-page.component.html',
    changeDetection: ChangeDetectionStrategy.OnPush,
    // This following syntax won't survive HMR reload.
    providers: [
        {
            provide: UI_COMPONENT,
            useValue: Foo.baz,
        },
    ],
    // The syntax below works fine.
    // providers: [
    //     {
    //         provide: UI_COMPONENT,
    //         useValue: definingTheValueOutsideDecoratorWillWork,
    //     },
    // ],
})
export class SearchPageComponent {}

Exception or Error

zone.js:2227 ReferenceError: Foo is not defined
    at SearchPageComponent_UpdateMetadata (search-page.component.ts:4:27)
    at Module.ɵɵreplaceMetadata (core.mjs:32363:17)
    at search-page.component.ts:30:33
    at _ZoneDelegate.invoke (zone.js:339:158)
    at ZoneImpl.run (zone.js:110:35)
    at zone.js:2428:30
    at _ZoneDelegate.invokeTask (zone.js:364:171)
    at ZoneImpl.runTask (zone.js:163:37)
    at drainMicroTaskQueue (zone.js:535:23)

Your Environment

Angular CLI: 19.1.5
Node: 20.18.0
Package Manager: npm 10.8.1
OS: win32 x64

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

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1901.5
@angular-devkit/build-angular   19.1.5
@angular-devkit/core            19.1.5
@angular-devkit/schematics      19.1.5
@angular/cdk                    19.1.2
@angular/cli                    19.1.5
@angular/material               19.1.2
@schematics/angular             19.1.5
rxjs                            7.8.1
typescript                      5.5.4
zone.js                         0.15.0

Anything else relevant?

No response

Metadata

Metadata

Assignees

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions