Skip to content

bug(cdk/layout): Circular dependency in DI detected for InjectionToken CSP nonce. #30567

@PuffPastry

Description

@PuffPastry

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

15

Description

This seems a very odd issue.

I have an older app that was building well in v15, which I recently have been attempting to upgrade to v19. This caused it to run into the issue that no routes can be build (even though I have set the serverRoutes to RenderMode.Client).

The error that always pops up on first render when running the app with ng serve is

ERROR RuntimeError: NG0200: Circular dependency in DI detected for InjectionToken CSP nonce. Find more at https://angular.dev/errors/NG0200
    at throwCyclicDependencyError (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:1401:9)
    at R3Injector.hydrate (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:2186:9)
    at R3Injector.get (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:2085:23)
    at injectInjectorOnly (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:1476:36)
    at ɵɵinject (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:1482:59)
    at Object.SharedStylesHost_Factory (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ULZ52YRG.js:442:103)
    at eval (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:2191:35)
    at runInInjectorProfilerContext (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:1344:5)
    at R3Injector.hydrate (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:2190:11)
    at R3Injector.get (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:2085:23) {
  code: -200
}
Error: NG0210: The document object is not available in this context. Make sure the DOCUMENT injection token is provided.
    at getDocument (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:5720:9)
    at Object.factory (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:5738:12)
    at eval (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:2191:35)
    at runInInjectorProfilerContext (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:1344:5)
    at R3Injector.hydrate (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:2190:11)
    at R3Injector.get (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:2085:23)
    at injectInjectorOnly (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:1476:36)
    at ɵɵinject (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:1482:59)
    at inject (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-ZMU7LZMM.js:1491:10)
    at _MediaMatcher.<instance_members_initializer> (/path/to/project/.angular/cache/19.1.8/portal/vite/deps_ssr/chunk-LJJNQH74.js:51:18)```

I cannot for the life of me figure out why the csp nonce is not being injected properly, or whether this is an issue coming from another component. 

After the first render fails in ng serve, the app runs perfectly afterwards.

### Reproduction

the stackblitz doesn't let me build with v19

### Expected Behavior

no errors

### Actual Behavior

see above stack trace

### Environment

- Angular: v19.x
- CDK/Material: v19.x
- Browser(s): All
- Operating System (e.g. Windows, macOS, Ubuntu): macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs triageThis issue needs to be triaged by the team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions