From 0132e6567b5dc334144e97c39fa0064f4bf96209 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Tue, 1 Dec 2026 21:45:39 +0100 Subject: [PATCH] fix(zipapp): keep runfiles symlinks in --build_python_zip zips With `--build_python_zip`, building a `py_binary` or `py_test` that has another `py_binary` in its runfiles fails: File .../_bin.venv/bin/python3 does not seem to exist. The venv interpreter of the inner binary is a declared symlink with a relative target that only resolves inside a runfiles tree. Bazel's zipper reads every file through its symlinks, so it can't zip it. Build the zip with the zipper that `py_zipapp_binary` uses instead. It stores symlinks as symlink entries, which both zip bootstraps already restore when extracting. As with `py_zipapp_binary`, relative source symlinks are now kept as symlinks, and the zip no longer has an unused empty `__init__.py` at its root. The zip action is registered for every binary, so Bazel's zipper remains the fallback when neither the `shrubbery` toolchain nor an exec runtime is available. --- news/build-python-zip-binary-data.fixed.md | 3 + python/private/py_executable.bzl | 89 +++++++++++++++++++ tests/bootstrap_impls/BUILD.bazel | 21 ++++- tests/bootstrap_impls/call_data_binary.py | 7 ++ .../run_binary_zip_with_binary_data_test.sh | 26 ++++++ 5 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 news/build-python-zip-binary-data.fixed.md create mode 100644 tests/bootstrap_impls/call_data_binary.py create mode 100755 tests/bootstrap_impls/run_binary_zip_with_binary_data_test.sh diff --git a/news/build-python-zip-binary-data.fixed.md b/news/build-python-zip-binary-data.fixed.md new file mode 100644 index 0000000000..9dbad9c5f2 --- /dev/null +++ b/news/build-python-zip-binary-data.fixed.md @@ -0,0 +1,3 @@ +(zipapp) `--build_python_zip` now keeps symlinks in the runfiles as symlinks, +so it no longer fails when a `py_binary` has another `py_binary` in its +runfiles. diff --git a/python/private/py_executable.bzl b/python/private/py_executable.bzl index 4fb64b2a87..dcdf3c5486 100644 --- a/python/private/py_executable.bzl +++ b/python/private/py_executable.bzl @@ -280,6 +280,10 @@ accepting arbitrary Python versions. default = labels.VENVS_USE_DECLARE_SYMLINK, providers = [BuildSettingInfo], ), + "_zipapp_zipper": lambda: attrb.Label( + cfg = "exec", + default = "//tools/zipapp:zipper", + ), "_zipper": lambda: attrb.Label( cfg = "exec", executable = True, @@ -1129,8 +1133,93 @@ def _create_stage1_bootstrap( def _map_runtime_venv_symlink(entry): return entry.venv_path + "|" + entry.link_to_path +def _zip_is_symlink(file): + if hasattr(file, "is_symlink"): + return str(int(file.is_symlink)) + else: + return "-1" + +def _map_zip_empty_filenames(list_paths_cb): + return ["rf-empty|" + path for path in list_paths_cb().to_list()] + +def _map_zip_runfiles(file): + # NOTE: Use "+" for performance + return "rf-file|" + _zip_is_symlink(file) + "|" + file.short_path + "|" + file.path + def _create_zip_file(ctx, *, output, zip_main, runfiles): """Create a Python zipapp (zip with __main__.py entry point).""" + zipper = get_shrubbery_tool(ctx, "zipper", ctx.attr._zipapp_zipper) + if not zipper.from_shrubbery_toolchain: + exec_tools_toolchain = ctx.toolchains[EXEC_TOOLS_TOOLCHAIN_TYPE] + if exec_tools_toolchain == None or exec_tools_toolchain.exec_tools.exec_runtime == None: + # Bazel's zipper doesn't need an exec runtime, but fails on + # declared symlinks in the runfiles, such as the venv interpreter + # of a py_binary. + _create_zip_file_with_bazel_zipper( + ctx, + output = output, + zip_main = zip_main, + runfiles = runfiles, + ) + return + + manifest = ctx.actions.args() + manifest.use_param_file("%s", use_always = True) + manifest.set_param_file_format("multiline") + + manifest.add(zip_main.path, format = "regular|0|__main__.py|%s") + + # The zipper stores declared symlinks, such as the venv interpreter of a + # py_binary, as symlinks. It keeps the first entry for a path, so files + # come before the empty files that they may replace. + manifest.add_all(runfiles.files, map_each = _map_zip_runfiles) + manifest.add_all( + # NOTE: Accessing runfiles.empty_filenames implicitly flattens the runfiles. + # Smuggle a lambda in via a list to defer that flattening. + [lambda: runfiles.empty_filenames], + map_each = _map_zip_empty_filenames, + allow_closure = True, + ) + manifest.add("rf-empty|" + _INIT_PY) + + inputs = [zip_main] + zip_repo_mapping_manifest = maybe_create_repo_mapping( + ctx = ctx, + runfiles = runfiles, + ) + if zip_repo_mapping_manifest: + manifest.add( + zip_repo_mapping_manifest.path, + format = "rf-root-symlink|0|_repo_mapping|%s", + ) + inputs.append(zip_repo_mapping_manifest) + + zipper_args = ctx.actions.args() + zipper_args.add(output) + zipper_args.add(ctx.workspace_name, format = "--workspace-name=%s") + zipper_args.add( + str(int(_py_builtins.get_legacy_external_runfiles(ctx))), + format = "--legacy-external-runfiles=%s", + ) + zipper_args.add("--compression=6") + zipper_args.add("--runfiles-dir=" + _ZIP_RUNFILES_DIRECTORY_NAME) + zipper_args.add( + "\\" if is_windows_platform(ctx) else "/", + format = "--target-platform-pathsep=%s", + ) + + actions_run( + ctx, + executable = zipper.executable, + toolchain = zipper.toolchain, + arguments = zipper.prefix_args + [manifest, zipper_args], + inputs = depset(inputs, transitive = [runfiles.files]), + outputs = [output], + mnemonic = "PythonZipper", + progress_message = "Building Python zip: %{label}", + ) + +def _create_zip_file_with_bazel_zipper(ctx, *, output, zip_main, runfiles): workspace_name = ctx.workspace_name legacy_external_runfiles = _py_builtins.get_legacy_external_runfiles(ctx) diff --git a/tests/bootstrap_impls/BUILD.bazel b/tests/bootstrap_impls/BUILD.bazel index 4dc5acbead..b4290bddb7 100644 --- a/tests/bootstrap_impls/BUILD.bazel +++ b/tests/bootstrap_impls/BUILD.bazel @@ -18,7 +18,7 @@ load("//python:py_test.bzl", "py_test") load("//python:pytest_test.bzl", "pytest_test") load("//tests/support:py_reconfig.bzl", "py_reconfig_binary", "py_reconfig_test") load("//tests/support:sh_py_run_test.bzl", "sh_py_run_test") -load("//tests/support:support.bzl", "SUPPORTS_BOOTSTRAP_SCRIPT") +load("//tests/support:support.bzl", "NOT_WINDOWS", "SUPPORTS_BOOTSTRAP_SCRIPT") load(":venv_relative_path_tests.bzl", "relative_path_test_suite") py_reconfig_binary( @@ -65,6 +65,25 @@ sh_py_run_test( sh_src = "run_binary_zip_yes_test.sh", ) +sh_py_run_test( + name = "run_binary_zip_with_binary_data_test", + build_python_zip = True, + data = [":zip_data_bin"], + py_src = "call_data_binary.py", + sh_src = "run_binary_zip_with_binary_data_test.sh", + # --build_python_zip is ignored on Windows. + target_compatible_with = NOT_WINDOWS, +) + +py_reconfig_binary( + name = "zip_data_bin", + srcs = ["bin.py"], + # Runs from the runfiles of the zip above, so its venv must be in the zip. + build_python_zip = False, + main = "bin.py", + tags = ["manual"], +) + sh_py_run_test( name = "run_binary_venvs_use_declare_symlink_no_test", bootstrap_impl = "script", diff --git a/tests/bootstrap_impls/call_data_binary.py b/tests/bootstrap_impls/call_data_binary.py new file mode 100644 index 0000000000..0e6d3cf702 --- /dev/null +++ b/tests/bootstrap_impls/call_data_binary.py @@ -0,0 +1,7 @@ +"""Runs the py_binary that is a data dependency of this binary.""" + +import os +import subprocess + +data_bin = os.path.join(os.path.dirname(__file__), "zip_data_bin") +subprocess.run([data_bin], check=True) diff --git a/tests/bootstrap_impls/run_binary_zip_with_binary_data_test.sh b/tests/bootstrap_impls/run_binary_zip_with_binary_data_test.sh new file mode 100755 index 0000000000..705bcd2777 --- /dev/null +++ b/tests/bootstrap_impls/run_binary_zip_with_binary_data_test.sh @@ -0,0 +1,26 @@ +# --- begin runfiles.bash initialization v3 --- +# Copy-pasted from the Bazel Bash runfiles library v3. +set -uo pipefail; set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash +source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \ + source "$0.runfiles/$f" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ + { echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e +# --- end runfiles.bash initialization v3 --- +set +e + +bin=$(rlocation $BIN_RLOCATION) +if [[ -z "$bin" ]]; then + echo "Unable to locate test binary: $BIN_RLOCATION" + exit 1 +fi +actual=$($bin 2>&1) + +# The zip runs the data binary from its extracted runfiles, which requires the +# venv of the data binary to be intact. +if ! (echo "$actual" | grep "^Hello$" ) >/dev/null; then + echo "expected the data binary to print: Hello" + echo "but got: $actual" + exit 1 +fi