Skip to content

bazel 8 + bzlmod and child modules resolution #472

@mgenov

Description

@mgenov

VS Code Bazel extension does not respect root toolchains / .bazelrc in multi-module bzlmod monorepos

Problem description

When using Bazel with bzlmod in a monorepo, each app or library is its own Bazel module (apps/appX/MODULE.bazel, modules/shared/MODULE.bazel, etc.).
The monorepo root also has a MODULE.bazel where all toolchains and the main .bazelrc are centralized.

Expected behavior

  • The VS Code Bazel extension should treat the monorepo root as the single Bazel workspace root.
  • All build/test/run actions (CodeLens, Test UI, right-click commands) should run Bazel from the repo root, so that:
    • the root .bazelrc is applied,
    • toolchains registered in the root MODULE.bazel are available,
    • cross-module deps work (e.g. @other_module//path:target),
    • labels are consistent (//modules/transport/...:foo) rather than relative to a leaf folder.

Actual behavior

  • The extension currently picks the nearest MODULE.bazel or WORKSPACE file to the open file and treats that as the workspace root.
  • If I open apps/app-go/src/main.go and run “Bazel: Build Target”, the extension runs Bazel in apps/app-go/.
    • Labels resolve relative to that folder (e.g. //proto/... instead of //modules/transport/proto/...).
    • Toolchains defined in the root MODULE.bazel are not registered.
    • The root .bazelrc is ignored.
  • This makes cross-module builds/tests fail when triggered from VS Code, even though they succeed when run manually from the monorepo root.

Why this matters

  • With bzlmod, the recommended monorepo structure is multiple modules with a root module that wires toolchains and overrides.
  • IDE users (VS Code) expect Bazel commands to “just work” without having to cd into the root manually.
  • As of today, developers must either:
    • open only the repo root in VS Code (which breaks per-app workspace setups), or
    • duplicate toolchain registration and .bazelrc in every leaf module (which defeats the purpose of centralization).

Steps to reproduce

  1. Create a monorepo with:
    MODULE.bazel # root, toolchains + local_path_override
    apps/app-go/MODULE.bazel # leaf module
  2. Open apps/app-go/ in VS Code.
  3. Run “Bazel: Build Target” on a file.
  4. Observe: Bazel runs from apps/app-go/ instead of the monorepo root, toolchains are missing, and labels resolve incorrectly.

Desired solution

  • Provide a VS Code setting to force Bazel commands to run from the top-level workspace root (not the nearest).
  • Example: "bazel.workspaceRootMode": "repo-root-only".
  • Or auto-detect the “primary” root (the one containing .git/ and MODULE.bazel) instead of treating every nested module as independent.
  • At minimum, document that opening a leaf module as the VS Code workspace will break root toolchains/config.

Environment

  • Bazel: 8.x with bzlmod enabled
  • VS Code: latest
  • Extension: vscode-bazel latest
  • OS: Any

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions