Skip to content
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

Let Python stubs respect RUNFILES_* while finding the module space #14740

Conversation

EdSchouten
Copy link
Contributor

When invoking a py_binary() through an sh_binary() using the Bash
runfiles library, the location of the py_binary() will be resolved from
the runfiles manifest file. This means that the argv[0] of the Python
stub script will not point to a location under the runfiles directory of
the shell script, but to a location in Bazel's execroot.

Normally this does not lead to any issues, as argv[0] + ".runfiles"
happens to point to be a valid runfiles directory as well. It does
become problematic when --nobuild_runfile_links is provided, as in that
case only the outer shell script is guaranteed to have a runfiles
directory; not the inner Python script.

This change extends the Python stub template to also consider
RUNFILES_DIR when no runfiles directory can be found. Even though it's
not technically correct, we also attempt to derive the runfiles
directory from RUNFILES_MANIFEST_FILE. I suspect that this is a
necessity as long as py_binary()s cannot operate purely using a manifest
file. They currently depend on a concrete instantiation of the runfiles
directory.

Fixes: #11997

@EdSchouten EdSchouten force-pushed the eschouten/20220205-python-runfiles-dir branch 4 times, most recently from 0aa9bb0 to 538e2e0 Compare February 7, 2022 19:54
@sgowroji sgowroji added the team-Rules-Python Native rules for Python label Mar 24, 2022
@sgowroji sgowroji added the awaiting-review PR is awaiting review from an assigned reviewer label Apr 21, 2022
@EdSchouten
Copy link
Contributor Author

Friendly ping!

@EdSchouten
Copy link
Contributor Author

Hi @brandjon; happen to have a couple of spare cycles to look into this? Thanks!

@EdSchouten EdSchouten force-pushed the eschouten/20220205-python-runfiles-dir branch from 538e2e0 to 868279b Compare August 11, 2022 12:41
@EdSchouten
Copy link
Contributor Author

Hey @comius, would you be interested in reviewing this change for me? Thanks!

@EdSchouten
Copy link
Contributor Author

Hmmm... The CI failures for "buildkite/bazel-bazel-github-presubmit/darwin-openjdk-11-xcode-shard-*" seem to be caused by infrastructure failure. The underlying system ran out of disk space?

@EdSchouten EdSchouten force-pushed the eschouten/20220205-python-runfiles-dir branch from 868279b to af4d5e5 Compare August 12, 2022 17:26
@EdSchouten EdSchouten force-pushed the eschouten/20220205-python-runfiles-dir branch from af4d5e5 to e59eb0d Compare August 22, 2022 11:03
@EdSchouten EdSchouten force-pushed the eschouten/20220205-python-runfiles-dir branch from e59eb0d to 9b00616 Compare September 26, 2022 09:36
@EdSchouten
Copy link
Contributor Author

Hi @rickeylev, I think I've addressed all of your comments. PTAL!

@EdSchouten EdSchouten force-pushed the eschouten/20220205-python-runfiles-dir branch from 9b00616 to 73c2f4c Compare September 26, 2022 09:39
When invoking a py_binary() through an sh_binary() using the Bash
runfiles library, the location of the py_binary() will be resolved from
the runfiles manifest file. This means that the argv[0] of the Python
stub script will not point to a location under the runfiles directory of
the shell script, but to a location in Bazel's execroot.

Normally this does not lead to any issues, as argv[0] + ".runfiles"
happens to point to be a valid runfiles directory as well. It does
become problematic when --nobuild_runfile_links is provided, as in that
case only the outer shell script is guaranteed to have a runfiles
directory; not the inner Python script.

This change extends the Python stub template to also consider
RUNFILES_DIR when no runfiles directory can be found. Even though it's
not technically correct, we also attempt to derive the runfiles
directory from RUNFILES_MANIFEST_FILE. I suspect that this is a
necessity as long as py_binary()s cannot operate purely using a manifest
file. They currently depend on a concrete instantiation of the runfiles
directory.

Fixes: bazelbuild#11997
@EdSchouten EdSchouten force-pushed the eschouten/20220205-python-runfiles-dir branch from 73c2f4c to 86521ab Compare September 26, 2022 09:40
@rickeylev rickeylev assigned rickeylev and unassigned brandjon Sep 26, 2022
@rickeylev rickeylev added awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally and removed awaiting-review PR is awaiting review from an assigned reviewer labels Sep 26, 2022
@rickeylev
Copy link
Contributor

Ok, I think I set the labels up correctly here for the "import it into google" people to process it.

If you don't hear back in a day that the import process has begun, tag me and I'll check it didn't fall into some void

@EdSchouten
Copy link
Contributor Author

Hey @rickeylev, just tagging you because two days have passed. :D

@sgowroji
Copy link
Member

Hello @EdSchouten, Thank you for the patience. I will update once it gets merged.

@rickeylev
Copy link
Contributor

Just to give an update: I've been fighting with our internal windows test environment to make the test added to python_stub_test.sh pass for the last day or so. For whatever reason, it seems to be missing a lot of the supporting code/config that the github environment has. If I don't have it figured out by Monday, I'll just disable this test in our internal builds for now.

@rickeylev
Copy link
Contributor

Woo, got everything happy! Code is out for review internally; should land early next week.

@copybara-service copybara-service bot closed this in c3425fe Oct 4, 2022
@EdSchouten EdSchouten deleted the eschouten/20220205-python-runfiles-dir branch October 5, 2022 05:22
@sgowroji sgowroji removed the awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally label Oct 6, 2022
aiuto pushed a commit to aiuto/bazel that referenced this pull request Oct 12, 2022
When invoking a py_binary() through an sh_binary() using the Bash
runfiles library, the location of the py_binary() will be resolved from
the runfiles manifest file. This means that the argv[0] of the Python
stub script will not point to a location under the runfiles directory of
the shell script, but to a location in Bazel's execroot.

Normally this does not lead to any issues, as argv[0] + ".runfiles"
happens to point to be a valid runfiles directory as well. It does
become problematic when --nobuild_runfile_links is provided, as in that
case only the outer shell script is guaranteed to have a runfiles
directory; not the inner Python script.

This change extends the Python stub template to also consider
RUNFILES_DIR when no runfiles directory can be found. Even though it's
not technically correct, we also attempt to derive the runfiles
directory from RUNFILES_MANIFEST_FILE. I suspect that this is a
necessity as long as py_binary()s cannot operate purely using a manifest
file. They currently depend on a concrete instantiation of the runfiles
directory.

Fixes: bazelbuild#11997

Closes bazelbuild#14740.

PiperOrigin-RevId: 478857199
Change-Id: I8cc6ea014bfd4b9ea2f1672e8e814ba38a5bf471
github-merge-queue bot pushed a commit to bazelbuild/rules_python that referenced this pull request Sep 26, 2023
Pass the environment generated by the `runfiles` helper so that the
Python launcher can find the runfiles correctly as implemented in
bazelbuild/bazel#14740.

Fixes #1426
github-merge-queue bot pushed a commit to bazelbuild/rules_python that referenced this pull request Sep 26, 2023
Pass the environment generated by the `runfiles` helper so that the
Python launcher can find the runfiles correctly as implemented in
bazelbuild/bazel#14740.

Fixes #1426
wingsofovnia pushed a commit to wingsofovnia/rules_python_gazelle_plugin that referenced this pull request May 3, 2024
Pass the environment generated by the `runfiles` helper so that the
Python launcher can find the runfiles correctly as implemented in
bazelbuild/bazel#14740.

Fixes #1426
wingsofovnia pushed a commit to wingsofovnia/rules_python_gazelle_plugin that referenced this pull request May 3, 2024
Pass the environment generated by the `runfiles` helper so that the
Python launcher can find the runfiles correctly as implemented in
bazelbuild/bazel#14740.

Fixes #1426
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-Rules-Python Native rules for Python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

py_binary located via manifest-based runfiles lib fails when its own runfiles tree isn't built
4 participants