Repository navigation
Default runfiles to upwards directory as fallback - #7774
Globegitter wants to merge 1 commit into
Conversation
This is to fix cases where one depends on a binary that has the bash runfiles as one of their dependencies.
|
Please provide a complete example so I can repro the problem. The current example doesn't work:
|
|
Crucially, The workspace name is either defined in the WORKSPACE file's |
|
Sorry that was my bad with the example. I will put up a full repository to reproduce ASAP. |
|
@laszlocsomor For a reproducton see: https://github.com/globegitter/rules_nodejs_sh_wrapper_repro and follow the steps described here: https://github.com/globegitter/rules_nodejs_sh_wrapper_repro#issue-1 I am using the |
|
Thanks, I can repro the problem. Interestingly, |
|
I found the culprit: it's indeed the working directory, set by We are bitten by Bazel merging runfiles. When binary (or test) "A" data-depends on binary "B", Bazel merges B's runfiles into A's and only generates a manifest/directory for "A" but not "B". To let "B" find its runfiles, "A" must discover and export the "RUNFILES_*" envvars first. This is common for tests (A) that run binaries (B) with data-dependencies. In that spirit, the top-level binary (what we $ cd ~/stuff/atlassian_bazel-tools
$ git rev-parse HEAD
986ed8f71dc910ac8766c920cef56e0e94db5bd0
$ git diff
diff --git a/multirun/def.bzl b/multirun/def.bzl
index edd8c6e..d711d97 100644
--- a/multirun/def.bzl
+++ b/multirun/def.bzl
@@ -4,10 +4,36 @@ _CONTENT_PREFIX = """#!/usr/bin/env bash
set -euo pipefail
+# --- begin runfiles.bash initialization ---
+# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
+set -euo pipefail
+if [[ ! -d "${RUNFILES_DIR:-/dev/null}" && ! -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
+ if [[ -f "$0.runfiles_manifest" ]]; then
+ export RUNFILES_MANIFEST_FILE="$0.runfiles_manifest"
+ elif [[ -f "$0.runfiles/MANIFEST" ]]; then
+ export RUNFILES_MANIFEST_FILE="$0.runfiles/MANIFEST"
+ elif [[ -f "$0.runfiles/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
+ export RUNFILES_DIR="$0.runfiles"
+ fi
+fi
+if [[ -f "${RUNFILES_DIR:-/dev/null}/bazel_tools/tools/bash/runfiles/runfiles.bash" ]]; then
+ source "${RUNFILES_DIR}/bazel_tools/tools/bash/runfiles/runfiles.bash"
+elif [[ -f "${RUNFILES_MANIFEST_FILE:-/dev/null}" ]]; then
+ source "$(grep -m1 "^bazel_tools/tools/bash/runfiles/runfiles.bash " \
+ "$RUNFILES_MANIFEST_FILE" | cut -d ' ' -f 2-)"
+else
+ echo >&2 "ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash"
+ exit 1
+fi
+# --- end runfiles.bash initialization ---
+
+# Export RUNFILES_* envvars (and a couple more) for subprocesses.
+runfiles_export_envvars
+
"""
def _multirun_impl(ctx):
- runfiles = ctx.runfiles()
+ runfiles = ctx.runfiles().merge(ctx.attr._bash_runfiles[DefaultInfo].default_runfiles)
content = [_CONTENT_PREFIX]
for command in ctx.attr.commands:
@@ -45,6 +71,9 @@ _multirun = rule(
doc = "Targets to run in specified order",
cfg = "target",
),
+ "_bash_runfiles": attr.label(
+ default = Label("@bazel_tools//tools/bash/runfiles"),
+ ),
},
executable = True,
)
@@ -59,7 +88,8 @@ def multirun(**kwargs):
)
def _command_impl(ctx):
- runfiles = ctx.runfiles()
+ runfiles = ctx.runfiles().merge(ctx.attr._bash_runfiles[DefaultInfo].default_runfiles)
+
defaultInfo = ctx.attr.command[DefaultInfo]
default_runfiles = defaultInfo.default_runfiles
@@ -118,6 +148,9 @@ _command = rule(
doc = "Target to run",
cfg = "target",
),
+ "_bash_runfiles": attr.label(
+ default = Label("@bazel_tools//tools/bash/runfiles"),
+ ),
},
executable = True,
)This fixes I was wrong earlier, the |
Fixes the bug in bazelbuild/bazel#7774
|
Great @laszlocsomor thanks for sending that over. Given the auto-merging that bazel does this PR should still work though and would increase usability when one wants to wrap other binary targets. Does it make sense to still merge this? My aim is really to increase usability of bazel and I am coming from an environment where I introduced bazel to a group of developers who have never heard of any of bazel's concepts and there it was a suprise that some undocumented setup is needed to be able to to run a binary in a shell wrapper and that "bazel does not figure it out itself". But of course good documentation (as tracked here #6402) would help here also to make the behaviour at least less surprising. |
|
Friendly ping to all. |
|
Closing due to age. Feel free to reopen if you have updates. |
|
Sorry, shouldn't have closed this. It's waiting for a reply from @laszlocsomor |
|
Yep it is indeed @laszlocsomor any chance to get an update on my last comment? |
|
Yes, sorry I haven't replied earlier.
I don't know. Frankly I don't understand what this PR does. It sets |
|
What's the state of this? |
|
@meisterT at the current state I do not use bazel as actively anymore, so while this imo is still a usability concern I do not have the time anymore, so feel free to close. |
|
Thanks for your Bazel contributions, I hope you come to Bazel one day! |
This is to fix cases where one depends on a binary that has the bash runfiles as one of their dependencies.
For background see: bazel-contrib/rules_nodejs#587
Posting the simple example here again:
Given a BUILD file
if
my_script.shtries to execute theappit will fail withERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash. I think this case should work without me having to know anything about how to setRUNFILESetc correctly for my dependency.Happy to provide more information if needed.