Skip to content

Commit b7f5c75

Browse files
authored
Support MODULE.bazel and REPO.bazel (#503)
As of Bazel 6.3.0, MODULE.bazel and REPO.bazel are also considered workspace boundaries. Bazelisk now searches for these in the same order Bazel does.
1 parent 89e6693 commit b7f5c75

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

bazelisk.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ def decide_which_bazel_version_to_use():
9595
def find_workspace_root(root=None):
9696
if root is None:
9797
root = os.getcwd()
98-
if os.path.exists(os.path.join(root, "WORKSPACE")):
99-
return root
100-
if os.path.exists(os.path.join(root, "WORKSPACE.bazel")):
101-
return root
98+
for boundary in ["MODULE.bazel", "REPO.bazel", "WORKSPACE.bazel", "WORKSPACE"]:
99+
path = os.path.join(root, boundary)
100+
if os.path.exists(path) and not os.path.isdir(path):
101+
return root
102102
new_root = os.path.dirname(root)
103103
return find_workspace_root(new_root) if new_root != root else None
104104

ws/ws.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@ import (
77

88
// FindWorkspaceRoot returns the root directory of the Bazel workspace in which the passed root exists, if any.
99
func FindWorkspaceRoot(root string) string {
10-
if isValidWorkspace(filepath.Join(root, "WORKSPACE")) {
11-
return root
12-
}
13-
14-
if isValidWorkspace(filepath.Join(root, "WORKSPACE.bazel")) {
15-
return root
10+
for _, boundary := range [...]string{"MODULE.bazel", "REPO.bazel", "WORKSPACE.bazel", "WORKSPACE"} {
11+
if isValidWorkspace(filepath.Join(root, boundary)) {
12+
return root
13+
}
1614
}
1715

1816
parentDirectory := filepath.Dir(root)
@@ -23,9 +21,9 @@ func FindWorkspaceRoot(root string) string {
2321
return FindWorkspaceRoot(parentDirectory)
2422
}
2523

26-
// isValidWorkspace returns true iff the supplied path is the workspace root, defined by the presence of
27-
// a file named WORKSPACE or WORKSPACE.bazel
28-
// see https://github.com/bazelbuild/bazel/blob/8346ea4cfdd9fbd170d51a528fee26f912dad2d5/src/main/cpp/workspace_layout.cc#L37
24+
// isValidWorkspace returns true if the supplied path is the workspace root, defined by the presence of
25+
// a file named MODULE.bazel, REPO.bazel, WORKSPACE.bazel, or WORKSPACE
26+
// see https://github.com/bazelbuild/bazel/blob/6.3.0/src/main/cpp/workspace_layout.cc#L34
2927
func isValidWorkspace(path string) bool {
3028
info, err := os.Stat(path)
3129
if err != nil {

0 commit comments

Comments
 (0)