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

pkg_tar ignores Python toolchain #926

Open
bmclarnon opened this issue Jan 22, 2025 · 2 comments
Open

pkg_tar ignores Python toolchain #926

bmclarnon opened this issue Jan 22, 2025 · 2 comments

Comments

@bmclarnon
Copy link

bmclarnon commented Jan 22, 2025

pkg_tar delegates to build_tar.py to construct the output tar file. When that tool is run, the system python3 interpreter is used, regardless of the default Bazel python toolchain.

In most cases, this isn't an issue because python3 is available in the path and build_tar's output isn't affected by the Python version, but it's inconvenient when using remote execution: it requires that python3 is present in the build image -- even if the WORKSPACE registers a hermetic Python toolchain.

I verified the issue exists in 1.0.1 with a pretty old version of Bazel (6.5.0). I'm not sure if any rules_pkg rules besides pkg_tar are affected.

I believe the fix is straightforward: make the pkg_tar rule also depend on the current Python toolchain and include the corresponding runfiles in the ctx.actions.run() call. This should work with both platform and in-build runtimes based on the PyRuntimeInfo documentation.

--- pkg/private/tar/tar.bzl
+++ pkg/private/tar/tar.bzl
@@ -187,16 +187,21 @@ def _pkg_tar_impl(ctx):
+    py3_runtime = ctx.toolchains["@bazel_tools//tools/python:toolchain_type"].py3_runtime
     ctx.actions.run(
         mnemonic = "PackageTar",
         progress_message = "Writing: %s" % output_file.path,
         inputs = inputs,
-        tools = [ctx.executable.compressor] if ctx.executable.compressor else [],
+        tools = (
+            [ctx.executable.compressor] if ctx.executable.compressor else [] +
+            [py3_runtime.files] if py3_runtime.files else []
+        ),
         executable = ctx.executable._build_tar,
         arguments = [args],
         outputs = [output_file],
         env = {
             "LANG": "en_US.UTF-8",
             "LC_CTYPE": "UTF-8",
             "PYTHONIOENCODING": "UTF-8",
             "PYTHONUTF8": "1",
+            "PATH": "$PATH" + (":" + py3_runtime.interpreter.dirname if py3_runtime.interpreter else ""),
         },
         use_default_shell_env = True,
     )
@@ -310,3 +315,4 @@ pkg_tar_impl = rule(
         ),
     },
+    toolchains = ["@bazel_tools//tools/python:toolchain_type"],
 )

I'm happy to send a PR if you agree this is worth fixing. I'm also relatively new to Starlark, so I'm also very open to suggestions. Thanks!

@aiuto
Copy link
Collaborator

aiuto commented Jan 22, 2025 via email

@bmclarnon
Copy link
Author

Of course -- and thanks for the heads up!

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

No branches or pull requests

2 participants