refactor(toolchains): generalize exe_zip_maker into shrubbery toolchain (#4402)
Separate per-tool Rust binaries and toolchains duplicate the Rust
standard library across release artifacts and require distinct toolchain
types and flags for every converted build tool.
Replace the standalone `exe_zip_maker` binary and toolchain with the
unified `shrubbery` ("Shell replacements for Bazel build tools in Rust")
subcommand binary and `shrubbery_toolchain`
(`--//python/private/toolchains:need_shrubbery`), and route both
`py_zipapp` and `py_binary` self-executable zip creation through
`get_shrubbery_tool`.
Work towards #4250
diff --git a/.bazelci/presubmit.yml b/.bazelci/presubmit.yml
index 219a1e5..c3ddb1b 100644
--- a/.bazelci/presubmit.yml
+++ b/.bazelci/presubmit.yml
@@ -61,12 +61,19 @@
test_flags:
? "--noenable_bzlmod"
? "--test_tag_filters=-integration-test"
+ coverage_flags:
+ ? "--noenable_bzlmod"
+ ? "--test_tag_filters=-integration-test"
.common_workspace_flags: &common_workspace_flags
skip_in_bazel_downstream_pipeline: "Bazel 9 doesn't support workspace"
test_flags:
? "--noenable_bzlmod"
? "--enable_workspace"
? "--test_tag_filters=-integration-test"
+ coverage_flags:
+ ? "--noenable_bzlmod"
+ ? "--enable_workspace"
+ ? "--test_tag_filters=-integration-test"
build_flags:
? "--noenable_bzlmod"
? "--enable_workspace"
diff --git a/.bazelrc.deleted_packages b/.bazelrc.deleted_packages
index d0c8d40..4b8b5bf 100644
--- a/.bazelrc.deleted_packages
+++ b/.bazelrc.deleted_packages
@@ -13,6 +13,7 @@
common --deleted_packages=examples/bzlmod/tests
common --deleted_packages=examples/bzlmod/tests/other_module
common --deleted_packages=examples/bzlmod/whl_mods
+common --deleted_packages=examples/multi_python_versions
common --deleted_packages=examples/multi_python_versions/libs/my_lib
common --deleted_packages=examples/multi_python_versions/requirements
common --deleted_packages=examples/multi_python_versions/tests
diff --git a/crates/BUILD.bazel b/crates/BUILD.bazel
index d6bf725..2c4fb2b 100644
--- a/crates/BUILD.bazel
+++ b/crates/BUILD.bazel
@@ -1,10 +1,9 @@
+load("//python/private:distribution_filegroup.bzl", "distribution_filegroup") # buildifier: disable=bzl-visibility
+
package(default_visibility = ["//:__subpackages__"])
licenses(["notice"])
-filegroup(
+distribution_filegroup(
name = "distribution",
- srcs = glob(["**"]) + [
- "//crates/exe_zip_maker:distribution",
- ],
)
diff --git a/crates/exe_zip_maker/BUILD.bazel b/crates/exe_zip_maker/BUILD.bazel
index 94c4783..bb7e741 100644
--- a/crates/exe_zip_maker/BUILD.bazel
+++ b/crates/exe_zip_maker/BUILD.bazel
@@ -1,4 +1,5 @@
-load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")
+load("@rules_rust//rust:defs.bzl", "rust_library")
+load("//python/private:distribution_filegroup.bzl", "distribution_filegroup") # buildifier: disable=bzl-visibility
package(default_visibility = ["//:__subpackages__"])
@@ -11,15 +12,6 @@
deps = ["@crates//:sha2"],
)
-rust_binary(
- name = "exe_zip_maker",
- srcs = ["src/main.rs"],
- edition = "2021",
- visibility = ["//visibility:public"],
- deps = [":exe_zip_maker_lib"],
-)
-
-filegroup(
+distribution_filegroup(
name = "distribution",
- srcs = glob(["**"]),
)
diff --git a/crates/exe_zip_maker/src/lib.rs b/crates/exe_zip_maker/src/lib.rs
index a7b467e..9afa6d3 100644
--- a/crates/exe_zip_maker/src/lib.rs
+++ b/crates/exe_zip_maker/src/lib.rs
@@ -1,5 +1,6 @@
//! Library supporting creating self-executable zip files.
+use std::ffi::OsString;
use std::fs::{self, File};
use std::io::{self, BufReader, BufWriter, Read, Write};
use std::path::Path;
@@ -41,7 +42,7 @@
hasher.update(&buffer[..n]);
}
let digest = hasher.finalize();
- Ok(format!("{:x}", digest))
+ Ok(digest.iter().map(|b| format!("{b:02x}")).collect())
}
/// Creates a self-executable zip archive by prepending a preamble to a zip archive
@@ -77,3 +78,25 @@
Ok(())
}
+
+/// Parses subcommand arguments (`<preamble> <zip> <output>`) and creates a self-executable zip
+/// archive, returning the process exit code.
+pub fn run_cli<I, T>(args: I) -> i32
+where
+ I: IntoIterator<Item = T>,
+ T: Into<OsString>,
+{
+ let os_args: Vec<OsString> = args.into_iter().map(Into::into).collect();
+ if os_args.len() != 3 {
+ eprintln!("Usage: exe_zip_maker <preamble> <zip> <output>");
+ return 1;
+ }
+ let preamble_path = Path::new(&os_args[0]);
+ let zip_path = Path::new(&os_args[1]);
+ let output_path = Path::new(&os_args[2]);
+ if let Err(e) = create_exe_zip(preamble_path, zip_path, output_path) {
+ eprintln!("exe_zip_maker: error: {}", e);
+ return 1;
+ }
+ 0
+}
diff --git a/crates/exe_zip_maker/src/main.rs b/crates/exe_zip_maker/src/main.rs
deleted file mode 100644
index d66fdb2..0000000
--- a/crates/exe_zip_maker/src/main.rs
+++ /dev/null
@@ -1,24 +0,0 @@
-use std::env;
-use std::path::Path;
-use std::process;
-
-fn main() {
- let args: Vec<_> = env::args_os().collect();
- if args.len() != 4 {
- let prog_name = args
- .first()
- .map(|s| s.to_string_lossy().into_owned())
- .unwrap_or_else(|| "exe_zip_maker".to_string());
- eprintln!("Usage: {} <preamble> <zip> <output>", prog_name);
- process::exit(1);
- }
-
- let preamble_path = Path::new(&args[1]);
- let zip_path = Path::new(&args[2]);
- let output_path = Path::new(&args[3]);
-
- if let Err(e) = exe_zip_maker_lib::create_exe_zip(preamble_path, zip_path, output_path) {
- eprintln!("exe_zip_maker: error: {}", e);
- process::exit(1);
- }
-}
diff --git a/crates/shrubbery/BUILD.bazel b/crates/shrubbery/BUILD.bazel
new file mode 100644
index 0000000..338dff3
--- /dev/null
+++ b/crates/shrubbery/BUILD.bazel
@@ -0,0 +1,27 @@
+load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_library")
+load("//python/private:distribution_filegroup.bzl", "distribution_filegroup") # buildifier: disable=bzl-visibility
+
+package(default_visibility = ["//:__subpackages__"])
+
+licenses(["notice"])
+
+rust_library(
+ name = "shrubbery_lib",
+ srcs = ["src/lib.rs"],
+ edition = "2021",
+ deps = [
+ "//crates/exe_zip_maker:exe_zip_maker_lib",
+ ],
+)
+
+rust_binary(
+ name = "shrubbery",
+ srcs = ["src/main.rs"],
+ edition = "2021",
+ visibility = ["//visibility:public"],
+ deps = [":shrubbery_lib"],
+)
+
+distribution_filegroup(
+ name = "distribution",
+)
diff --git a/crates/shrubbery/src/lib.rs b/crates/shrubbery/src/lib.rs
new file mode 100644
index 0000000..7b06fda
--- /dev/null
+++ b/crates/shrubbery/src/lib.rs
@@ -0,0 +1,57 @@
+//! Unified `shrubbery` ("Shell replacements for Bazel build tools in Rust") subcommand
+//! dispatcher for `@rules_python` Rust build tools.
+
+use std::ffi::OsString;
+
+/// Canonical subcommand names supported by `shrubbery`.
+pub const SUBCOMMANDS: &[&str] = &[
+ // keep-sorted start
+ "exe_zip_maker",
+ // keep-sorted end
+];
+
+fn print_usage(prog_name: &str, to_stderr: bool) {
+ let msg = format!(
+ "shrubbery — A nice, two-level effect with a little path running down the middle.\n\nUsage: {} <subcommand> [args...]\n\nAvailable subcommands:\n {}",
+ prog_name,
+ SUBCOMMANDS.join("\n ")
+ );
+ if to_stderr {
+ eprintln!("{}", msg);
+ } else {
+ println!("{}", msg);
+ }
+}
+
+/// Dispatches CLI arguments (including `argv[0]`) to the requested subcommand and returns the
+/// process exit code.
+pub fn run_cli<I, T>(args: I) -> i32
+where
+ I: IntoIterator<Item = T>,
+ T: Into<OsString>,
+{
+ let mut iter = args.into_iter().map(Into::into);
+ let prog_name = iter
+ .next()
+ .map(|s| s.to_string_lossy().into_owned())
+ .unwrap_or_else(|| "shrubbery".to_string());
+
+ let Some(sub_os) = iter.next() else {
+ print_usage(&prog_name, true);
+ return 1;
+ };
+ let sub_raw = sub_os.to_string_lossy();
+ if sub_raw == "-h" || sub_raw == "--help" || sub_raw == "help" {
+ print_usage(&prog_name, false);
+ return 0;
+ }
+
+ match sub_raw.as_ref() {
+ "exe_zip_maker" => exe_zip_maker_lib::run_cli(iter),
+ _ => {
+ eprintln!("shrubbery: error: unknown subcommand '{}'", sub_raw);
+ print_usage(&prog_name, true);
+ 1
+ }
+ }
+}
diff --git a/crates/shrubbery/src/main.rs b/crates/shrubbery/src/main.rs
new file mode 100644
index 0000000..425eb4c
--- /dev/null
+++ b/crates/shrubbery/src/main.rs
@@ -0,0 +1,6 @@
+use std::env;
+use std::process;
+
+fn main() {
+ process::exit(shrubbery_lib::run_cli(env::args_os()));
+}
diff --git a/dev/release_artifacts/BUILD.bazel b/dev/release_artifacts/BUILD.bazel
index 54d26f4..1c3674e 100644
--- a/dev/release_artifacts/BUILD.bazel
+++ b/dev/release_artifacts/BUILD.bazel
@@ -13,7 +13,9 @@
# see build.sh.
release_files(
name = "artifacts_for_release",
- srcs = ["//crates/exe_zip_maker"],
+ srcs = [
+ "//crates/shrubbery",
+ ],
# Under WORKSPACE, rules_rust is a stub without the rustc flags setting
# that release_files sets, so building this would fail.
tags = [] if BZLMOD_ENABLED else ["manual"],
diff --git a/dev/release_artifacts/release_files.bzl b/dev/release_artifacts/release_files.bzl
index b91541d..d4010b6 100644
--- a/dev/release_artifacts/release_files.bzl
+++ b/dev/release_artifacts/release_files.bzl
@@ -12,13 +12,13 @@
def _release_transition_impl(settings, attr):
features = []
- rustc_flags = []
+ rustc_flags = ["-Cstrip=symbols"]
if attr.target_os == "linux":
# Rust links glibc dynamically, so the files would require at least
# the build machine's glibc version. Link it statically so they run on
# older distros too. Rust links musl statically for most CPUs, but not
# all, e.g. riscv64.
- rustc_flags = ["-Ctarget-feature=+crt-static"]
+ rustc_flags.append("-Ctarget-feature=+crt-static")
if attr.target_libc != "musl":
# The gold linker can't link static glibc, so use bfd.
rustc_flags.append("-Clink-arg=-fuse-ld=bfd")
@@ -26,7 +26,7 @@
# Statically link the C runtime so that the VC++ redistributable isn't
# required.
features = ["static_link_msvcrt"]
- rustc_flags = ["-Ctarget-feature=+crt-static"]
+ rustc_flags.append("-Ctarget-feature=+crt-static")
return {
"//command_line_option:compilation_mode": "opt",
# Only affects macOS. Without it, the minimum OS version is the build
diff --git a/dev/release_artifacts/verify.sh b/dev/release_artifacts/verify.sh
index bce09cf..1033a58 100755
--- a/dev/release_artifacts/verify.sh
+++ b/dev/release_artifacts/verify.sh
@@ -22,7 +22,12 @@
while IFS= read -r bin; do
if [[ "$triple_arg" == "linux" ]]; then
name="${bin##*/}"
- triple="${name#*-}"
+ if [[ "$name" =~ ^.+-(aarch64|armv7|i686|powerpc64le|riscv64gc|x86_64)-(.+)$ ]]; then
+ triple="${BASH_REMATCH[1]}-${BASH_REMATCH[2]}"
+ else
+ echo "::error::$bin: cannot parse triple from $name"
+ exit 1
+ fi
else
triple="$triple_arg"
fi
diff --git a/examples/multi_python_versions/BUILD.bazel b/examples/multi_python_versions/BUILD.bazel
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/examples/multi_python_versions/BUILD.bazel
diff --git a/python/private/BUILD.bazel b/python/private/BUILD.bazel
index 65d3537..3fd205f 100644
--- a/python/private/BUILD.bazel
+++ b/python/private/BUILD.bazel
@@ -563,6 +563,7 @@
":toolchain_types",
":transition_labels",
":venv_runfiles",
+ "//python/private/toolchains:shrubbery_toolchain",
"@bazel_skylib//lib:dicts",
"@bazel_skylib//lib:paths",
"@bazel_skylib//lib:structs",
diff --git a/python/private/common.bzl b/python/private/common.bzl
index 28f33ed..cbea882 100644
--- a/python/private/common.bzl
+++ b/python/private/common.bzl
@@ -646,7 +646,8 @@
provides `testing.ExecutionInfo`, the requirements will be added to
the execution requirements.
toolchain: The toolchain type to use. Must be None or
- `//python:exec_tools_toolchain_type`.
+ `//python:exec_tools_toolchain_type` when `executable` provides
+ `PyInterpreterProgramInfo`.
**kwargs: Additional arguments to pass to `ctx.actions.run()`.
`mnemonic` and `progress_message` are required.
"""
diff --git a/python/private/py_executable.bzl b/python/private/py_executable.bzl
index 3498c88..e57562b 100644
--- a/python/private/py_executable.bzl
+++ b/python/private/py_executable.bzl
@@ -19,6 +19,7 @@
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@rules_cc//cc/common:cc_common.bzl", "cc_common")
load("@rules_python_internal//:rules_python_config.bzl", rp_config = "config")
+load("//python/private/toolchains:shrubbery_toolchain.bzl", "get_shrubbery_tool")
load(":attr_builders.bzl", "attrb")
load(
":attributes.bzl",
@@ -73,6 +74,7 @@
"CC_TOOLCHAIN_TYPE",
"EXEC_TOOLS_TOOLCHAIN_TYPE",
"LAUNCHER_MAKER_TOOLCHAIN_TYPE",
+ "SHRUBBERY_TOOLCHAIN_TYPE",
TOOLCHAIN_TYPE = "TARGET_TOOLCHAIN_TYPE",
)
load(":transition_labels.bzl", "TRANSITION_LABELS")
@@ -1126,10 +1128,16 @@
args.add(prelude)
args.add(zip_file)
args.add(output)
+ exe_zip_maker = get_shrubbery_tool(
+ ctx,
+ "exe_zip_maker",
+ ctx.attr._exe_zip_maker,
+ )
actions_run(
ctx,
- executable = ctx.attr._exe_zip_maker,
- arguments = [args],
+ executable = exe_zip_maker.executable,
+ toolchain = exe_zip_maker.toolchain,
+ arguments = exe_zip_maker.prefix_args + [args],
inputs = depset([prelude, zip_file]),
outputs = [output],
mnemonic = "PyBuildExecutableZip",
@@ -2238,6 +2246,7 @@
ruleb.ToolchainType(TOOLCHAIN_TYPE),
ruleb.ToolchainType(EXEC_TOOLS_TOOLCHAIN_TYPE, mandatory = False),
ruleb.ToolchainType(CC_TOOLCHAIN_TYPE, mandatory = False),
+ ruleb.ToolchainType(SHRUBBERY_TOOLCHAIN_TYPE, mandatory = False),
] + ([ruleb.ToolchainType(LAUNCHER_MAKER_TOOLCHAIN_TYPE)] if rp_config.bazel_9_or_later else []),
cfg = dict(
implementation = _transition_executable_impl,
diff --git a/python/private/toolchain_types.bzl b/python/private/toolchain_types.bzl
index a4e5124..08c7bcd 100644
--- a/python/private/toolchain_types.bzl
+++ b/python/private/toolchain_types.bzl
@@ -20,7 +20,7 @@
TARGET_TOOLCHAIN_TYPE = Label("//python:toolchain_type")
EXEC_TOOLS_TOOLCHAIN_TYPE = Label("//python:exec_tools_toolchain_type")
-EXE_ZIP_MAKER_TOOLCHAIN_TYPE = Label("//python/private/toolchain_types:exe_zip_maker")
+SHRUBBERY_TOOLCHAIN_TYPE = Label("//python/private/toolchain_types:shrubbery")
PY_CC_TOOLCHAIN_TYPE = Label("//python/cc:toolchain_type")
LAUNCHER_MAKER_TOOLCHAIN_TYPE = Label("@bazel_tools//tools/launcher:launcher_maker_toolchain_type")
CC_TOOLCHAIN_TYPE = Label("@bazel_tools//tools/cpp:toolchain_type")
diff --git a/python/private/toolchain_types/BUILD.bazel b/python/private/toolchain_types/BUILD.bazel
index a3066bf..e93043e 100644
--- a/python/private/toolchain_types/BUILD.bazel
+++ b/python/private/toolchain_types/BUILD.bazel
@@ -14,10 +14,10 @@
name = "distribution",
)
-# Toolchain providing the tool that creates self-executable zip files
-# (`<preamble> <zip> <output>`). It is optional: when no toolchain is
-# registered, rules fall back to the Python `//tools/zipapp:exe_zip_maker`.
+# Toolchain providing the unified Rust `shrubbery` binary
+# (`shrubbery <subcommand> ...`). It is optional: when no toolchain is
+# registered, rules fall back to their Python or script implementations.
toolchain_type(
- name = "exe_zip_maker",
+ name = "shrubbery",
visibility = NOT_ACTUALLY_PUBLIC,
)
diff --git a/python/private/toolchains/BUILD.bazel b/python/private/toolchains/BUILD.bazel
index 7ca7aaf..a6dbabe 100644
--- a/python/private/toolchains/BUILD.bazel
+++ b/python/private/toolchains/BUILD.bazel
@@ -13,16 +13,17 @@
name = "distribution",
)
-# Whether to use the Rust implementation of exe_zip_maker.
+# Whether to use the unified Rust implementation (`shrubbery`) for
+# build tools.
# * `yes`: use it.
-# * `no`: use the built-in Python implementation.
+# * `no`: use the built-in Python or script implementations.
# * `auto`: let rules_python decide. Currently `yes` when developing
# rules_python (TOOL_DEPS_DEV_ONLY = False), and `no` in releases.
#
-# Enable with `--//python/private/toolchains:use_rust_exe_zip_maker=yes` or
-# disable with `--//python/private/toolchains:use_rust_exe_zip_maker=no`.
+# Enable with `--//python/private/toolchains:need_shrubbery=yes` or
+# disable with `--//python/private/toolchains:need_shrubbery=no`.
string_flag(
- name = "use_rust_exe_zip_maker",
+ name = "need_shrubbery",
build_setting_default = "auto",
values = [
"auto",
@@ -33,23 +34,23 @@
)
config_setting(
- name = "is_rust_exe_zip_maker_auto",
- flag_values = {":use_rust_exe_zip_maker": "auto"},
+ name = "is_need_shrubbery_auto",
+ flag_values = {":need_shrubbery": "auto"},
visibility = ["//visibility:private"],
)
config_setting(
- name = "is_rust_exe_zip_maker_yes",
- flag_values = {":use_rust_exe_zip_maker": "yes"},
+ name = "is_need_shrubbery_yes",
+ flag_values = {":need_shrubbery": "yes"},
visibility = ["//visibility:private"],
)
selects.config_setting_group(
- name = "is_rust_exe_zip_maker_enabled",
+ name = "is_need_shrubbery_enabled",
match_any = [
- ":is_rust_exe_zip_maker_yes",
+ ":is_need_shrubbery_yes",
] + ([
- ":is_rust_exe_zip_maker_auto",
+ ":is_need_shrubbery_auto",
] if not config.tool_deps_dev_only else []),
visibility = NOT_ACTUALLY_PUBLIC,
)
@@ -64,3 +65,12 @@
"//python/private:toolchain_types",
],
)
+
+bzl_library(
+ name = "shrubbery_toolchain",
+ srcs = ["shrubbery_toolchain.bzl"],
+ deps = [
+ "//python/private:sentinel_impl",
+ "//python/private:toolchain_types",
+ ],
+)
diff --git a/python/private/toolchains/from_source/BUILD.bazel b/python/private/toolchains/from_source/BUILD.bazel
index bbb52ec..6e90e28 100644
--- a/python/private/toolchains/from_source/BUILD.bazel
+++ b/python/private/toolchains/from_source/BUILD.bazel
@@ -7,7 +7,7 @@
load("//python/private:distribution_filegroup.bzl", "distribution_filegroup")
load("//python/private:visibility.bzl", "NOT_ACTUALLY_PUBLIC")
-load("//python/private/zipapp:py_exe_zip_maker_toolchain.bzl", "py_exe_zip_maker_toolchain")
+load("//python/private/toolchains:shrubbery_toolchain.bzl", "shrubbery_toolchain")
package(default_visibility = ["//:__subpackages__"])
@@ -17,9 +17,10 @@
name = "distribution",
)
-# Use the Rust implementation of exe_zip_maker instead of the Python one.
-py_exe_zip_maker_toolchain(
- name = "exe_zip_maker",
- exe_zip_maker = "//crates/exe_zip_maker",
+# Use the unified Rust `shrubbery` binary instead of the Python/script
+# tool implementations.
+shrubbery_toolchain(
+ name = "shrubbery",
+ shrubbery = "//crates/shrubbery",
visibility = NOT_ACTUALLY_PUBLIC,
)
diff --git a/python/private/toolchains/repo.bzl b/python/private/toolchains/repo.bzl
index af2fac9..2ac0d15 100644
--- a/python/private/toolchains/repo.bzl
+++ b/python/private/toolchains/repo.bzl
@@ -8,35 +8,35 @@
# makes --experimental_remote_repo_contents_cache materialize the whole repo.
load("//python/private:artifacts_manifest.bzl", "ARTIFACTS_MANIFEST_JSON")
load("//python/private:text_util.bzl", "render")
-load("//python/private:toolchain_types.bzl", "EXE_ZIP_MAKER_TOOLCHAIN_TYPE")
+load("//python/private:toolchain_types.bzl", "SHRUBBERY_TOOLCHAIN_TYPE")
-_IS_RUST_EXE_ZIP_MAKER_ENABLED = (
- "@rules_python//python/private/toolchains:is_rust_exe_zip_maker_enabled"
+_IS_NEED_SHRUBBERY_ENABLED = (
+ "@rules_python//python/private/toolchains:is_need_shrubbery_enabled"
)
-_FROM_SOURCE_EXE_ZIP_MAKER = (
- "@rules_python//python/private/toolchains/from_source:exe_zip_maker"
+_FROM_SOURCE_SHRUBBERY = (
+ "@rules_python//python/private/toolchains/from_source:shrubbery"
)
-_PREBUILT_EXE_ZIP_MAKER_TOOLCHAIN_IMPL_BUILD_TEMPLATE = """\
+_PREBUILT_SHRUBBERY_TOOLCHAIN_IMPL_BUILD_TEMPLATE = """\
# Generated by @rules_python//python/private/toolchains:repo.bzl
load(
- "@rules_python//python/private/zipapp:py_exe_zip_maker_toolchain.bzl",
- "py_exe_zip_maker_toolchain",
+ "@rules_python//python/private/toolchains:shrubbery_toolchain.bzl",
+ "shrubbery_toolchain",
)
-py_exe_zip_maker_toolchain(
+shrubbery_toolchain(
name = "toolchain",
- exe_zip_maker = "{exe_zip_maker}",
+ shrubbery = "{shrubbery}",
visibility = ["//visibility:public"],
)
"""
-def _prebuilt_exe_zip_maker_toolchain_impl_repo_impl(rctx):
+def _prebuilt_shrubbery_toolchain_impl_repo_impl(rctx):
rctx.file(
"BUILD.bazel",
- _PREBUILT_EXE_ZIP_MAKER_TOOLCHAIN_IMPL_BUILD_TEMPLATE.format(
- exe_zip_maker = rctx.attr.exe_zip_maker,
+ _PREBUILT_SHRUBBERY_TOOLCHAIN_IMPL_BUILD_TEMPLATE.format(
+ shrubbery = rctx.attr.shrubbery,
),
executable = False,
)
@@ -46,15 +46,15 @@
return None
return rctx.repo_metadata(reproducible = True)
-prebuilt_exe_zip_maker_toolchain_impl_repo = repository_rule(
- implementation = _prebuilt_exe_zip_maker_toolchain_impl_repo_impl,
+prebuilt_shrubbery_toolchain_impl_repo = repository_rule(
+ implementation = _prebuilt_shrubbery_toolchain_impl_repo_impl,
doc = """
-Creates a repository containing a py_exe_zip_maker_toolchain target for a
+Creates a repository containing a shrubbery_toolchain target for a
prebuilt binary.
""",
attrs = {
- "exe_zip_maker": attr.string(
- doc = "Label string of the prebuilt exe_zip_maker binary file.",
+ "shrubbery": attr.string(
+ doc = "Label string of the prebuilt shrubbery binary file.",
mandatory = True,
),
},
@@ -69,9 +69,9 @@
render.toolchain(
name = toolchain_name,
exec_compatible_with = exec_compatible_with,
- target_settings = [_IS_RUST_EXE_ZIP_MAKER_ENABLED],
+ target_settings = [_IS_NEED_SHRUBBERY_ENABLED],
toolchain = toolchain_impl,
- toolchain_type = str(EXE_ZIP_MAKER_TOOLCHAIN_TYPE),
+ toolchain_type = str(SHRUBBERY_TOOLCHAIN_TYPE),
),
)
rctx.file(
@@ -114,8 +114,8 @@
# Called by _config_impl in //python/extensions:config.bzl.
def configure_tool_toolchains(
http_file = http_file,
- prebuilt_exe_zip_maker_toolchain_impl_repo = (
- prebuilt_exe_zip_maker_toolchain_impl_repo
+ prebuilt_shrubbery_toolchain_impl_repo = (
+ prebuilt_shrubbery_toolchain_impl_repo
),
tools_toolchains_repo = tools_toolchains_repo,
artifacts_manifest_json = ARTIFACTS_MANIFEST_JSON):
@@ -124,7 +124,7 @@
Called by the `config` module extension (`//python/extensions:config.bzl`).
If `ARTIFACTS_MANIFEST_JSON` from `//python/private:artifacts_manifest.bzl`
is non-empty (as in release archives), creates an `http_file` repo and a
- `prebuilt_exe_zip_maker_toolchain_impl_repo` repo for each entry in the
+ `prebuilt_shrubbery_toolchain_impl_repo` repo for each entry in the
manifest, and registers `toolchain()` declarations in
`@rules_python_tools//toolchains`.
Otherwise (as in git checkouts and source builds), registers the
@@ -133,8 +133,9 @@
Args:
http_file: Repository rule callable for downloading prebuilt tool
binaries (injectable for testing).
- prebuilt_exe_zip_maker_toolchain_impl_repo: Repository rule callable for
- creating `py_exe_zip_maker_toolchain` repos (injectable for testing).
+ prebuilt_shrubbery_toolchain_impl_repo: Repository rule
+ callable for creating `shrubbery_toolchain` repos
+ (injectable for testing).
tools_toolchains_repo: Repository rule callable for creating
`@rules_python_tools` (injectable for testing).
artifacts_manifest_json: The JSON manifest of prebuilt tool binaries,
@@ -146,7 +147,7 @@
exec_compatible_with = {}
if manifest_content:
manifest = json.decode(manifest_content)
- entries = manifest.get("artifacts", {}).get("exe_zip_maker", [])
+ entries = manifest.get("artifacts", {}).get("shrubbery", [])
for i, entry in enumerate(entries):
name = entry["name"]
file_repo_name = "rules_python_" + name + "_file"
@@ -157,9 +158,9 @@
sha256 = entry["sha256"],
urls = entry["urls"],
)
- prebuilt_exe_zip_maker_toolchain_impl_repo(
+ prebuilt_shrubbery_toolchain_impl_repo(
name = toolchain_repo_name,
- exe_zip_maker = "@{}//file".format(file_repo_name),
+ shrubbery = "@{}//file".format(file_repo_name),
)
toolchain_names.append(name)
toolchain_impls.append(
@@ -167,8 +168,8 @@
)
exec_compatible_with[str(i)] = entry["exec_compatible_with"]
else:
- toolchain_names.append("exe_zip_maker_from_source")
- toolchain_impls.append(_FROM_SOURCE_EXE_ZIP_MAKER)
+ toolchain_names.append("shrubbery_from_source")
+ toolchain_impls.append(_FROM_SOURCE_SHRUBBERY)
exec_compatible_with["0"] = []
tools_toolchains_repo(
name = "rules_python_tools",
diff --git a/python/private/toolchains/shrubbery_toolchain.bzl b/python/private/toolchains/shrubbery_toolchain.bzl
new file mode 100644
index 0000000..757d44f
--- /dev/null
+++ b/python/private/toolchains/shrubbery_toolchain.bzl
@@ -0,0 +1,88 @@
+"""Toolchain for the unified Rust shrubbery binary."""
+
+load("//python/private:sentinel_impl.bzl", "SentinelInfo")
+load("//python/private:toolchain_types.bzl", "SHRUBBERY_TOOLCHAIN_TYPE")
+
+def _shrubbery_toolchain_impl(ctx):
+ shrubbery = ctx.attr.shrubbery
+ if shrubbery != None and SentinelInfo in shrubbery:
+ shrubbery = None
+
+ return [
+ platform_common.ToolchainInfo(
+ shrubbery = shrubbery,
+ ),
+ ]
+
+shrubbery_toolchain = rule(
+ implementation = _shrubbery_toolchain_impl,
+ doc = """
+Provides the unified `shrubbery` ("Shell replacements for Bazel build tools in
+Rust") subcommand binary.
+
+This provides `ToolchainInfo` with the following attributes:
+* `shrubbery`: {type}`Target | None`. Invoked as
+ `<binary> <subcommand> [args...]`. Must provide `DefaultInfo.files_to_run`.
+ If `None`, the rules fall back to their built-in Python or script
+ implementations.
+""",
+ attrs = {
+ "shrubbery": attr.label(
+ # NOTE: This is an executable, but can't use `executable = True`
+ # because the `//python:none` sentinel isn't executable.
+ # `allow_files = True` (not `allow_single_file`) because binary
+ # targets can have multiple files in `DefaultInfo.files`.
+ allow_files = True,
+ cfg = "exec",
+ doc = """
+The unified `shrubbery` binary.
+
+To indicate no tool, specify the special target {obj}`//python:none`.
+""",
+ ),
+ },
+)
+
+def get_shrubbery_tool(ctx, subcommand, fallback):
+ """Returns how to run a rules_python build tool subcommand or its fallback.
+
+ Prefers the `shrubbery` binary from the `shrubbery` toolchain, if one is
+ resolved and provides it, prepending `subcommand` to its arguments.
+ Otherwise, falls back to `fallback`.
+
+ Args:
+ ctx: The rule context. The rule must declare
+ `SHRUBBERY_TOOLCHAIN_TYPE` as an optional toolchain.
+ subcommand: {type}`str` Subcommand name passed as `argv[1]` when running
+ `shrubbery`.
+ fallback: {type}`Target` Fallback tool target when no `shrubbery`
+ toolchain is resolved.
+
+ Returns:
+ {type}`struct` with fields:
+ * `executable`: {type}`Target` to pass to `actions_run()`.
+ * `prefix_args`: {type}`list[str]` `[subcommand]` when resolved from
+ the `shrubbery` toolchain, else `[]`.
+ * `toolchain`: {type}`Label | None` the toolchain type to pass to
+ `actions_run()` or `ctx.actions.run()` so the action runs on the exec
+ platform the tool was built for. `None` when falling back to
+ `fallback`.
+ """
+ toolchain = ctx.toolchains[SHRUBBERY_TOOLCHAIN_TYPE]
+ if toolchain and toolchain.shrubbery:
+ return struct(
+ # Target to pass to `actions_run()`.
+ executable = toolchain.shrubbery,
+ # Leading subcommand argument prepended when invoking `shrubbery`.
+ prefix_args = [subcommand],
+ # Toolchain type for `actions_run()` / `ctx.actions.run()`.
+ toolchain = SHRUBBERY_TOOLCHAIN_TYPE,
+ )
+ return struct(
+ # Fallback tool target to pass to `actions_run()`.
+ executable = fallback,
+ # Empty prefix when invoking the standalone fallback tool directly.
+ prefix_args = [],
+ # `None` so fallback tools use the default exec tools or host execution.
+ toolchain = None,
+ )
diff --git a/python/private/zipapp/BUILD.bazel b/python/private/zipapp/BUILD.bazel
index 6da278a..e603ffb 100644
--- a/python/private/zipapp/BUILD.bazel
+++ b/python/private/zipapp/BUILD.bazel
@@ -33,7 +33,6 @@
name = "py_zipapp_rule",
srcs = ["py_zipapp_rule.bzl"],
deps = [
- ":py_exe_zip_maker_toolchain",
"//python/private:attributes",
"//python/private:builders",
"//python/private:common",
@@ -43,17 +42,8 @@
"//python/private:py_runtime_info",
"//python/private:toolchain_types",
"//python/private:transition_labels",
+ "//python/private/toolchains:shrubbery_toolchain",
"@bazel_skylib//lib:paths",
"@rules_python_internal//:rules_python_config",
],
)
-
-bzl_library(
- name = "py_exe_zip_maker_toolchain",
- srcs = ["py_exe_zip_maker_toolchain.bzl"],
- deps = [
- "//python/private:py_interpreter_program",
- "//python/private:sentinel_impl",
- "//python/private:toolchain_types",
- ],
-)
diff --git a/python/private/zipapp/py_exe_zip_maker_toolchain.bzl b/python/private/zipapp/py_exe_zip_maker_toolchain.bzl
deleted file mode 100644
index fb8efe9..0000000
--- a/python/private/zipapp/py_exe_zip_maker_toolchain.bzl
+++ /dev/null
@@ -1,75 +0,0 @@
-"""Toolchain for the tool that creates self-executable zip files."""
-
-load("//python/private:py_interpreter_program.bzl", "PyInterpreterProgramInfo")
-load("//python/private:sentinel_impl.bzl", "SentinelInfo")
-load("//python/private:toolchain_types.bzl", "EXE_ZIP_MAKER_TOOLCHAIN_TYPE")
-
-def _py_exe_zip_maker_toolchain_impl(ctx):
- exe_zip_maker = ctx.attr.exe_zip_maker
- if exe_zip_maker != None and SentinelInfo in exe_zip_maker:
- exe_zip_maker = None
-
- return [
- platform_common.ToolchainInfo(
- exe_zip_maker = exe_zip_maker,
- ),
- ]
-
-py_exe_zip_maker_toolchain = rule(
- implementation = _py_exe_zip_maker_toolchain_impl,
- doc = """Provides the tool used to create self-executable zip files.
-
-This provides `ToolchainInfo` with the following attributes:
-* `exe_zip_maker`: {type}`Target | None`. Invoked with three positional
- arguments: `<preamble> <zip> <output>`. Must provide either
- `PyInterpreterProgramInfo` or `DefaultInfo.files_to_run`. If `None`, the
- rules fall back to their built-in implementation.
-""",
- attrs = {
- "exe_zip_maker": attr.label(
- # NOTE: This is an executable, but can't use `executable = True`
- # because the `//python:none` sentinel isn't executable.
- # `allow_files = True` (not `allow_single_file`) because binary
- # targets can have multiple files in `DefaultInfo.files`.
- allow_files = True,
- cfg = "exec",
- doc = """
-The tool to create self-executable zip files.
-
-To indicate no tool, specify the special target {obj}`//python:none`.
-""",
- ),
- },
-)
-
-def get_exe_zip_maker(ctx):
- """Returns how to run the tool for creating self-executable zips.
-
- Prefers the tool from the `exe_zip_maker` toolchain, if one is resolved
- and provides it. Otherwise, falls back to the rule's `_exe_zip_maker`
- attribute.
-
- Args:
- ctx: The rule context. The rule must declare
- `EXE_ZIP_MAKER_TOOLCHAIN_TYPE` as an optional toolchain and have
- an `_exe_zip_maker` attribute.
-
- Returns:
- {type}`struct` with fields:
- * `executable`: {type}`Target` to pass to `actions_run()`.
- * `toolchain`: {type}`Label | None` the toolchain type to pass to
- `actions_run()` so the action runs on the exec platform the tool
- was built for. `None` when the tool is a `py_interpreter_program`
- (`actions_run` then uses the exec tools toolchain) or when falling
- back to the attribute.
- """
- toolchain = ctx.toolchains[EXE_ZIP_MAKER_TOOLCHAIN_TYPE]
- if toolchain and toolchain.exe_zip_maker:
- executable = toolchain.exe_zip_maker
- if PyInterpreterProgramInfo in executable:
- return struct(executable = executable, toolchain = None)
- return struct(
- executable = executable,
- toolchain = EXE_ZIP_MAKER_TOOLCHAIN_TYPE,
- )
- return struct(executable = ctx.attr._exe_zip_maker, toolchain = None)
diff --git a/python/private/zipapp/py_zipapp_rule.bzl b/python/private/zipapp/py_zipapp_rule.bzl
index ebf083c..06ed25a 100644
--- a/python/private/zipapp/py_zipapp_rule.bzl
+++ b/python/private/zipapp/py_zipapp_rule.bzl
@@ -18,9 +18,9 @@
load("//python/private:py_executable_info.bzl", "PyExecutableInfo")
load("//python/private:py_internal.bzl", "py_internal")
load("//python/private:py_runtime_info.bzl", "PyRuntimeInfo")
-load("//python/private:toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE", "EXE_ZIP_MAKER_TOOLCHAIN_TYPE", "LAUNCHER_MAKER_TOOLCHAIN_TYPE")
+load("//python/private:toolchain_types.bzl", "EXEC_TOOLS_TOOLCHAIN_TYPE", "LAUNCHER_MAKER_TOOLCHAIN_TYPE", "SHRUBBERY_TOOLCHAIN_TYPE")
load("//python/private:transition_labels.bzl", "TRANSITION_LABELS")
-load(":py_exe_zip_maker_toolchain.bzl", "get_exe_zip_maker")
+load("//python/private/toolchains:shrubbery_toolchain.bzl", "get_shrubbery_tool")
def _is_symlink(f):
if hasattr(f, "is_symlink"):
@@ -226,12 +226,16 @@
args.add(preamble)
args.add(zip_file)
args.add(pyz)
- exe_zip_maker = get_exe_zip_maker(ctx)
+ exe_zip_maker = get_shrubbery_tool(
+ ctx,
+ "exe_zip_maker",
+ ctx.attr._exe_zip_maker,
+ )
actions_run(
ctx,
executable = exe_zip_maker.executable,
toolchain = exe_zip_maker.toolchain,
- arguments = [args],
+ arguments = exe_zip_maker.prefix_args + [args],
inputs = depset([preamble, zip_file]),
outputs = [pyz],
mnemonic = "PyZipAppCreateExecutableZip",
@@ -401,7 +405,7 @@
_TOOLCHAINS = [
EXEC_TOOLS_TOOLCHAIN_TYPE,
- config_common.toolchain_type(EXE_ZIP_MAKER_TOOLCHAIN_TYPE, mandatory = False),
+ config_common.toolchain_type(SHRUBBERY_TOOLCHAIN_TYPE, mandatory = False),
] + ([LAUNCHER_MAKER_TOOLCHAIN_TYPE] if rp_config.bazel_9_or_later else [])
_COMMON_RULE_DOC = """
diff --git a/tests/exe_zip_maker/exe_zip_maker_test.rs b/tests/exe_zip_maker/exe_zip_maker_test.rs
index dcd6f94..d521a11 100644
--- a/tests/exe_zip_maker/exe_zip_maker_test.rs
+++ b/tests/exe_zip_maker/exe_zip_maker_test.rs
@@ -1,9 +1,7 @@
use std::env;
use std::fs;
-use exe_zip_maker_lib::{
- compute_file_sha256_hex, create_exe_zip, replace_bytes, PLACEHOLDER,
-};
+use exe_zip_maker_lib::{compute_file_sha256_hex, create_exe_zip, replace_bytes, PLACEHOLDER};
#[test]
fn test_replace_bytes_none() {
diff --git a/tests/exe_zip_maker_toolchain/BUILD.bazel b/tests/exe_zip_maker_toolchain/BUILD.bazel
deleted file mode 100644
index 57f2b55..0000000
--- a/tests/exe_zip_maker_toolchain/BUILD.bazel
+++ /dev/null
@@ -1,5 +0,0 @@
-load(":exe_zip_maker_toolchain_tests.bzl", "exe_zip_maker_toolchain_test_suite")
-
-exe_zip_maker_toolchain_test_suite(
- name = "exe_zip_maker_toolchain_tests",
-)
diff --git a/tests/integration/BUILD.bazel b/tests/integration/BUILD.bazel
index 79a8bac..950de14 100644
--- a/tests/integration/BUILD.bazel
+++ b/tests/integration/BUILD.bazel
@@ -117,7 +117,7 @@
rules_python_integration_test(
name = "prebuilt_toolchain_test",
- py_data = ["//crates/exe_zip_maker"],
+ py_data = ["//crates/shrubbery"],
py_deps = ["//python/runfiles"],
py_main = "prebuilt_toolchain_test.py",
target_compatible_with = SUPPORTS_BZLMOD + NOT_WINDOWS,
diff --git a/tests/integration/prebuilt_toolchain_test.py b/tests/integration/prebuilt_toolchain_test.py
index 28f31db..453ae5b 100644
--- a/tests/integration/prebuilt_toolchain_test.py
+++ b/tests/integration/prebuilt_toolchain_test.py
@@ -8,21 +8,19 @@
from python.runfiles import runfiles
from tests.integration import runner
-_USE_RUST_FLAG = "--@rules_python//python/private/toolchains:use_rust_exe_zip_maker=yes"
+_NEED_SHRUBBERY_FLAG = "--@rules_python//python/private/toolchains:need_shrubbery=yes"
class PrebuiltToolchainTest(runner.TestCase):
def setUp(self):
super().setUp()
rf = runfiles.CreateOrRaise()
- exe_zip_maker = (
- rf.root() / "rules_python/crates/exe_zip_maker/exe_zip_maker"
- ).resolve()
+ shrubbery = (rf.root() / "rules_python/crates/shrubbery/shrubbery").resolve()
self.assertTrue(
- exe_zip_maker.is_file(),
- f"Expected exe_zip_maker binary at {exe_zip_maker}",
+ shrubbery.is_file(),
+ f"Expected shrubbery binary at {shrubbery}",
)
- sha256 = hashlib.sha256(exe_zip_maker.read_bytes()).hexdigest()
+ sha256 = hashlib.sha256(shrubbery.read_bytes()).hexdigest()
orig_rules_python = self.repo_root.parent.parent.parent
self.rules_python_dir = self.test_tmp_dir / "rules_python"
@@ -48,20 +46,20 @@
manifest = {
"artifacts": {
- "exe_zip_maker": [
+ "shrubbery": [
{
"exec_compatible_with": [
"@platforms//os:windows",
],
- "name": "exe_zip_maker_unmatched",
+ "name": "shrubbery_unmatched",
"sha256": "0" * 64,
"urls": ["https://invalid.example.com/should-not-be-fetched"],
},
{
"exec_compatible_with": [],
- "name": "exe_zip_maker_prebuilt",
+ "name": "shrubbery_prebuilt",
"sha256": sha256,
- "urls": [exe_zip_maker.as_uri()],
+ "urls": [shrubbery.as_uri()],
},
],
},
@@ -72,25 +70,25 @@
encoding="utf-8",
)
- def test_prebuilt_exe_zip_maker_toolchain(self):
+ def test_prebuilt_shrubbery_toolchain(self):
override_flag = f"--override_module=rules_python={self.rules_python_dir}"
self.run_bazel(
"test",
override_flag,
- _USE_RUST_FLAG,
+ _NEED_SHRUBBERY_FLAG,
"//:zipapp_test",
)
aquery_res = self.run_bazel(
"aquery",
override_flag,
- _USE_RUST_FLAG,
+ _NEED_SHRUBBERY_FLAG,
"mnemonic(PyZipAppCreateExecutableZip, //:zipapp_test)",
)
self.assert_result_matches(
aquery_res,
- r"rules_python_exe_zip_maker_prebuilt_file/file/downloaded",
+ r"rules_python_shrubbery_prebuilt_file/file/downloaded",
)
diff --git a/tests/shrubbery/BUILD.bazel b/tests/shrubbery/BUILD.bazel
new file mode 100644
index 0000000..de800e5
--- /dev/null
+++ b/tests/shrubbery/BUILD.bazel
@@ -0,0 +1,20 @@
+load("@rules_rust//rust:defs.bzl", "rust_test")
+
+package(default_visibility = ["//:__subpackages__"])
+
+licenses(["notice"])
+
+rust_test(
+ name = "shrubbery_test",
+ size = "small",
+ srcs = ["shrubbery_test.rs"],
+ data = [
+ "//crates/shrubbery",
+ ],
+ env = {
+ "SHRUBBERY_BIN": "$(rootpath //crates/shrubbery)",
+ },
+ deps = [
+ "//crates/shrubbery:shrubbery_lib",
+ ],
+)
diff --git a/tests/shrubbery/shrubbery_test.rs b/tests/shrubbery/shrubbery_test.rs
new file mode 100644
index 0000000..e10de69
--- /dev/null
+++ b/tests/shrubbery/shrubbery_test.rs
@@ -0,0 +1,89 @@
+use std::env;
+use std::fs;
+use std::path::PathBuf;
+use std::process::Command;
+
+use shrubbery_lib::{run_cli, SUBCOMMANDS};
+
+fn temp_test_dir(prefix: &str) -> PathBuf {
+ let base = env::var_os("TEST_TMPDIR")
+ .map(PathBuf::from)
+ .unwrap_or_else(env::temp_dir);
+ let dir = base.join(format!("shrubbery_{}_{}", prefix, std::process::id()));
+ let _ = fs::remove_dir_all(&dir);
+ fs::create_dir_all(&dir).unwrap();
+ dir
+}
+
+#[test]
+fn test_subcommands_registry_sorted_and_complete() {
+ assert_eq!(SUBCOMMANDS.len(), 1);
+ let mut sorted = SUBCOMMANDS.to_vec();
+ sorted.sort_unstable();
+ assert_eq!(SUBCOMMANDS, sorted.as_slice());
+}
+
+#[test]
+fn test_help_flags_exit_zero() {
+ assert_eq!(run_cli(["shrubbery", "--help"]), 0);
+ assert_eq!(run_cli(["shrubbery", "-h"]), 0);
+ assert_eq!(run_cli(["shrubbery", "help"]), 0);
+}
+
+#[test]
+fn test_missing_or_unknown_subcommand_exits_one() {
+ assert_eq!(run_cli(["shrubbery"]), 1);
+ assert_eq!(run_cli(["shrubbery", "does_not_exist"]), 1);
+}
+
+#[test]
+fn test_subcommand_forwarding() {
+ let dir = temp_test_dir("dispatch");
+ let preamble = dir.join("preamble.sh");
+ let zip_in = dir.join("in.zip");
+ let out_pyz = dir.join("out.pyz");
+
+ fs::write(&preamble, b"#!/bin/sh\n").unwrap();
+ fs::write(
+ &zip_in,
+ b"PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
+ )
+ .unwrap();
+
+ assert_eq!(
+ run_cli([
+ "shrubbery",
+ "exe_zip_maker",
+ preamble.to_str().unwrap(),
+ zip_in.to_str().unwrap(),
+ out_pyz.to_str().unwrap(),
+ ]),
+ 0
+ );
+ assert!(out_pyz.exists());
+}
+
+#[test]
+fn test_cli_binary_subprocess() {
+ let resolved = PathBuf::from(
+ env::var_os("SHRUBBERY_BIN").expect("SHRUBBERY_BIN must be set"),
+ );
+
+ let help_out = Command::new(&resolved).arg("--help").output().unwrap();
+ assert_eq!(help_out.status.code(), Some(0));
+ let stdout = String::from_utf8_lossy(&help_out.stdout);
+ for sub in SUBCOMMANDS {
+ assert!(stdout.contains(sub), "missing {} in help: {}", sub, stdout);
+ }
+
+ let bad_out = Command::new(&resolved)
+ .arg("not_a_subcommand")
+ .output()
+ .unwrap();
+ assert_eq!(bad_out.status.code(), Some(1));
+ assert!(
+ String::from_utf8_lossy(&bad_out.stderr).contains("unknown subcommand"),
+ "unexpected stderr: {}",
+ String::from_utf8_lossy(&bad_out.stderr)
+ );
+}
diff --git a/tests/shrubbery_toolchain/BUILD.bazel b/tests/shrubbery_toolchain/BUILD.bazel
new file mode 100644
index 0000000..9da7c31
--- /dev/null
+++ b/tests/shrubbery_toolchain/BUILD.bazel
@@ -0,0 +1,5 @@
+load(":shrubbery_toolchain_tests.bzl", "shrubbery_toolchain_test_suite")
+
+shrubbery_toolchain_test_suite(
+ name = "shrubbery_toolchain_tests",
+)
diff --git a/tests/exe_zip_maker_toolchain/main.py b/tests/shrubbery_toolchain/main.py
similarity index 100%
rename from tests/exe_zip_maker_toolchain/main.py
rename to tests/shrubbery_toolchain/main.py
diff --git a/tests/exe_zip_maker_toolchain/exe_zip_maker_toolchain_tests.bzl b/tests/shrubbery_toolchain/shrubbery_toolchain_tests.bzl
similarity index 64%
rename from tests/exe_zip_maker_toolchain/exe_zip_maker_toolchain_tests.bzl
rename to tests/shrubbery_toolchain/shrubbery_toolchain_tests.bzl
index 1e7fd19..6b336ce 100644
--- a/tests/exe_zip_maker_toolchain/exe_zip_maker_toolchain_tests.bzl
+++ b/tests/shrubbery_toolchain/shrubbery_toolchain_tests.bzl
@@ -1,4 +1,4 @@
-"""Tests for the exe_zip_maker toolchain configuration and registration."""
+"""Tests for the shrubbery toolchain configuration and registration."""
load("@rules_testing//lib:analysis_test.bzl", "analysis_test")
load("@rules_testing//lib:test_suite.bzl", "test_suite")
@@ -7,7 +7,7 @@
load("@rules_testing//lib:util.bzl", rt_util = "util")
load("//python:py_binary.bzl", "py_binary")
load("//python/private/toolchains:repo.bzl", "configure_tool_toolchains") # buildifier: disable=bzl-visibility
-load("//python/private/zipapp:py_exe_zip_maker_toolchain.bzl", "py_exe_zip_maker_toolchain") # buildifier: disable=bzl-visibility
+load("//python/private/toolchains:shrubbery_toolchain.bzl", "shrubbery_toolchain") # buildifier: disable=bzl-visibility
load("//python/zipapp:py_zipapp_binary.bzl", "py_zipapp_binary")
load("//tests/support:support.bzl", "NOT_WINDOWS", "SUPPORTS_BZLMOD")
load("//tests/support/mocks:mocks.bzl", "mocks")
@@ -15,19 +15,20 @@
_tests = []
# Under bzlmod, MODULE.bazel registers @rules_python_tools//toolchains:all,
-# which points to the Rust implementation (from source in git, or prebuilt in
-# release archives) when use_rust_exe_zip_maker is "auto" (in dev mode) or
-# "yes", and falls back to the Python implementation when "no".
+# which points to the unified Rust shrubbery binary (from source in git, or
+# prebuilt in release archives) when need_shrubbery is "auto" (in dev mode) or
+# "yes", and falls back to the built-in Python/script tools when "no".
#
# In WORKSPACE mode, rules_rust is a stub and the Python fallback is used, so
# skip there.
#
# On Windows, py_zipapp uses the Bazel launcher instead of a self-executable
-# zip, so the exe_zip_maker toolchain is never used; skip there too.
+# zip, so PyZipAppCreateExecutableZip is not created; skip there for zipapp
+# self-executable tests.
_COMPATIBLE_WITH = SUPPORTS_BZLMOD + NOT_WINDOWS
-_RUST_EXE_ZIP_MAKER_SUFFIX = "crates/exe_zip_maker/exe_zip_maker"
+_SHRUBBERY_SUFFIX = "crates/shrubbery/shrubbery"
_PYTHON_EXE_ZIP_MAKER_SUFFIX = "tools/zipapp/exe_zip_maker_.py"
-_USE_RUST_FLAG = str(Label("//python/private/toolchains:use_rust_exe_zip_maker"))
+_NEED_SHRUBBERY_FLAG = str(Label("//python/private/toolchains:need_shrubbery"))
def _define_py_zipapp_subject(name):
rt_util.helper_target(
@@ -42,52 +43,53 @@
binary = name + "_bin",
)
-def _test_py_zipapp_default_uses_toolchain_exe_zip_maker(name):
+def _test_py_zipapp_default_uses_toolchain_shrubbery(name):
_define_py_zipapp_subject(name)
analysis_test(
name = name,
- impl = _test_py_zipapp_uses_toolchain_exe_zip_maker_impl,
+ impl = _test_py_zipapp_uses_toolchain_shrubbery_impl,
target = name + "_subject",
attr_values = {"target_compatible_with": _COMPATIBLE_WITH},
)
-_tests.append(_test_py_zipapp_default_uses_toolchain_exe_zip_maker)
+_tests.append(_test_py_zipapp_default_uses_toolchain_shrubbery)
-def _test_py_zipapp_uses_toolchain_exe_zip_maker(name):
+def _test_py_zipapp_uses_toolchain_shrubbery(name):
_define_py_zipapp_subject(name)
analysis_test(
name = name,
- impl = _test_py_zipapp_uses_toolchain_exe_zip_maker_impl,
+ impl = _test_py_zipapp_uses_toolchain_shrubbery_impl,
target = name + "_subject",
config_settings = {
- _USE_RUST_FLAG: "yes",
+ _NEED_SHRUBBERY_FLAG: "yes",
},
attr_values = {"target_compatible_with": _COMPATIBLE_WITH},
)
-def _test_py_zipapp_uses_toolchain_exe_zip_maker_impl(env, target):
+def _test_py_zipapp_uses_toolchain_shrubbery_impl(env, target):
action = env.expect.that_target(target).action_named(
"PyZipAppCreateExecutableZip",
)
action.argv().contains_predicate(
- matching.str_endswith(_RUST_EXE_ZIP_MAKER_SUFFIX),
+ matching.str_endswith(_SHRUBBERY_SUFFIX),
)
+ action.argv().contains("exe_zip_maker")
-_tests.append(_test_py_zipapp_uses_toolchain_exe_zip_maker)
+_tests.append(_test_py_zipapp_uses_toolchain_shrubbery)
-def _test_py_zipapp_flag_disabled_uses_python_exe_zip_maker(name):
+def _test_py_zipapp_flag_disabled_uses_python_fallbacks(name):
_define_py_zipapp_subject(name)
analysis_test(
name = name,
- impl = _test_py_zipapp_flag_disabled_uses_python_exe_zip_maker_impl,
+ impl = _test_py_zipapp_flag_disabled_uses_python_fallbacks_impl,
target = name + "_subject",
config_settings = {
- _USE_RUST_FLAG: "no",
+ _NEED_SHRUBBERY_FLAG: "no",
},
attr_values = {"target_compatible_with": _COMPATIBLE_WITH},
)
-def _test_py_zipapp_flag_disabled_uses_python_exe_zip_maker_impl(env, target):
+def _test_py_zipapp_flag_disabled_uses_python_fallbacks_impl(env, target):
action = env.expect.that_target(target).action_named(
"PyZipAppCreateExecutableZip",
)
@@ -95,13 +97,13 @@
matching.str_endswith(_PYTHON_EXE_ZIP_MAKER_SUFFIX),
)
-_tests.append(_test_py_zipapp_flag_disabled_uses_python_exe_zip_maker)
+_tests.append(_test_py_zipapp_flag_disabled_uses_python_fallbacks)
def _test_prebuilt_file_toolchain_target(name):
rt_util.helper_target(
- py_exe_zip_maker_toolchain,
+ shrubbery_toolchain,
name = name + "_subject",
- exe_zip_maker = "main.py",
+ shrubbery = "main.py",
)
analysis_test(
name = name,
@@ -111,9 +113,9 @@
def _test_prebuilt_file_toolchain_target_impl(env, target):
tc_info = target[platform_common.ToolchainInfo]
- exe = tc_info.exe_zip_maker[DefaultInfo].files_to_run.executable
+ exe = tc_info.shrubbery[DefaultInfo].files_to_run.executable
env.expect.that_file(exe).short_path_equals(
- "tests/exe_zip_maker_toolchain/main.py",
+ "tests/shrubbery_toolchain/main.py",
)
_tests.append(_test_prebuilt_file_toolchain_target)
@@ -130,7 +132,7 @@
tools_toolchains_repo = mocks.repo()
configure_tool_toolchains(
http_file = http_file.rule,
- prebuilt_exe_zip_maker_toolchain_impl_repo = toolchain_repo.rule,
+ prebuilt_shrubbery_toolchain_impl_repo = toolchain_repo.rule,
tools_toolchains_repo = tools_toolchains_repo.rule,
artifacts_manifest_json = "\n",
)
@@ -141,10 +143,10 @@
"exec_compatible_with": {"0": []},
"name": "rules_python_tools",
"toolchain_impls": [
- "@rules_python//python/private/toolchains/from_source:exe_zip_maker",
+ "@rules_python//python/private/toolchains/from_source:shrubbery",
],
"toolchain_names": [
- "exe_zip_maker_from_source",
+ "shrubbery_from_source",
],
},
])
@@ -160,24 +162,24 @@
def _test_configure_tool_toolchains_prebuilt_impl(env):
manifest_json = json.encode({
"artifacts": {
- "exe_zip_maker": [
+ "shrubbery": [
{
"exec_compatible_with": [
"@platforms//cpu:aarch64",
"@platforms//os:macos",
],
- "name": "exe_zip_maker_aarch64_apple_darwin",
+ "name": "shrubbery_aarch64_apple_darwin",
"sha256": "deadbeef1",
- "urls": ["https://example.com/exe_zip_maker-aarch64-apple-darwin"],
+ "urls": ["https://example.com/shrubbery-aarch64-apple-darwin"],
},
{
"exec_compatible_with": [
"@platforms//cpu:x86_64",
"@platforms//os:windows",
],
- "name": "exe_zip_maker_x86_64_pc_windows_msvc",
+ "name": "shrubbery_x86_64_pc_windows_msvc",
"sha256": "deadbeef2",
- "urls": ["https://example.com/exe_zip_maker-x86_64-pc-windows-msvc.exe"],
+ "urls": ["https://example.com/shrubbery-x86_64-pc-windows-msvc.exe"],
},
],
},
@@ -187,32 +189,32 @@
tools_toolchains_repo = mocks.repo()
configure_tool_toolchains(
http_file = http_file.rule,
- prebuilt_exe_zip_maker_toolchain_impl_repo = toolchain_repo.rule,
+ prebuilt_shrubbery_toolchain_impl_repo = toolchain_repo.rule,
tools_toolchains_repo = tools_toolchains_repo.rule,
artifacts_manifest_json = manifest_json,
)
env.expect.that_collection(http_file.calls).contains_exactly([
{
"executable": True,
- "name": "rules_python_exe_zip_maker_aarch64_apple_darwin_file",
+ "name": "rules_python_shrubbery_aarch64_apple_darwin_file",
"sha256": "deadbeef1",
- "urls": ["https://example.com/exe_zip_maker-aarch64-apple-darwin"],
+ "urls": ["https://example.com/shrubbery-aarch64-apple-darwin"],
},
{
"executable": True,
- "name": "rules_python_exe_zip_maker_x86_64_pc_windows_msvc_file",
+ "name": "rules_python_shrubbery_x86_64_pc_windows_msvc_file",
"sha256": "deadbeef2",
- "urls": ["https://example.com/exe_zip_maker-x86_64-pc-windows-msvc.exe"],
+ "urls": ["https://example.com/shrubbery-x86_64-pc-windows-msvc.exe"],
},
])
env.expect.that_collection(toolchain_repo.calls).contains_exactly([
{
- "exe_zip_maker": "@rules_python_exe_zip_maker_aarch64_apple_darwin_file//file",
- "name": "rules_python_exe_zip_maker_aarch64_apple_darwin_toolchain",
+ "name": "rules_python_shrubbery_aarch64_apple_darwin_toolchain",
+ "shrubbery": "@rules_python_shrubbery_aarch64_apple_darwin_file//file",
},
{
- "exe_zip_maker": "@rules_python_exe_zip_maker_x86_64_pc_windows_msvc_file//file",
- "name": "rules_python_exe_zip_maker_x86_64_pc_windows_msvc_toolchain",
+ "name": "rules_python_shrubbery_x86_64_pc_windows_msvc_toolchain",
+ "shrubbery": "@rules_python_shrubbery_x86_64_pc_windows_msvc_file//file",
},
])
env.expect.that_collection(tools_toolchains_repo.calls).contains_exactly([
@@ -229,19 +231,19 @@
},
"name": "rules_python_tools",
"toolchain_impls": [
- "@rules_python_exe_zip_maker_aarch64_apple_darwin_toolchain//:toolchain",
- "@rules_python_exe_zip_maker_x86_64_pc_windows_msvc_toolchain//:toolchain",
+ "@rules_python_shrubbery_aarch64_apple_darwin_toolchain//:toolchain",
+ "@rules_python_shrubbery_x86_64_pc_windows_msvc_toolchain//:toolchain",
],
"toolchain_names": [
- "exe_zip_maker_aarch64_apple_darwin",
- "exe_zip_maker_x86_64_pc_windows_msvc",
+ "shrubbery_aarch64_apple_darwin",
+ "shrubbery_x86_64_pc_windows_msvc",
],
},
])
_tests.append(_test_configure_tool_toolchains_prebuilt)
-def exe_zip_maker_toolchain_test_suite(name):
+def shrubbery_toolchain_test_suite(name):
test_suite(
name = name,
tests = _tests,
diff --git a/tests/workflows/release_artifacts_test.py b/tests/workflows/release_artifacts_test.py
index 95637c0..49a0400 100644
--- a/tests/workflows/release_artifacts_test.py
+++ b/tests/workflows/release_artifacts_test.py
@@ -23,15 +23,15 @@
manifest_json_file = tmp_path / "artifacts_manifest.json"
manifest_bzl_file = tmp_path / "artifacts_manifest.bzl"
- linux_bin = artifacts_dir / "exe_zip_maker-x86_64-unknown-linux-musl"
+ linux_bin = artifacts_dir / "shrubbery-x86_64-unknown-linux-musl"
linux_bin.write_bytes(b"linux-binary-contents")
linux_sha = hashlib.sha256(b"linux-binary-contents").hexdigest()
- win_bin = artifacts_dir / "exe_zip_maker-x86_64-pc-windows-msvc.exe"
+ win_bin = artifacts_dir / "shrubbery-x86_64-pc-windows-msvc.exe"
win_bin.write_bytes(b"windows-binary-contents")
win_sha = hashlib.sha256(b"windows-binary-contents").hexdigest()
- mac_bin = artifacts_dir / "exe_zip_maker-aarch64-apple-darwin"
+ mac_bin = artifacts_dir / "shrubbery-aarch64-apple-darwin"
mac_bin.write_bytes(b"mac-binary-contents")
mac_sha = hashlib.sha256(b"mac-binary-contents").hexdigest()
@@ -56,33 +56,33 @@
base_url = "https://github.com/bazel-contrib/rules_python/releases/download/2.4.0"
expected_manifest = {
"artifacts": {
- "exe_zip_maker": [
+ "shrubbery": [
{
"exec_compatible_with": [
"@platforms//cpu:aarch64",
"@platforms//os:macos",
],
- "name": "exe_zip_maker_aarch64_apple_darwin",
+ "name": "shrubbery_aarch64_apple_darwin",
"sha256": mac_sha,
- "urls": [f"{base_url}/exe_zip_maker-aarch64-apple-darwin"],
+ "urls": [f"{base_url}/shrubbery-aarch64-apple-darwin"],
},
{
"exec_compatible_with": [
"@platforms//cpu:x86_64",
"@platforms//os:windows",
],
- "name": "exe_zip_maker_x86_64_pc_windows_msvc",
+ "name": "shrubbery_x86_64_pc_windows_msvc",
"sha256": win_sha,
- "urls": [f"{base_url}/exe_zip_maker-x86_64-pc-windows-msvc.exe"],
+ "urls": [f"{base_url}/shrubbery-x86_64-pc-windows-msvc.exe"],
},
{
"exec_compatible_with": [
"@platforms//cpu:x86_64",
"@platforms//os:linux",
],
- "name": "exe_zip_maker_x86_64_unknown_linux_musl",
+ "name": "shrubbery_x86_64_unknown_linux_musl",
"sha256": linux_sha,
- "urls": [f"{base_url}/exe_zip_maker-x86_64-unknown-linux-musl"],
+ "urls": [f"{base_url}/shrubbery-x86_64-unknown-linux-musl"],
},
]
}