-
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
sh_binary has multiple outputs in some contexts #11820
Comments
I've also noticed this. With a
With With
With
With
and an empty
with no problems, but with
I get an error from bazel due to I verified that this behavior persists in bazel 3.7.0rc1 on Ubuntu 18.04. |
Even simpler: if we list the sh_binary label in the
|
…h expansion Gives a tool built using `nodejs_binary` or `sh_binary`, the tool cannot be used as binary in an integration test command currently because: * Bazel location expansion does not respect the executable "FilesToRun" information of the binaries, and rather sees _all_ outputs, erroring that `$(locations X)` need to be used instead. See e.g. bazelbuild/bazel#11820) * The command is currently split by space and this decouples the Make funtion expression incorrectly into a broken expansion. e.g. `$(rootpath X)` will become `["$(rootpath", "X"]`). This commit fixes both things by relying on the Bazel `CommandHelper.java` class which handles the expansion of commands as expected, with similar semantics of a `genrule` as per `GenRuleBase.java`. This allows test authors to conveniently use a `nodejs_binary` or `sh_binary` as command binary because the `CommandHelper` knows exactly which targets provide executables or not. There is no good API for the plain Make expansion of a command itself, so we use a little trick (which is documented sufficiently in the code).
…h expansion Gives a tool built using `nodejs_binary` or `sh_binary`, the tool cannot be used as binary in an integration test command currently because: * Bazel location expansion does not respect the executable "FilesToRun" information of the binaries, and rather sees _all_ outputs, erroring that `$(locations X)` need to be used instead. See e.g. bazelbuild/bazel#11820) * The command is currently split by space and this decouples the Make funtion expression incorrectly into a broken expansion. e.g. `$(rootpath X)` will become `["$(rootpath", "X"]`). This commit fixes both things by relying on the Bazel `CommandHelper.java` class which handles the expansion of commands as expected, with similar semantics of a `genrule` as per `GenRuleBase.java`. This allows test authors to conveniently use a `nodejs_binary` or `sh_binary` as command binary because the `CommandHelper` knows exactly which targets provide executables or not. There is no good API for the plain Make expansion of a command itself, so we use a little trick (which is documented sufficiently in the code).
…h expansion (#285) * feat(bazel): allow integration commands to resolve executables through expansion Gives a tool built using `nodejs_binary` or `sh_binary`, the tool cannot be used as binary in an integration test command currently because: * Bazel location expansion does not respect the executable "FilesToRun" information of the binaries, and rather sees _all_ outputs, erroring that `$(locations X)` need to be used instead. See e.g. bazelbuild/bazel#11820) * The command is currently split by space and this decouples the Make funtion expression incorrectly into a broken expansion. e.g. `$(rootpath X)` will become `["$(rootpath", "X"]`). This commit fixes both things by relying on the Bazel `CommandHelper.java` class which handles the expansion of commands as expected, with similar semantics of a `genrule` as per `GenRuleBase.java`. This allows test authors to conveniently use a `nodejs_binary` or `sh_binary` as command binary because the `CommandHelper` knows exactly which targets provide executables or not. There is no good API for the plain Make expansion of a command itself, so we use a little trick (which is documented sufficiently in the code). * fixup! feat(bazel): allow integration commands to resolve executables through expansion Address feedback
This issue still exists in Bazel 5.3.1, is there a good workaround for it? |
Or more generally: there needs to be a way to go from a |
+1 I'm also facing issues due to this |
Currently, we provide single-file tools such as `ctags`, `gsutil` etc via an `sh_binary` wrapper, to have a single target to reference that automatically does platform selection of the underlying tool. Due to some [unfortunate reason](bazelbuild/bazel#11820), the underlying srcs (which is [a single file](https://bazel.build/reference/be/shell#sh_binary.srcs)) of an `sh_binary` are also exposed as outputs (rather than just as typical runfiles) alongside the script that wraps. This is _sometimes_ problematic when doing location expansion (e.g. `$(location ...)`) due to these only allowing a single output (dont ask why this works in some contexts but not others, I dont know). To address this, we create a wrapper macro + rule to replicate what we want from `sh_binary` (automatic platform selection + tool naming), while only exposing a singular file. See example of currently required approach to consuming a tool: [BUILD.bazel](https://github.com/sourcegraph/sourcegraph/pull/62801/files#diff-e2a562c2e13908933b2ee24f0ac596829b38a5325cc69a4aee05c383aaa2e494R8) & [main_test.go](https://github.com/sourcegraph/sourcegraph/pull/62801/files#diff-7a91cb5143064bfc8993ef97baf68b718ef49747ccc1d3c5e1150d4696b88305R66). With this change, `rlocationpath` (singular) can be used instead (or any of the other singular nouns in different contexts), as well as no `strings.Split/strings.Fields` being required ## Test plan `bazel cquery --output=files //dev/tools:dropdb` yields 1 vs 2 files. Also updated the rule behind `//internal/database:generate_schemas` due to the workaround in it for the fact that the underlying srcs was also exposed. The correctness is verified by running said target (locally + CI)
+1 also bitten by this |
I think that this may not be an issue with CC @comius |
Looks like this issue was fixed before https://github.com/bazelbuild/bazel/pull/16381/files/d8176177c95ff90c471db2c46085dfbc93fca4c1 and got reverted silently to "fix breakage in an internal use case" which sounds like the opposite of what should have happened. ab71a10 Something this trivial should not be reverted or at least be communicated clearly. Cross-Ref: #20038 |
I'd like to fix this issue forever and have it not reverted due to "fix breakage in an internal use case". What needs to be done here? |
@thesayyn A safe way to fix this would be to resubmit the reverted PR and have it use the |
Oh great, Thank you! I'll put up a PR for this. |
Work towards #11820 Fixes #20038 Fixes #23200 Fixes #24613 RELNOTES: Extra targets provided to `ctx.expand_location` now expand to their executable (if any) instead of resulting in an error if they provide a number of files different from one. RELNOTES[INC]: The `--incompatible_locations_prefers_executable` flag has been added and enabled, which makes it so that `ctx.expand_location` expands `$(locations :x)` to the executable of an extra target `:x` if it provides one and the number of files provided by it is not one. Closes #24690. PiperOrigin-RevId: 713453768 Change-Id: I0d6e052bc70deea029554ab722feb544f9597a23
Work towards bazelbuild#11820 Fixes bazelbuild#20038 Fixes bazelbuild#23200 Fixes bazelbuild#24613 RELNOTES: Extra targets provided to `ctx.expand_location` now expand to their executable (if any) instead of resulting in an error if they provide a number of files different from one. RELNOTES[INC]: The `--incompatible_locations_prefers_executable` flag has been added and enabled, which makes it so that `ctx.expand_location` expands `$(locations :x)` to the executable of an extra target `:x` if it provides one and the number of files provided by it is not one. Closes bazelbuild#24690. PiperOrigin-RevId: 713453768 Change-Id: I0d6e052bc70deea029554ab722feb544f9597a23 (cherry picked from commit 457d248)
…24874) Work towards #11820 Fixes #20038 Fixes #23200 Fixes #24613 RELNOTES: Extra targets provided to `ctx.expand_location` now expand to their executable (if any) instead of resulting in an error if they provide a number of files different from one. RELNOTES[INC]: The `--incompatible_locations_prefers_executable` flag has been added and enabled, which makes it so that `ctx.expand_location` expands `$(locations :x)` to the executable of an extra target `:x` if it provides one and the number of files provided by it is not one. Closes #24690. PiperOrigin-RevId: 713453768 Change-Id: I0d6e052bc70deea029554ab722feb544f9597a23 (cherry picked from commit 457d248) Fixes #24646
Description of the problem:
A
sh_binary
target that wraps a shell script has multiple outputs in some contexts. This means that ash_binary
cannot be used (at least not straight-forwardly) in some contexts. E.g. it cannot be used insrcs
of anothersh_binary
, or$(rootpath )
cannot be applied to it in some contexts (instead$(rootpaths )
is required).Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
A
sh_binary
cannot besrcs
to anothersh_binary
(A use-case would be a
sh_test
wrapping ash_binary
and extending the runfiles tree with additionaldata
attributes.)A
sh_binary
cannot be used with$(rootpath )
(A use-case would be generating a script file that calls another tool at runtime.)
Instead one has to use the plural form
$(execroots )
.(On Linux this happens to place the symlink
repro/script
first which makes it directly executable. However, on Windows it places the.exe
second, meaning that additional logic is required to find the.exe
from the result of$(rootpaths )
.)However, one can use the singular
$(execroot )
in the context ofsh_test
:Where
test.sh
looks as follows:What operating system are you running Bazel on?
Ubuntu 19.10
Windows 10
What's the output of
bazel info release
?Linux: release 3.3.1- (@non-git)
Windows: release 3.3.1-patched-1dac3221f72f5d22a0b79f0531af1f63
If
bazel info release
returns "development version" or "(@non-git)", tell us how you built Bazel.On Linux, built using nixpkgs revision 1d8018068278a717771e9ec4054dff1ebd3252b0
On Windows, built with the following patch, which should be irrelevant to the issue.
Have you found anything relevant by searching the web?
No
Any other information, logs, or outputs that you want to share?
We encountered this issue when trying to work around the removal of
--noincompatible_windows_native_test_wrapper
on Windows. See digital-asset/daml@2248fcd and digital-asset/daml@23f4a59. We tried to replace instances of custom test rules that wrote an executable.sh
file as the test executable, by a more genericsh_inline_test
macro that combines a custom rule that generates a script file with ash_test
. With this change we had to replace instances ofctx.executable.some_tool
by$(rootpath :some_tool)
, which didn't work as described above. On Windows we have to be careful to find the.exe
wrapper because the tool is invoked indirectly in a way that doesn't support shell scripts but only Windows executables.The following test target illustrates the issue
With the following building blocks:
sh.bzl
:sh.tpl
:runner.c
:Full example: digital-asset/daml@a357bb4
The text was updated successfully, but these errors were encountered: