From 7a946579cb81568e9b03e639dae90a910873a750 Mon Sep 17 00:00:00 2001 From: Daniel Brauner Date: Mon, 24 Nov 2025 11:55:44 +0100 Subject: [PATCH] added check if current directory is a Bazel workspace root to CLionNotificationProvider --- .../AutoImportProjectOpenProcessor.java | 7 ++-- .../blaze/clwb/CLionNotificationProvider.kt | 37 ++++++++++++++++--- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/base/src/com/google/idea/blaze/base/project/AutoImportProjectOpenProcessor.java b/base/src/com/google/idea/blaze/base/project/AutoImportProjectOpenProcessor.java index dfb4cacb66d..447298f8814 100644 --- a/base/src/com/google/idea/blaze/base/project/AutoImportProjectOpenProcessor.java +++ b/base/src/com/google/idea/blaze/base/project/AutoImportProjectOpenProcessor.java @@ -37,6 +37,7 @@ import java.io.IOException; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; import java.util.Objects; import java.util.Optional; import javax.swing.Icon; @@ -58,6 +59,8 @@ */ public class AutoImportProjectOpenProcessor extends ProjectOpenProcessor { + public static final String[] WORKSPACE_MARKER_FILES = {"WORKSPACE", "WORKSPACE.bazel", "MODULE.bazel"}; + public static final String MANAGED_PROJECT_RELATIVE_PATH = "tools/intellij/.managed.bazelproject"; public static final String PROJECT_VIEW_FROM_ENV = "INTELLIJ_BAZEL_PROJECT_VIEW_TEMPLATE"; @@ -97,9 +100,7 @@ public boolean canOpenProject(@NotNull VirtualFile virtualFile) { } private boolean isBazelWorkspace(VirtualFile virtualFile) { - return virtualFile.findChild("WORKSPACE") != null - || virtualFile.findChild("WORKSPACE.bazel") != null - || virtualFile.findChild("MODULE.bazel") != null; + return Arrays.stream(WORKSPACE_MARKER_FILES).anyMatch((file) -> virtualFile.findChild(file) != null); } @Override diff --git a/clwb/src/com/google/idea/blaze/clwb/CLionNotificationProvider.kt b/clwb/src/com/google/idea/blaze/clwb/CLionNotificationProvider.kt index 25eb10320e1..740dfa380bd 100644 --- a/clwb/src/com/google/idea/blaze/clwb/CLionNotificationProvider.kt +++ b/clwb/src/com/google/idea/blaze/clwb/CLionNotificationProvider.kt @@ -16,6 +16,7 @@ package com.google.idea.blaze.clwb import com.google.idea.blaze.base.lang.buildfile.language.BuildFileType +import com.google.idea.blaze.base.project.AutoImportProjectOpenProcessor import com.google.idea.blaze.base.settings.Blaze import com.google.idea.blaze.base.sync.data.BlazeDataStorage import com.google.idea.blaze.base.wizard2.BazelImportCurrentProjectAction @@ -27,8 +28,13 @@ import com.intellij.openapi.vfs.VirtualFile import com.intellij.util.concurrency.annotations.RequiresReadLock import com.jetbrains.cidr.lang.daemon.OCFileScopeProvider.Companion.getProjectSourceLocationKind import com.google.idea.sdkcompat.clion.projectStatus.* +import com.intellij.util.concurrency.annotations.RequiresReadLockAbsence import com.jetbrains.cidr.project.workspace.CidrWorkspace +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext import java.io.File +import java.nio.file.Files +import java.nio.file.Path /// This function is a little overloaded, ensures the project could be imported // and returns the potential workspace root. @@ -38,7 +44,7 @@ private fun guessWorkspaceRoot(project: Project, file: VirtualFile?): String? { return null } - if (!Blaze.isBlazeProject(project)) { + if (Blaze.isBlazeProject(project)) { return null } @@ -56,13 +62,32 @@ private fun guessWorkspaceRoot(project: Project, file: VirtualFile?): String? { return workspaceRoot.removeSuffix(BlazeDataStorage.PROJECT_DATA_SUBDIRECTORY) } -@RequiresReadLock -private fun guessWidgetStatus(project: Project, currentFile: VirtualFile?): WidgetStatus? { +@RequiresReadLockAbsence +private fun validateWorkspaceRoot(workspaceRoot: String): Boolean { + return AutoImportProjectOpenProcessor.WORKSPACE_MARKER_FILES.any { + Files.exists(Path.of(workspaceRoot, it)) + } +} + +@RequiresReadLockAbsence +private suspend fun guessWidgetStatus(project: Project, currentFile: VirtualFile?): WidgetStatus? { if (Blaze.isBlazeProject(project)) { return DefaultWidgetStatus(Status.OK, Scope.Project, "Bazel project is configured") } - if (currentFile == null || guessWorkspaceRoot(project, currentFile) == null) { + if (currentFile == null) { + return null + } + + val workspaceRoot = readAction { + guessWorkspaceRoot(project, currentFile) + } + + if (workspaceRoot == null) { + return null + } + + if (withContext(Dispatchers.IO) { !validateWorkspaceRoot(workspaceRoot) }) { return null } @@ -86,13 +111,13 @@ class BazelProjectFixesProvider : ProjectFixesProvider { class BazelWidgetStatusProvider : WidgetStatusProvider { override suspend fun computeWidgetStatus(project: Project, currentFile: VirtualFile?): WidgetStatus? { - return readAction { guessWidgetStatus(project, currentFile) } + return guessWidgetStatus(project, currentFile) } } class BazelEditorNotificationProvider : EditorNotificationWarningProvider { override suspend fun computeProjectNotification(project: Project, file: VirtualFile): ProjectNotification? { - return convertStatus(readAction { guessWidgetStatus(project, file) }) + return convertStatus(guessWidgetStatus(project, file)) } }