Skip to content

Commit

Permalink
feat: detect bzlmod files marking workspace directory
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenandpork committed Jan 9, 2025
1 parent 778a4da commit ccfc866
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
20 changes: 10 additions & 10 deletions internal/ibazel/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,24 @@ func (m *MainWorkspace) FindWorkspace() (string, error) {
}

volume := filepath.VolumeName(path)
sentinel_filenames := []string{"WORKSPACE.bzlmod", "WORKSPACE.bazel", "MODULE.bazel", "WORKSPACE"} // search order

for {
// filepath.Dir() includes a trailing separator if we're at the root
if path == volume+string(filepath.Separator) {
path = volume
}

// Check if we're at the workspace path
if s, err := os.Stat(filepath.Join(path, "WORKSPACE")); err == nil {
if !s.IsDir() && s.Name() == "WORKSPACE" {
// In macOS directories called "workspace" will match because the file
// system isn't case sensitive.
return path, nil
}
}
for _, sentinel := range sentinel_filenames {
// Check if we're at the workspace path
if s, err := os.Stat(filepath.Join(path, sentinel)); err == nil {
// In macOS directories called "workspace" will match "WORKSPACE"
// because the file system isn't case sensitive

if _, err := os.Stat(filepath.Join(path, "WORKSPACE.bazel")); err == nil {
return path, nil
if !s.IsDir() && s.Name() == sentinel {
return path, nil
}
}
}

// If we've reached the root, then we know the cwd isn't within a workspace
Expand Down
53 changes: 53 additions & 0 deletions internal/ibazel/workspace/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ func TestAppleCaseInsensitivity(t *testing.T) {
files: []string{},
err: false,
},
"simple with bzlmod extension": {
startingWD: "",
wantPath: "",
dirs: []string{},
workspacePath: "/WORKSPACE.bzlmod",
files: []string{},
err: false,
},
"simple with MODULE.bazel": {
startingWD: "",
wantPath: "",
dirs: []string{},
workspacePath: "/MODULE.bazel",
files: []string{},
err: false,
},
"no workspace": {
startingWD: "c/d",
wantPath: "",
Expand All @@ -53,6 +69,43 @@ func TestAppleCaseInsensitivity(t *testing.T) {
files: []string{},
err: true,
},
"no workspace in workspace-named path": {
startingWD: "c/WORKSPACE",
wantPath: "/a",
dirs: []string{
"a/b",
"c/WORKSPACE",
},
workspacePath: "/a/WORKSPACE",
files: []string{},
err: true,
},
"no workspace in MODULE.bazel-named path": {
startingWD: "c/MODULE.bazel",
wantPath: "",
dirs: []string{
"a/b",
"c/MODULE.bazel",
},
workspacePath: "/a/WORKSPACE",
files: []string{},
err: true,
},
"workspace nested in workspace-named path": {
// this is intended to catch case-insensitive Macs but mimics the
// case-insensitive match with a case-sensitive match of the dir
// the "WORKSPACE" dirname should not early-quit the search, allowing
// us to find /c/MODULE.bazel
startingWD: "c/d/WORKSPACE",
wantPath: "/c",
dirs: []string{
"a/b",
"c/d/WORKSPACE",
},
workspacePath: "/c/MODULE.bazel",
files: []string{},
err: false,
},
"nested workspace": {
startingWD: "a/workspace",
wantPath: "",
Expand Down

0 comments on commit ccfc866

Please sign in to comment.