Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: detect bzlmod files identifying workspace directory #697

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/e2e/ibazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

// Maximum amount of time to wait before failing a test for not matching your expectations.
const (
defaultDelay = 20 * time.Second
defaultDelay = 40 * time.Second
)

type IBazelTester struct {
Expand Down
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: filepath.FromSlash("/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: filepath.FromSlash("/c"),
dirs: []string{
"a/b",
"c/d/WORKSPACE",
},
workspacePath: "/c/MODULE.bazel",
files: []string{},
err: false,
},
"nested workspace": {
startingWD: "a/workspace",
wantPath: "",
Expand Down