From 15b5ccea2e54b5299fcb742c78e8ce35711a37bc Mon Sep 17 00:00:00 2001 From: Matt Mackay Date: Sat, 1 Mar 2025 19:45:47 -0500 Subject: [PATCH] fix: spill module mapping args to a file --- CHANGELOG.md | 2 ++ gazelle/modules_mapping/def.bzl | 15 +++++++++++---- gazelle/modules_mapping/generator.py | 3 +++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 849b458745..8eaac3d9cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,8 @@ Unreleased changes template. is now the default. Note that running as root may still cause spurious Bazel cache invalidation ([#1169](https://github.com/bazelbuild/rules_python/issues/1169)). +* (gazelle) Don't collapse depsets to a list or into args when generating the modules mapping file. + Support spilling modules mapping args into a params file. {#v0-0-0-added} ### Added diff --git a/gazelle/modules_mapping/def.bzl b/gazelle/modules_mapping/def.bzl index eb17f5c3d4..48a5477b93 100644 --- a/gazelle/modules_mapping/def.bzl +++ b/gazelle/modules_mapping/def.bzl @@ -25,18 +25,25 @@ module name doesn't match the wheel distribution name. def _modules_mapping_impl(ctx): modules_mapping = ctx.actions.declare_file(ctx.attr.modules_mapping_name) - args = ctx.actions.args() all_wheels = depset( [whl for whl in ctx.files.wheels], transitive = [dep[DefaultInfo].files for dep in ctx.attr.wheels] + [dep[DefaultInfo].data_runfiles.files for dep in ctx.attr.wheels], ) - args.add("--output_file", modules_mapping.path) + + args = ctx.actions.args() + + # Spill parameters to a file prefixed with '@'. Note, the '@' prefix is the same + # prefix as used in the `generator.py` in `fromfile_prefix_chars` attribute. + args.use_param_file(param_file_arg = "@%s") + args.set_param_file_format(format = "multiline") if ctx.attr.include_stub_packages: args.add("--include_stub_packages") + args.add("--output_file", modules_mapping) args.add_all("--exclude_patterns", ctx.attr.exclude_patterns) - args.add_all("--wheels", [whl.path for whl in all_wheels.to_list()]) + args.add_all("--wheels", all_wheels) + ctx.actions.run( - inputs = all_wheels.to_list(), + inputs = all_wheels, outputs = [modules_mapping], executable = ctx.executable._generator, arguments = [args], diff --git a/gazelle/modules_mapping/generator.py b/gazelle/modules_mapping/generator.py index 99f565e8d6..d5ddca2ef2 100644 --- a/gazelle/modules_mapping/generator.py +++ b/gazelle/modules_mapping/generator.py @@ -152,6 +152,9 @@ def data_has_purelib_or_platlib(path): parser = argparse.ArgumentParser( prog="generator", description="Generates the modules mapping used by the Gazelle manifest.", + # Automatically read parameters from a file. Note, the '@' is the same prefix + # as set in the 'args.use_param_file' in the bazel rule. + fromfile_prefix_chars="@", ) parser.add_argument("--output_file", type=str) parser.add_argument("--include_stub_packages", action="store_true")