Skip to content

Commit 7a94657

Browse files
committed
added check if current directory is a Bazel workspace root to CLionNotificationProvider
1 parent 52f2c0c commit 7a94657

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

base/src/com/google/idea/blaze/base/project/AutoImportProjectOpenProcessor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.io.IOException;
3838
import java.nio.file.Path;
3939
import java.nio.file.Paths;
40+
import java.util.Arrays;
4041
import java.util.Objects;
4142
import java.util.Optional;
4243
import javax.swing.Icon;
@@ -58,6 +59,8 @@
5859
*/
5960
public class AutoImportProjectOpenProcessor extends ProjectOpenProcessor {
6061

62+
public static final String[] WORKSPACE_MARKER_FILES = {"WORKSPACE", "WORKSPACE.bazel", "MODULE.bazel"};
63+
6164
public static final String MANAGED_PROJECT_RELATIVE_PATH = "tools/intellij/.managed.bazelproject";
6265
public static final String PROJECT_VIEW_FROM_ENV = "INTELLIJ_BAZEL_PROJECT_VIEW_TEMPLATE";
6366

@@ -97,9 +100,7 @@ public boolean canOpenProject(@NotNull VirtualFile virtualFile) {
97100
}
98101

99102
private boolean isBazelWorkspace(VirtualFile virtualFile) {
100-
return virtualFile.findChild("WORKSPACE") != null
101-
|| virtualFile.findChild("WORKSPACE.bazel") != null
102-
|| virtualFile.findChild("MODULE.bazel") != null;
103+
return Arrays.stream(WORKSPACE_MARKER_FILES).anyMatch((file) -> virtualFile.findChild(file) != null);
103104
}
104105

105106
@Override

clwb/src/com/google/idea/blaze/clwb/CLionNotificationProvider.kt

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.google.idea.blaze.clwb
1717

1818
import com.google.idea.blaze.base.lang.buildfile.language.BuildFileType
19+
import com.google.idea.blaze.base.project.AutoImportProjectOpenProcessor
1920
import com.google.idea.blaze.base.settings.Blaze
2021
import com.google.idea.blaze.base.sync.data.BlazeDataStorage
2122
import com.google.idea.blaze.base.wizard2.BazelImportCurrentProjectAction
@@ -27,8 +28,13 @@ import com.intellij.openapi.vfs.VirtualFile
2728
import com.intellij.util.concurrency.annotations.RequiresReadLock
2829
import com.jetbrains.cidr.lang.daemon.OCFileScopeProvider.Companion.getProjectSourceLocationKind
2930
import com.google.idea.sdkcompat.clion.projectStatus.*
31+
import com.intellij.util.concurrency.annotations.RequiresReadLockAbsence
3032
import com.jetbrains.cidr.project.workspace.CidrWorkspace
33+
import kotlinx.coroutines.Dispatchers
34+
import kotlinx.coroutines.withContext
3135
import java.io.File
36+
import java.nio.file.Files
37+
import java.nio.file.Path
3238

3339
/// This function is a little overloaded, ensures the project could be imported
3440
// and returns the potential workspace root.
@@ -38,7 +44,7 @@ private fun guessWorkspaceRoot(project: Project, file: VirtualFile?): String? {
3844
return null
3945
}
4046

41-
if (!Blaze.isBlazeProject(project)) {
47+
if (Blaze.isBlazeProject(project)) {
4248
return null
4349
}
4450

@@ -56,13 +62,32 @@ private fun guessWorkspaceRoot(project: Project, file: VirtualFile?): String? {
5662
return workspaceRoot.removeSuffix(BlazeDataStorage.PROJECT_DATA_SUBDIRECTORY)
5763
}
5864

59-
@RequiresReadLock
60-
private fun guessWidgetStatus(project: Project, currentFile: VirtualFile?): WidgetStatus? {
65+
@RequiresReadLockAbsence
66+
private fun validateWorkspaceRoot(workspaceRoot: String): Boolean {
67+
return AutoImportProjectOpenProcessor.WORKSPACE_MARKER_FILES.any {
68+
Files.exists(Path.of(workspaceRoot, it))
69+
}
70+
}
71+
72+
@RequiresReadLockAbsence
73+
private suspend fun guessWidgetStatus(project: Project, currentFile: VirtualFile?): WidgetStatus? {
6174
if (Blaze.isBlazeProject(project)) {
6275
return DefaultWidgetStatus(Status.OK, Scope.Project, "Bazel project is configured")
6376
}
6477

65-
if (currentFile == null || guessWorkspaceRoot(project, currentFile) == null) {
78+
if (currentFile == null) {
79+
return null
80+
}
81+
82+
val workspaceRoot = readAction {
83+
guessWorkspaceRoot(project, currentFile)
84+
}
85+
86+
if (workspaceRoot == null) {
87+
return null
88+
}
89+
90+
if (withContext(Dispatchers.IO) { !validateWorkspaceRoot(workspaceRoot) }) {
6691
return null
6792
}
6893

@@ -86,13 +111,13 @@ class BazelProjectFixesProvider : ProjectFixesProvider {
86111
class BazelWidgetStatusProvider : WidgetStatusProvider {
87112

88113
override suspend fun computeWidgetStatus(project: Project, currentFile: VirtualFile?): WidgetStatus? {
89-
return readAction { guessWidgetStatus(project, currentFile) }
114+
return guessWidgetStatus(project, currentFile)
90115
}
91116
}
92117

93118
class BazelEditorNotificationProvider : EditorNotificationWarningProvider {
94119

95120
override suspend fun computeProjectNotification(project: Project, file: VirtualFile): ProjectNotification? {
96-
return convertStatus(readAction { guessWidgetStatus(project, file) })
121+
return convertStatus(guessWidgetStatus(project, file))
97122
}
98123
}

0 commit comments

Comments
 (0)