buildbuddy-io / buildbuddy

BuildBuddy is an open source Bazel build event viewer, result store, remote cache, and remote build execution platform.
https://buildbuddy.io
Other
598 stars 92 forks source link

[CLI] `Gathering metadata for bazel version...` breaks with some `tools/bazel` wrappers #6466

Open brentleyjones opened 6 months ago

brentleyjones commented 6 months ago

Describe the bug

The Gathering metadata for bazel version... step calls bazel --ignore_all_rc_files help version via tools/bazel, which can fail depending on what tools/bazel does.

To Reproduce

Have a tools/bazel wrapper that adds --config flags to commands. Then run bb in such a way that it needs to collect metadata, e.g. by running a new command like bb version. The tools/bazel wrapper will end up calling something like this:

/Users/brentley.jones/Library/Caches/bazelisk/downloads/sha256/dae351f491ead382bfc7c14d8957b9c8d735300c566c2161e34035eab994c1f2/bin/bazel --ignore_all_rc_files help --config=buildbuddy_cache version

Which will fail because of --ignore_all_rc_files.

Expected behavior

This step probably shouldn't call into the wrapper.

sluongng commented 6 months ago

Currently, I think the order of execution is like this

bb
 -> bazelisk
  -> tools/bazel

In bb CLI layer, we manually parse all the provided user flags and rc files, then translate them into bazelisk call. This lets us fiddle with the canonical flag of the invocation and inject our flags into it, as well as passing the canonical flags to our Plugin systems.

If you are injecting --config= in tools/bazel, this will be un-noticed by bb CLI, which could lead to unexpected behaviors.

With that said, I wonder if we could add a config flag somewhere that would let you opt-in to disabling --ignore_all_rc_files 🤔 . cc: @bduffany

sluongng commented 6 months ago

@brentleyjones Is it possible for you to implement your feature as a pre_bazel bb plugin? https://www.buildbuddy.io/docs/cli-plugins#pre_bazelsh

Then inside tools/bazel, you could detect whether BB CLI is being used to optionally add/skip the --config= flag.

brentleyjones commented 6 months ago

Maybe. But I'm classifying this as a bug because it prevents the CLI from being out of the box with tools/bazel that already exist and work with bazelisk.

bduffany commented 5 months ago

@brentleyjones Are you planning to use the .bazelversion trick for bb distribution or will you run bb directly? I think that might also factor into how we solve this.

Tentatively, I have a proposed fix for this here: https://github.com/buildbuddy-io/buildbuddy/compare/master...cli-toolsbazel

The patch makes sure that we call tools/bazel before canonicalizing / expanding flags, rather than after. However, there are some pros/cons to weigh:

Pros:

Cons:

brentleyjones commented 5 months ago

Are you planning to use the .bazelversion trick for bb distribution or will you run bb directly

We would use .bazelversion. Also, I think that is the main way people will use the CLI in the future, as it doesn't require additional infrastructure to get going.

  • Passing --config flags in tools/bazel might be an anti-pattern to begin with - it seems the bazelisk documentation for tools/bazel mentions that it can be useful for setting env vars but doesn't say anything about flags. I would think it would be tricky for a tools/bazel script to modify bazel flags, because they'd have to be aware of the schema for each bazel command.

Sadly lots of wrappers do this, so the ship has sailed there.