Skip to content

fix: Fall back to directory based runfiles using relative paths #2621

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

mering
Copy link

@mering mering commented Feb 21, 2025

This makes the runfiles libraries work when they're repackaged and extracted elsewhere.

e.g. a py_binary is packaged using pkg_tar, then extracted somewhere else.
The bazel-bin/{BIN,BIN.runfiles} structure (sans the bazel-bin prefix) is preserved, and then e.g. ./BIN is run. This is equivalent to running bazel-bin/BIN (i.e no
presumption about environment variables can be made), just the location differs.

This makes it work out of the box when the runfiles tree is packaged into a container image as follows:

py_binary(
    name = "binary",
    srcs = ["main.py"],
    data = ["runfile.txt"],
)

pkg_tar(
    name = "layer",
    srcs = [":binary"],
    strip_prefix = "/",
    include_runfiles = True,
)

oci_image(
    name = "image",
    tars = [":layer"],
)

@rickeylev
Copy link
Collaborator

Thanks for the fix @mering . I think this looks fine, but some surrounding changes to make:

  • Can you describe the problematic case and why this fixes it?
  • Tests: This looks easy to test if we just have a shell script setup the environment (unset the RUNFILES env vars) and call the binary and check it worked. The tests/support/sh_py_run_test.bzl#sh_py_run_test should make this pretty easy. There are examples of using it in tests/bootstrap_impls
  • CHANGELOG.md needs to be updated.

@mering
Copy link
Author

mering commented Feb 26, 2025

@rickeylev Thanks for the review. See my answers below:

  • When used in a container image without involving Bazel at all, the environment variables are not set but runfiles are in the expected folder BIN.runfiles as it was packaged via pkg_tar with include_runfiles = True. I updated the description with an example.
  • I tried doing this but for some reason the $RUNFILES_DIR is still set within Python even though I unset it in the shell script.
  • I added this to the CHANGELOG.

mering added 3 commits April 25, 2025 13:28
This will also work out of the box when the runfiles tree is packaged into a container image.
@mering mering force-pushed the runfiles-relative branch from cf7f3af to d060f57 Compare April 25, 2025 13:28
@rickeylev
Copy link
Collaborator

Overall LGTM. CI is failing -- that test just needs to be updated. It's assuming the only two ways to find the runfiles root is the environment variables, but we're adding a 3rd attempt to look based upon its own file path.

  • When used in a container image without involving Bazel at all, the environment variables are not set but runfiles are in the expected folder BIN.runfiles as it was packaged via pkg_tar with include_runfiles = True. I updated the description with an example.

Thanks for explaining. So basically, copying {BIN,BIN.runfiles} somewhere else, and then running e.g. ./BIN.

Yeah, that should work -- the structure is preserved; I don't see why it wouldn't. I've updated the description to reflect this.

  • I tried doing this but for some reason the $RUNFILES_DIR is still set within Python even though I unset it in the shell script.

What you're probably seeing is the bootstrap will set the RUNFILES_DIR environment variable (or manifest env var, depending on what it figures out).

er but -- if the bootstrap is always setting those env vars, how is runfiles not seeing them?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants