From ddfa38ed3876f05e7b9aa93c613b1dd4783700ef Mon Sep 17 00:00:00 2001
From: Piotr Tomiak <piotr.tomiak@jetbrains.com>
Date: Tue, 24 Jun 2025 11:55:23 +0200
Subject: [PATCH] fix(intellij): project graph or project graph nodes may be
 null if project is misconfigured

---
 .../kotlin/dev/nx/console/angular/NxAngularConfigService.kt   | 3 ++-
 .../kotlin/dev/nx/console/nx_toolwindow/NxToolWindowPanel.kt  | 2 +-
 .../console/nx_toolwindow/tree/builder/NxTreeBuilderBase.kt   | 2 +-
 .../src/main/kotlin/dev/nx/console/models/NxWorkspace.kt      | 4 ++--
 4 files changed, 6 insertions(+), 5 deletions(-)

diff --git a/apps/intellij/src/main/kotlin/dev/nx/console/angular/NxAngularConfigService.kt b/apps/intellij/src/main/kotlin/dev/nx/console/angular/NxAngularConfigService.kt
index 4406339f11..9576922a6b 100644
--- a/apps/intellij/src/main/kotlin/dev/nx/console/angular/NxAngularConfigService.kt
+++ b/apps/intellij/src/main/kotlin/dev/nx/console/angular/NxAngularConfigService.kt
@@ -76,7 +76,8 @@ class NxAngularConfigService(private val project: Project, private val cs: Corou
             return
         }
         val projectFiles =
-            workspace.projectGraph.nodes.values
+            (workspace.projectGraph?.nodes ?: return)
+                .values
                 .asSequence()
                 // TODO: use framework metadata in the future, for now just register all projects
                 //                .filter { project ->
diff --git a/apps/intellij/src/main/kotlin/dev/nx/console/nx_toolwindow/NxToolWindowPanel.kt b/apps/intellij/src/main/kotlin/dev/nx/console/nx_toolwindow/NxToolWindowPanel.kt
index 3f486a2e38..9deb32a153 100644
--- a/apps/intellij/src/main/kotlin/dev/nx/console/nx_toolwindow/NxToolWindowPanel.kt
+++ b/apps/intellij/src/main/kotlin/dev/nx/console/nx_toolwindow/NxToolWindowPanel.kt
@@ -115,7 +115,7 @@ class NxToolWindowPanel(private val project: Project) : SimpleToolWindowPanel(tr
                                 it.second
                             }
                         }
-                    } else if (workspace == null || workspace.projectGraph.nodes.isEmpty()) {
+                    } else if (workspace == null || workspace.projectGraph?.nodes.isNullOrEmpty()) {
                         noProjectsComponent
                     } else {
                         projectTreeComponent
diff --git a/apps/intellij/src/main/kotlin/dev/nx/console/nx_toolwindow/tree/builder/NxTreeBuilderBase.kt b/apps/intellij/src/main/kotlin/dev/nx/console/nx_toolwindow/tree/builder/NxTreeBuilderBase.kt
index 27d65df3f0..2d53f7823e 100644
--- a/apps/intellij/src/main/kotlin/dev/nx/console/nx_toolwindow/tree/builder/NxTreeBuilderBase.kt
+++ b/apps/intellij/src/main/kotlin/dev/nx/console/nx_toolwindow/tree/builder/NxTreeBuilderBase.kt
@@ -178,7 +178,7 @@ abstract class NxTreeBuilderBase(private val nxWorkspace: NxWorkspace?) {
         if (nxWorkspace == null) {
             return emptyArray()
         }
-        return nxWorkspace.projectGraph.nodes
+        return (nxWorkspace.projectGraph?.nodes ?: return emptyArray())
             .filter { it.value.data.targets?.contains(targetsListNode.targetName) ?: false }
             .map {
                 NxSimpleNode.Target(
diff --git a/libs/intellij/models/src/main/kotlin/dev/nx/console/models/NxWorkspace.kt b/libs/intellij/models/src/main/kotlin/dev/nx/console/models/NxWorkspace.kt
index 3298c3fff8..cbbb4959de 100644
--- a/libs/intellij/models/src/main/kotlin/dev/nx/console/models/NxWorkspace.kt
+++ b/libs/intellij/models/src/main/kotlin/dev/nx/console/models/NxWorkspace.kt
@@ -4,7 +4,7 @@ import kotlinx.serialization.*
 
 data class NxWorkspace(
     val validWorkspaceJson: Boolean,
-    val projectGraph: NxProjectGraph,
+    val projectGraph: NxProjectGraph?,
     val daemonEnabled: Boolean?,
     val workspacePath: String,
     val errors: Array<NxError>?,
@@ -53,7 +53,7 @@ data class NxWorkspace(
 
 data class WorkspaceLayout(val appsDir: String?, val libsDir: String?)
 
-data class NxProjectGraph(val nodes: Map<String, NxProjectGraphProjectNode>)
+data class NxProjectGraph(val nodes: Map<String, NxProjectGraphProjectNode>?)
 
 data class NxProjectGraphProjectNode(val name: String, val type: String, val data: NxProject)