Skip to content

Feat/manual workspace path#625

Open
Zsi-r wants to merge 2 commits into
bazel-contrib:masterfrom
Zsi-r:feat/manual-workspace-path
Open

Feat/manual workspace path#625
Zsi-r wants to merge 2 commits into
bazel-contrib:masterfrom
Zsi-r:feat/manual-workspace-path

Conversation

@Zsi-r

@Zsi-r Zsi-r commented Apr 26, 2026

Copy link
Copy Markdown

Summary

Add a new configuration option bazel.workspacePath that allows users to manually specify the Bazel workspace directory path.

Problem

Fixes #621
Fixes #472

When working with multiple MODULE.bazel files in nested directories (e.g., monorepo setups with local_path_override), the extension picks the nearest workspace file instead of the intended root workspace. This causes queries to fail because they run in the wrong directory.

This is especially useful when opening a subdirectory of the Bazel workspace in VS Code, where the root MODULE.bazel/WORKSPACE file is located in a parent directory outside the opened folder.

Related: #428

Solution

Added a new configuration option bazel.workspacePath that:

  • Accepts an absolute path or a path relative to the VS Code workspace folder
  • Takes precedence over auto-detection when set
  • Falls back to the existing auto-detection behavior when not configured

Usage

{
  "bazel.workspacePath": "/absolute/path/to/workspace"
}

Zsi-r added 2 commits April 26, 2026 09:49
Add a new configuration option `bazel.workspacePath` that allows users
to manually specify the Bazel workspace directory path. This is useful
when:

- Working with multiple MODULE.bazel files in nested directories
- The auto-detection picks the wrong workspace root
- Using local_path_override with multiple repositories

The path can be absolute or relative to the VS Code workspace folder.

Fixes bazel-contrib#621
…onfig

Add tests to verify:
- Auto-detection works when workspacePath is not configured
- Configured absolute path is used correctly
- Configured relative path is resolved correctly
- Invalid configured path falls back to auto-detection
- Configured path takes precedence over nested MODULE.bazel
@cbandera

cbandera commented May 3, 2026

Copy link
Copy Markdown
Collaborator

We recently merged #608. Might this maybe already be enough to fix your problem? It at least should enable you to open a subdirectory of a Bazel workspace and have the queries run from the root of it.

As I am generally against adding to many config options I would like to understand your usecase better. What is the motivation for opening just a subdirectory of a workspace instead of the true root?

@benjamin-maier96

benjamin-maier96 commented May 5, 2026

Copy link
Copy Markdown
Contributor

We recently merged #608. Might this maybe already be enough to fix your problem? It at least should enable you to open a subdirectory of a Bazel workspace and have the queries run from the root of it.

I don't think it helps for the linked issues #621 and #472. The problem here is that we have multiple MODULE.bazel files and we run all queries from the nearest MODULE.bazel file. But there exists project setups, where only the root MODULE.bazel file contains all the resolved paths and therefore every query must be run from the root folder.

The PR you mentioned is only able to find the MODULE.bzl file if we have only a subfolder open.

@cameron-martin

Copy link
Copy Markdown
Collaborator

I do think this is a reasonable request, although I would be inclined to make it an array, like the linkedProjects config in rust-analyzer, and resolve the closest parent directory in this array from the current file.

There was a previous discussion on how to automatically fix the situation you're encountering in a slack thread a while ago, but doing this correctly is tricky so a config option sounds better here.

@benjamin-maier96

benjamin-maier96 commented May 6, 2026

Copy link
Copy Markdown
Contributor

I tested the changes on my windows machine and it picks up the correct path.

However, currently not all queries are executed from bazel root folder. For example the go to definition query uses the current document as working directory.

const workspaceInfo = BazelWorkspaceInfo.fromDocument(document);
if (workspaceInfo === undefined) {
// Not in a Bazel Workspace.
return null;
}
const range = document.getWordRangeAtPosition(position, LABEL_REGEX);
const targetText = document.getText(range);
const location = await targetToUri(targetText, Utils.dirname(document.uri));
return location
? [
{
originSelectionRange: range,
targetUri: Uri.file(location.path).with({
fragment: `${location.line}:${location.column}`,
}),
targetRange: location.range,
},
]
: null;

I assume this is simply a bug and we should use workspaceInfo.bazelWorksapcePath as the working directory. With this change my issue #621 would be resolved. Besided that we might have more code lines where we would need to use the correct working directory.

@benjamin-maier96

Copy link
Copy Markdown
Contributor

Any updates on this one?

@benjamin-maier96

Copy link
Copy Markdown
Contributor

@cbandera @cameron-martin
What is the current state of this pull-request? If we can agree on the extra parameter I would be willing to finish the necessary changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getBazelWorkspaceFolder for multiple MODULE.bazel files bazel 8 + bzlmod and child modules resolution

4 participants