Skip to content

Optional injection does not handle missing aliased (useExisting) ProviderToken #61121

Closed

Description

@TobiasB5

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

core

Is this a regression?

No

Description

We noticed that inject(XYZ, { optional: true }) does not work if the given ProviderToken is using useExisting to refer to another, unavailable ProviderToken.

We would have expected the given inject call to just return null in case the aliased ProviderToken is not available. Instead NullInjectorError and an erroneous Error: NG0200 are thrown.

According to the inject functions documentation, it should not throw DI Errors if called with optional injection.
The erroneous NG0200 is caused by the first error leaving the Injectors record in the creating state.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/jpygnzwp?file=src%2Fmain.ts

Please provide the exception or error you saw

error1 NullInjectorError: R3InjectorError(Environment Injector)[InjectionToken relayed injectable -> InjectionToken base injectable -> InjectionToken base injectable]: 
  NullInjectorError: No provider for InjectionToken base injectable!
    Angular 16
    _PlaygroundComponent main.ts:15
    PlaygroundComponent_Factory main.js:34222
    Angular 41
    <anonymous> main.ts:29
main.ts:18:20
error2 Error: NG0200: Circular dependency in DI detected for InjectionToken relayed injectable. Find more at https://angular.dev/errors/NG0200
    Angular 10
    _PlaygroundComponent main.ts:21
    PlaygroundComponent_Factory main.js:34222
    Angular 41
    <anonymous> main.ts:29

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

_                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 19.2.5
Node: 20.19.1
Package Manager: npm 10.8.2
OS: linux x64

Angular: 19.2.4
... animations, common, compiler, compiler-cli, core, forms
... platform-browser

Package                      Version
------------------------------------------------------
@angular-devkit/architect    0.1902.5
@angular-devkit/core         19.2.5
@angular-devkit/schematics   19.2.5
@angular/build               19.2.5
@angular/cdk                 19.2.7
@angular/cli                 19.2.5
@angular/material            19.2.7
@schematics/angular          19.2.5
rxjs                         7.8.2
typescript                   5.8.2
zone.js                      0.15.0

Anything else?

No response

Activity

  1. added
    area: core
    on May 5, 2025
  2. added this to the needsTriage milestone on May 5, 2025
  3. self-assigned this
    on May 6, 2025
  4. added a commit that references this issue on May 6, 2025
    583b9a7
  5. added a commit that references this issue on May 6, 2025
    a1ec298
  6. added a commit that references this issue on May 6, 2025
    4623b61
  7. angular-automatic-lock-bot commented on Jun 6, 2025

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  8. locked and limited conversation to collaborators on Jun 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: corebugcore: distate: has PR

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions