-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Respect precedence of --extra_toolchains during toolchains resolution #19942
Conversation
This is an interesting idea. We faced something similar with Would that approach work for you? I'd also like to try and get a broader sense of how Bazel users are using I'm going to test-import this and see if it causes any problems with our internal version of Bazel: this may take a day or so to see results in any case. |
To be fair, I think the reason this never was an issue before is because people do not use --extra_toolchains on the command line, and seldom use it in bazelrc files. Because if it can be in your bazelrc, it can probably be in your WORKSPACE, unless you use it behind --config flags. Toolchains are complicated enough that you do not want the have to pass one on the command line. Also, toolchains should generally not compete for the same config. I have read somewhere that relying on registration order is a bad practice. And this issue only happens when you do rely on registration order between rc files and command line arguments. (if you use only command line arguments, you can fix it by changing the arguments order). The reason I hit this issue is because in nix packaging of bazel, the default prebuilt java toolchains do not work. To keep the expectation that bazel comes with built-in working java toolchains, we patch @bazel_tools to provide nonprebuilt java toolchains. And we hardcode a special system bazelrc that --extra_toolchains all these toolchains. (And I was unknowingly fighting #19934 at the same time). Now, we could register them all in a WORSPACE prefix/preloaded part, but that does not work with bzlmod. So, when I wanted to override the hardcoded system bazelrc with a different toolchain, I discovered that bazel ignores my command line arguments. A proper fix for nix packaging would be to provide nonprebuilt toolchains in rules_java, and better support for nix/nixos and other uncommon platforms where prebuilt binaries do not work. I am slowly moving towards that, but it is out of scope for this PR.
That would also work indeed. Not sure it is worth forcing users to scramble everything on one line though. Because where execution platforms add to a set, toolchains do override each other (when they have the same constraints). So to override a previously defined toolchain, just add a new one with higher precedence. With execution platforms there was no way to override a previously added exec platform. Which is ironically the problem I am trying to fix here. There is no way to override toolchains by adding more flags, because the order is wrong.
I think this patch fixes a bug, and that starting a whole discussion about it is a bit overkill. The discussion could still happen though, but it is orthogonal. If the discussion happens, it should help decide between the fixed version of accumulated flags, and a non-accumulating flag. Nobody will want the current, broken accumulating flag version. |
This is fairly convincing: thank you for explaining your reasoning. I would still like you to file an issue, because while I suspect you are correct that Bazel users do not use |
There you are #19945 |
See #19945 (comment), I think the better approach is to not accumulate values at all. I'm closing this PR and I'll work on that solution. Thanks for bringing this to my attention! |
I am re-viving this after being convinced it's a better approach |
…-wins. This only changes the precedence within all uses of `--extra_toolchains`, it does not change the precedence for `register_toolchains` calls. After this, the priority order for toolchains is: 1. Consider toolchains registered via `--extra_toolchains` 1. Within this set, the last mentioned toolchain has highest priority 2. Consider toolchains registered via `register_toolchains` 1. Within this set, the first mentioned toolchain has highest priority In all cases, pseudo-targets like `:all` and `/...` are ordered by Bazel's package loading mechanism, which is currently using a lexicographic ordering. Fixes bazelbuild#19945. Closes bazelbuild#19942. PiperOrigin-RevId: 578850060 Change-Id: Ibd9c32e5479a6a27717734852b875f9b3f31b510
@katre Should this be cherry-picked as an incompatible change? |
Into Bazel 7, you mean? I'm not sure: it's a fairly big change to the semantics of |
I do expect breakages to show up in complex end user project at a higher rate than in bazelbuild org projects. It will be breaking no matter which release we include it in, so I would say this is more of a question of how much "breaking changes budget" we have left for Bazel 7. @iancha1992 Do you happen to know whether the 7.0.0 release already has many breaking changes compared to previous releases? |
|
@iancha1992 Thanks for the notification.
We don't really have a clear defined breaking change budget. As long as it won't break too many downstream projects, I'm fine with getting it into Bazel 7.0.0. I'll leave the decision to @katre |
I'm fine with cherrypicking this if it's not too late. Do we know yet if there'll be an rc5? Or should it go into 7.1.0 at this point? |
7.1.0 must be fully backwards compatible with 7.0.0, so that's not an option. Let's sync with the gTech team. |
@bazel-io fork 7.0.0 |
…-wins. This only changes the precedence within all uses of `--extra_toolchains`, it does not change the precedence for `register_toolchains` calls. After this, the priority order for toolchains is: 1. Consider toolchains registered via `--extra_toolchains` 1. Within this set, the last mentioned toolchain has highest priority 2. Consider toolchains registered via `register_toolchains` 1. Within this set, the first mentioned toolchain has highest priority In all cases, pseudo-targets like `:all` and `/...` are ordered by Bazel's package loading mechanism, which is currently using a lexicographic ordering. Fixes #19945. Closes #19942. Closes #20031. PiperOrigin-RevId: 579845262 Change-Id: Ibd9c32e5479a6a27717734852b875f9b3f31b510
…of first-wins. (#20239) This only changes the precedence within all uses of `--extra_toolchains`, it does not change the precedence for `register_toolchains` calls. After this, the priority order for toolchains is: 1. Consider toolchains registered via `--extra_toolchains` 1. Within this set, the last mentioned toolchain has highest priority 2. Consider toolchains registered via `register_toolchains` 1. Within this set, the first mentioned toolchain has highest priority In all cases, pseudo-targets like `:all` and `/...` are ordered by Bazel's package loading mechanism, which is currently using a lexicographic ordering. Fixes #19945. Closes #19942. Closes #20031. PiperOrigin-RevId: 579845262 Change-Id: Ibd9c32e5479a6a27717734852b875f9b3f31b510 Co-authored-by: Guillaume Maudoux <[email protected]>
I have noticed that
--extra_toolchains
do not override each other as could be expected from the usual semantics of options (rc files, then command line flags, the last one always "wins").bazel/site/en/run/bazelrc.md
Lines 141 to 143 in 6c02329
In practice, it is impossible to override an
--extra_toolchain
from an rc file on the command line.While the toolchains are indeed accumulated as per the options semantics, the way toolchain selection works is that the first one wins. I had to reverse the options list to make the last defined one to be examined first during selection.
I have confirmed that the test fails without the patch, and that the patch fixes the new test.
Fixes #19945