Skip to content

HMR broken with dynamic component providers #59634

@mattlewis92

Description

@mattlewis92

Which @angular/* package(s) are the source of the bug?

compiler

Is this a regression?

Yes

Description

When trying out the new component HMR we ran into a couple issues when using dynamic component providers

For example:

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  providers: [isDevelopmentMode ? DirectiveTimingService : []],
  standalone: false,
})
export class AppComponent {}

Throws app.component.ts:108 ReferenceError: DirectiveTimingService is not defined at runtime:

Image

Not sure if this is the same or a different issue, but declaring a provider with useFactory and deps also throws an error

@Component({
  selector: 'cu-prompt-template-empty-description',
  templateUrl: './prompt-template-empty-description.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  providers: [
    {
      provide: DropdownListKeyboardController,
      useFactory: (
        parentInjector: Injector,
        dropdownListKeyboardController?: DropdownListKeyboardController,
      ) => {
        if (!dropdownListKeyboardController) {
          const injector = Injector.create({
            providers: [{ provide: DropdownListKeyboardController }],
            parent: parentInjector,
          });
          dropdownListKeyboardController = injector.get(
            DropdownListKeyboardController,
          );
        }

        return dropdownListKeyboardController;
      },
      deps: [
        Injector,
        [new Optional(), new SkipSelf(), DropdownListKeyboardController],
      ],
    },
  ],
  standalone: false,
})
export class PromptTemplateEmptyDescriptionComponent {}

Will throw ReferenceError: Optional is not defined:

Image

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

Please let me know if the information above isn't sufficient to create a test case!

Please provide the environment you discovered this bug in (run ng version)

Angular: 19.1.2
... animations, common, compiler, compiler-cli, core, elements
... forms, language-service, localize, platform-browser
... platform-browser-dynamic, router, service-worker

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1901.3
@angular-devkit/build-angular   19.1.3
@angular-devkit/core            19.1.3
@angular-devkit/schematics      19.1.3
@angular/build                  19.1.3
@angular/cdk                    18.2.2
@angular/google-maps            15.2.9
@schematics/angular             19.1.3
ng-packagr                      19.0.1
rxjs                            7.8.0
typescript                      5.6.3
webpack                         5.96.1
zone.js                         0.14.4

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions