diff --git a/packages/core/src/render3/util/injector_discovery_utils.ts b/packages/core/src/render3/util/injector_discovery_utils.ts index 02352299074..dd028611245 100644 --- a/packages/core/src/render3/util/injector_discovery_utils.ts +++ b/packages/core/src/render3/util/injector_discovery_utils.ts @@ -151,6 +151,12 @@ function getProviderImportsContainer(injector: Injector): Type|null { return null; } + // In standalone applications, the root environment injector created by bootstrapApplication + // may have no associated "instance". + if (defTypeRef.instance === null) { + return null; + } + return defTypeRef.instance.constructor; } @@ -351,13 +357,29 @@ function walkProviderTreeToDiscoverImportPaths( * @returns an array of objects representing the providers of the given injector */ function getEnvironmentInjectorProviders(injector: EnvironmentInjector): ProviderRecord[] { + const providerRecords = getFrameworkDIDebugData().resolverToProviders.get(injector) ?? []; + + // platform injector has no provider imports container so can we skip trying to + // find import paths + if (isPlatformInjector(injector)) { + return providerRecords; + } + const providerImportsContainer = getProviderImportsContainer(injector); if (providerImportsContainer === null) { + // There is a special case where the bootstrapped component does not + // import any NgModules. In this case the environment injector connected to + // that component is the root injector, which does not have a provider imports + // container (and thus no concept of module import paths). Therefore we simply + // return the provider records as is. + if (isRootInjector(injector)) { + return providerRecords; + } + throwError('Could not determine where injector providers were configured.'); } const providerToPath = getProviderImportPaths(providerImportsContainer); - const providerRecords = getFrameworkDIDebugData().resolverToProviders.get(injector) ?? []; return providerRecords.map(providerRecord => { let importPath = providerToPath.get(providerRecord.provider) ?? [providerImportsContainer]; @@ -374,6 +396,14 @@ function getEnvironmentInjectorProviders(injector: EnvironmentInjector): Provide }); } +function isPlatformInjector(injector: Injector) { + return injector instanceof R3Injector && injector.scopes.has('platform'); +} + +function isRootInjector(injector: Injector) { + return injector instanceof R3Injector && injector.scopes.has('root'); +} + /** * Gets the providers configured on an injector. *