Skip to content

Default runfiles to upwards directory as fallback - #7774

Closed
Globegitter wants to merge 1 commit into
bazelbuild:masterfrom
Globegitter:patch-1
Closed

Globegitter wants to merge 1 commit into
bazelbuild:masterfrom
Globegitter:patch-1

Conversation

@Globegitter

@Globegitter Globegitter commented Mar 20, 2019 •

Copy link
Copy Markdown

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

nodejs_binary(
    name = "app", 
    entry_point = "__main__/app.js",
    data = ["app.js", "@npm//somedep"],
)

sh_binary(
    name = "app",
    srcs = ["my_script.sh"],
    data = [":app"],
)

if my_script.sh tries to execute the app it will fail with ERROR: cannot find @bazel_tools//tools/bash/runfiles:runfiles.bash. I think this case should work without me having to know anything about how to set RUNFILES etc correctly for my dependency.

Happy to provide more information if needed.

This is to fix cases where one depends on a binary that has the bash runfiles as one of their dependencies.
@laszlocsomor

Copy link
Copy Markdown
Contributor

Please provide a complete example so I can repro the problem.

The current example doesn't work:

  • rule names must differ
  • the sh_binary should depend on the runfiles library, see
    # Runfiles lookup library for Bazel-built Bash binaries and tests.
    #
    # ENVIRONMENT:
    # - Use the example code provided below. It initializes the environment
    # variables required by this script.
    # - If RUNFILES_LIB_DEBUG=1 is set, the script will print diagnostic messages to
    # stderr.
    #
    # USAGE:
    # 1. Depend on this runfiles library from your build rule:
    #
    # sh_binary(
    # name = "my_binary",
    # ...
    # deps = ["@bazel_tools//tools/bash/runfiles"],
    # )
    #
    # 2. Source the runfiles library.
    #
    # The runfiles library itself defines rlocation which you would need to look
    # up the library's runtime location, thus we have a chicken-and-egg problem.
    # Insert the following code snippet to the top of your main script:
    #
    # # --- 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 ---
    #
    # 3. Use rlocation to look up runfile paths:
    #
    # cat "$(rlocation my_workspace/path/to/my/data.txt)"
    #
  • rlocation's argument in my_script.sh may be wrong

@laszlocsomor

laszlocsomor commented Mar 26, 2019 •

Copy link
Copy Markdown
Contributor

Crucially, rlocation's argument must include the workspace name.

The workspace name is either defined in the WORKSPACE file's workspace() rule, or in absence of that rule the name is __main__.

@Globegitter

Copy link
Copy Markdown
Author

Sorry that was my bad with the example. I will put up a full repository to reproduce ASAP.

@Globegitter

Copy link
Copy Markdown
Author

@laszlocsomor

Copy link
Copy Markdown
Contributor

Thanks, I can repro the problem.

Interestingly, //app:app's runfiles_manifest (bazel-bin/app/app.bash.runfiles_manifest) contains external/bazel_tools/tools/bash/runfiles/runfiles.bash but not bazel_tools/tools/bash/runfiles/runfiles.bash. I don't yet understand why or what that means.

@laszlocsomor

Copy link
Copy Markdown
Contributor

I found the culprit: it's indeed the working directory, set by bazel run. But it's WAI.

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 bazel run/test, i.e. //app:app) must discover and export the envvars. Let's patch @com_github_atlassian_bazel_tools//:multirun/def.bzl like so:

  $ 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 bazel run //app.

I was wrong earlier, the //app:app's runfiles_manifest DOES contain bazel_tools/tools/bash/runfiles/runfiles.bash, it's a mystery how I missed that.

@laszlocsomor

Copy link
Copy Markdown
Contributor

Sent atlassian/bazel-tools#56

@Globegitter

Globegitter commented Apr 16, 2019 •

Copy link
Copy Markdown
Author

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.

@aiuto

aiuto commented Aug 21, 2019

Copy link
Copy Markdown

Friendly ping to all.

@ulfjack

ulfjack commented Oct 2, 2019

Copy link
Copy Markdown
Contributor

Closing due to age. Feel free to reopen if you have updates.

@ulfjack ulfjack closed this Oct 2, 2019
@ulfjack ulfjack reopened this Oct 2, 2019
@ulfjack

ulfjack commented Oct 2, 2019

Copy link
Copy Markdown
Contributor

Sorry, shouldn't have closed this. It's waiting for a reply from @laszlocsomor

@Globegitter

Copy link
Copy Markdown
Author

Yep it is indeed @laszlocsomor any chance to get an update on my last comment?

@laszlocsomor

Copy link
Copy Markdown
Contributor

Yes, sorry I haven't replied earlier.

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?

I don't know. Frankly I don't understand what this PR does. It sets RUNFILES_DIR=.., but what is the working directory that this is relative to, and where does .. take us?

@meisterT

Copy link
Copy Markdown
Member

What's the state of this?

@Globegitter

Copy link
Copy Markdown
Author

@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.

@meisterT

Copy link
Copy Markdown
Member

Thanks for your Bazel contributions, I hope you come to Bazel one day!

@meisterT meisterT closed this Jul 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants