Skip to content

bug(cdk/a11y): HighContrastModeDetector crashes with NG0205 when combined with withAutoCleanupInjectors() #33919

Description

@vinnisch

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

Root cause

HighContrastModeDetector is declared without providedIn: 'root':

The first inject(HighContrastModeDetector) call from A11yModule.constructor() therefore creates the instance in the route-scoped injector that A11yModule is imported into (via a Material component). withAutoCleanupInjectors() tears down that injector on navigation. Next navigation triggers a fresh A11yModule constructor whose inject-chain references the destroyed injector → NG0205.

Fix suggestion: Add providedIn: 'root' to the @Injectable of HighContrastModeDetector (and audit the other a11y services for the same shape).

Workaround

Add HighContrastModeDetector to root providers manually:

providers: [..., HighContrastModeDetector]





### Reproduction

- Angular 22.2.0, @angular/cdk 22.2.1
- `provideRouter(..., withAutoCleanupInjectors())`
- Lazy-loaded route uses any Angular Material component (MatInput, MatButton, MatDialog, ...)
- Navigate: /route-a → /route-b → /route-a  (triggers A11yModule twice)


### Expected Behavior

Navigation succeeds.




### Actual Behavior

Second visit to a route that imports A11yModule throws:
`ERROR Error: NG0205` (InjectorAlreadyDestroyed)

Stack bottoms out at `HighContrastModeDetector` factory being called in an already-destroyed route injector.

### Environment

- Angular: 22.2.0 (@angular/core, @angular/common, @angular/router, @angular/platform-browser, @angular/compiler-cli)
- @angular/cdk: 22.2.0 (also reproduced on 22.2.1 and 22.3.0-next.0 — unchanged a11y code)
- @angular/material: 22.2.0
- @angular/cli / @angular/build: 22.2.0
- Node: v24.15.0
- npm: 11.12.1
- TypeScript target: ES2022, moduleResolution: bundler, strict: true, strictTemplates: true
- Rendering: ZoneJS active (zoneless-compatible code via Signals)
- Browser (via Playwright): Chromium 153 (headless + headed)
- OS: Windows 11 Pro 10.0.26200 (CI reproduces more frequently on Linux containers)
- Router config:
  provideRouter(
    routes,
    withComponentInputBinding(),
    withRouterConfig({ paramsInheritanceStrategy: "always" }),
    withPreloading(PreloadAllModules),
    withAutoCleanupInjectors(),
  )

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: cdk/a11ygemini-triagedLabel noting that an issue has been triaged by geminineeds: clarificationThe issue does not contain enough information for the team to determine if it is a real bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions