bazel: Change of --output_base causes subsequent builds to fail

Description of the problem / feature request:

Change in --output_base parameter causes the build to break with very obscure error messages.

Feature requests: what underlying problem are you trying to solve with this feature?

When build is done using some IDEs (VSCode or IntelliJ) they tend to set their own --output_base, different from the one configured by the user for the command line builds. This should not cause any problems, but unfortunately it appears that after --output_base changes the build is broken.

Bugs: what’s the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

Select any simplest Bazel project. Build it with bazel --output_base=C:/blah1 build ... so it builds successfully. Then try to build it with bazel --output_base=C:/blah2 build ... this time it breaks with the error messages which don’t make any sense.

What operating system are you running Bazel on?

The problem does not seem to be OS dependent.

What’s the output of bazel info release?

2.0.0

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

I figured that the problem is must probably caused by the stale “courtesy” symlink in WORKSPACE folder. After the first build Bazel creates “courtesy” symlink such as bazel-<workspace_name> in the workspace folder and it points inside output_base. When we issue second build command with the different output_base Bazel is smart enough to realize that and spawn second build server process, but unfortunately that stale bazel-<workspace_name> symlink still stays and points inside the old output_base which seems to confuse Bazel. Running bazel clean between builds or simply deleting of that symlink fixes the problem. It looks like when Bazel discovers the change in startup parameters which warrants spawning new build server it should at the same time remove existing courtesy symlinks as they are not valid anymore and cause the build to fail.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 28
  • Comments: 18 (12 by maintainers)

Commits related to this issue

Most upvoted comments

The basic problem here is that the bazel-$WORKSPACE convenience symlink is blindly traversed by Bazel: you can even do bazel build //bazel-$WORKSPACE/... and it will load the labels without any issues, and even build the targets there if you’re lucky. When you switch output bases, the symlinks are broken, but the fundamental issue is visiting that convenience symlink in the first place.

My suggestion for a workaround for now is to do echo "bazel-$WORKSPACE" >> .bazelignore. The .bazelignore file in the root of the workspace tells Bazel not to consider those directories. Of course, Bazel should be smart enough to not consider them on its own.

cc @mhy1992

For those who are having this failure with the VSCode Bazel Extension with an error along the lines of:

Command failed: bazel --output_base=/var/folders/z4/tjbsqpbj5pz3_mh9jzb4vhxw0000gn/T/5dc9f0e710b2578b352c533d7f70060e query ...:* --output=package
Loading: 0 packages loaded
ERROR: error loading package '': at /Users/--------/go/src/gitlab.com/-----/-----/build/go/-----.bzl:3:6: Every .bzl file must have a corresponding package, but '@bazel_gazelle//:deps.bzl' does not have one. Please create a BUILD file in the same or any parent directory. Note that this BUILD file does not need to do anything except exist.
Loading: 0 packages loaded
Loading: 0 packages loaded

For me, this problem was rectified by opening the VSCode Bazel extension settings, finding the “Queries Share Server” checkbox, and ensuring it is ticked (unticked by default).

The box has the description: “Use the same Bazel server for queries and builds. By default, vscode-bazel uses a separate server for queries so that they can be executed in parallel with builds. You can enable this setting if running multiple Bazel servers has a negative performance impact on your system, but you may experience degraded performance in Visual Studio Code for operations that require queries.”

I think the root cause is probably related to this issue, because (I think) to get the separate server the --output_base argument is provided, which then runs into the issue you describe here.

Is anyone actively working on this? This is a pretty annoying bug that affects how e.g. Jenkins build nodes have to be spawned because the cannot handle multiple executors being isolated with output_base. I’d be willing to investigate a fix but am not familiar enough with bazel’s codebase to even know where to start looking. Any pointers would be appreciated 😃

For those coming from the future: You’ll also get this sort of error if the workspace name in your WORKSPACE file differs from the enclosing directory name.

e.g. if you have foo/WORKSPACE and that file contains ‘workspace(name=“bar”)’ you will get weird and confusing errors.

I found a workaround, but I don’t know why it works:

  • Let $WORKSPACE=app
  • Remove the existing symlink bazel-app
  • Replace with a dummy file echo "dummy" >| bazel-app
  • The builds no longer fail, but you get a warning: failed to create one or more convenience symlinks for prefix 'bazel-'

I tried setting --symlink_prefix, --experimental_use_sandboxfs, but it did not work. It’s as if ‘bazel-’ is hardcoded somewhere.

I hope that helps.