From 9dffdd744dea0ee4942029bad2c62943710153d2 Mon Sep 17 00:00:00 2001 From: Doug Parker Date: Fri, 14 Mar 2025 15:46:17 -0700 Subject: [PATCH] refactor(devtools): fix reading `resolutionPath` (#60403) Sometimes `forest` can be empty if the provided roots are empty, and was leading to a "Cannot read `resolutionPath` of `undefined`" error. Now we check the forest has a tree in it before looking up `resolutionPath`. There might be a separate issue with the fact that the backend script likely shouldn't be emitting an empty forest in the first place. However we already check that a resolution path exists at all, so I think it's fair to also check that a tree was provided. We can separately look into making sure the backend is emitting valid data. PR Close #60403 --- .../src/lib/client-event-subscribers.ts | 14 ++++++++------ .../injector-tree/injector-tree.component.ts | 12 +++++++----- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/devtools/projects/ng-devtools-backend/src/lib/client-event-subscribers.ts b/devtools/projects/ng-devtools-backend/src/lib/client-event-subscribers.ts index 3f0049c37d2..9c81935b24c 100644 --- a/devtools/projects/ng-devtools-backend/src/lib/client-event-subscribers.ts +++ b/devtools/projects/ng-devtools-backend/src/lib/client-event-subscribers.ts @@ -222,13 +222,15 @@ const getRoutes = (messageBus: MessageBus) => { initializeOrGetDirectiveForestHooks().getIndexedDirectiveForest(), ngDebugDependencyInjectionApiIsSupported(), ); + if (forest.length === 0) return; + const rootInjector = (forest[0].resolutionPath ?? []).find((i) => i.name === 'Root'); - if (rootInjector) { - const route = getRouterConfigFromRoot(rootInjector); - if (route) { - messageBus.emit('updateRouterTree', [[route]]); - } - } + if (!rootInjector) return; + + const route = getRouterConfigFromRoot(rootInjector); + if (!route) return; + + messageBus.emit('updateRouterTree', [[route]]); }; const getSerializedProviderRecords = (injector: SerializedInjector) => { diff --git a/devtools/projects/ng-devtools/src/lib/devtools-tabs/injector-tree/injector-tree.component.ts b/devtools/projects/ng-devtools/src/lib/devtools-tabs/injector-tree/injector-tree.component.ts index 3c838c666ad..22faaad7373 100644 --- a/devtools/projects/ng-devtools/src/lib/devtools-tabs/injector-tree/injector-tree.component.ts +++ b/devtools/projects/ng-devtools/src/lib/devtools-tabs/injector-tree/injector-tree.component.ts @@ -91,11 +91,13 @@ export class InjectorTreeComponent { private init() { this._messageBus.on('latestComponentExplorerView', (view: ComponentExplorerView) => { - if (view.forest[0].resolutionPath !== undefined) { - this.diDebugAPIsAvailable.set(true); - this.rawDirectiveForest = view.forest; - this.updateInjectorTreeVisualization(view.forest); - } + if (view.forest.length === 0) return; + + if (!view.forest[0].resolutionPath) return; + + this.diDebugAPIsAvailable.set(true); + this.rawDirectiveForest = view.forest; + this.updateInjectorTreeVisualization(view.forest); }); this._messageBus.on(