bazelbuild / bazelbuild/bazel

Loading top-level targets in local_path_override modules in child directory breaks the build

Open
#22,208 11 comments 6 reactions 0 assignees View on GitHub
area-Bzlmod P2 team-ExternalDeps type: bug
Dominant language
Java
Stars
25.8k
Forks
4.6k
Avg merge
2d 20h
Merged PRs (30d)
72

Description

### Description of the bug:

Using a Bazel module within a child directory (i.e., in the same repository, via `local_path_override()`) that calls `load()` on a top-level target breaks the build. The equivalent build using `local_repository()` in a `WORKSPACE` file succeeds.

In my [mbland/bzlmod-local-module-bug](https://github.com/mbland/bzlmod-local-module-bug) example repo:

```
$ bazel build --nobuild --enable_bzlmod=false //...

INFO: Analyzed 0 targets (1 packages loaded, 0 targets configured).
INFO: Found 0 targets...
INFO: Elapsed time: 0.099s, Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully, 0 total actions

$ bazel build --nobuild --enable_bzlmod=true //...

WARNING: Target pattern parsing failed.
ERROR: Skipping '//...': error loading package under directory '': error loading package 'third_party/vendored_module': cannot load '//:top_level_target.bzl': no such file
ERROR: error loading package under directory '': error loading package 'third_party/vendored_module': cannot load '//:top_level_target.bzl': no such file
INFO: Elapsed time: 0.054s
INFO: 0 processes.
ERROR: Build did NOT complete successfully
```

`local_path_override()` and the bzlmod mechanism seem to assume the module will not reside within the same repo. The [WORKSPACE or MODULE.bazel repository boundary marker files](https://bazel.build/concepts/build-ref#repositories) don't appear to have an effect.

Please see the README.md in my example repo for more information.

### Which category does this issue belong to?

Core

### What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

```
git clone https://github.com/mbland/bzlmod-local-module-bug.git
cd bzlmod-local-module-bug
bazel build --nobuild --enable_bzlmod=true //...
```

### Which operating system are you running Bazel on?

macOS 14.4.1 (23E224)

### What is the output of `bazel info release`?

release 8.0.0-pre.20240415.1

### If `bazel info release` returns `development version` or `(@non-git)`, tell us how you built Bazel.

_No response_

### What's the output of `git remote get-url origin; git rev-parse HEAD` ?

```text
git@github.com:mbland/bzlmod-local-module-bug.git
31f1f966f4f2f1ef1c27cfe8ddf77a102fccdfa0
```

### Is this a regression? If yes, please try to identify the Bazel commit where the bug was introduced.

No. This happens in the current 7.1.1 release, as well as 7.0.0, 6.5.0, and 6.0.0.

### Have you found anything relevant by searching the web?

No. All `local_path_override()` examples and issues I found presume the local module is _outside_ the current directory.

### Any other information, logs, or outputs that you want to share?

_No response_

Contributor guide

Open the contributing guide

Research direction

Clone the linked bzlmod-local-module-bug repository, read its README.md, and reproduce the failure with `bazel build --nobuild --enable_bzlmod=true //...`. Compare the nested module's MODULE.bazel and WORKSPACE boundary setup with the successful non-bzlmod build; done means the local_path_override case builds successfully without regressing the WORKSPACE behavior.

Written by the indexing model from the issue text.

Assessment

Domain
build-system
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.