From 9b8b29be9cb2f09fb62d4e61da00cd586eb32840 Mon Sep 17 00:00:00 2001 From: Artemy Granat Date: Sun, 28 Jun 2026 22:54:20 +0300 Subject: [PATCH 1/5] fix: add explicit `data` attribute to `compile_pip_requirements` and forward it to the generated `py_binary` --- news/3858.fixed.md | 3 +++ python/private/pypi/pip_compile.bzl | 18 ++++++++++++++++-- .../compile_pip_requirements/BUILD.bazel | 11 +++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 news/3858.fixed.md diff --git a/news/3858.fixed.md b/news/3858.fixed.md new file mode 100644 index 0000000000..dd991e4654 --- /dev/null +++ b/news/3858.fixed.md @@ -0,0 +1,3 @@ +(compile_pip_requirements) Add the explicit `data` attribute and forward it +directly to the generated `py_binary`, so files passed via `data` can be +referenced from `extra_args` using `$(location ...)`. diff --git a/python/private/pypi/pip_compile.bzl b/python/private/pypi/pip_compile.bzl index 3ef2cdb39c..df512a53d0 100644 --- a/python/private/pypi/pip_compile.bzl +++ b/python/private/pypi/pip_compile.bzl @@ -22,6 +22,16 @@ make it possible to have multiple tools inside the `pypi` directory load("//python:py_binary.bzl", _py_binary = "py_binary") load("//python:py_test.bzl", _py_test = "py_test") +# The `data` attribute does not allow duplicate labels, but user-provided `data` +# can overlap with labels added from attributes like `src`. +def _dedupe_data(data): + res = [] + for d in data: + if d not in res: + res.append(d) + return res + + def pip_compile( name, srcs = None, @@ -39,6 +49,7 @@ def pip_compile( visibility = ["//visibility:private"], tags = None, constraints = [], + data = [], **kwargs): """Generates targets for managing pip dependencies with pip-compile (piptools). @@ -81,6 +92,7 @@ def pip_compile( tags: tagging attribute common to all build rules, passed to both the _test and .update rules. visibility: passed to both the _test and .update rules. constraints: a list of files containing constraints to pass to pip-compile with `--constraint`. + data: A list of labels to include as part of the `data` attribute in the generated `py_binary`. **kwargs: other bazel attributes passed to the "_test" rule. """ if len([x for x in [srcs, src, requirements_in] if x != None]) > 1: @@ -95,16 +107,18 @@ def pip_compile( requirements_txt = name + ".txt" if requirements_txt == None else requirements_txt + data = data or [] + # "Default" target produced by this macro # Allow a compile_pip_requirements rule to include another one in the data # for a requirements file that does `-r ../other/requirements.txt` native.filegroup( name = name, - srcs = kwargs.pop("data", []) + [requirements_txt], + srcs = data + [requirements_txt], visibility = visibility, ) - data = [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] + constraints + data = _dedupe_data(data + [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] + constraints) # Use the Label constructor so this is expanded in the context of the file # where it appears, which is to say, in @rules_python diff --git a/tests/integration/compile_pip_requirements/BUILD.bazel b/tests/integration/compile_pip_requirements/BUILD.bazel index 6df46b8372..7a96eccc47 100644 --- a/tests/integration/compile_pip_requirements/BUILD.bazel +++ b/tests/integration/compile_pip_requirements/BUILD.bazel @@ -29,6 +29,17 @@ compile_pip_requirements( requirements_txt = "requirements_lock.txt", ) +genquery( + name = "requirements_update_data", + expression = """ +some(labels(data, //:requirements.update) intersect //:requirements.in) union +some(labels(data, //:requirements.update) intersect //:requirements_extra.in) +""", + scope = [ + ":requirements.update", + ], +) + compile_pip_requirements( name = "requirements_nohashes", src = "requirements.txt", From 6a7d65ca20d0a887821f8ece9867b5dc69c0cad6 Mon Sep 17 00:00:00 2001 From: Artemy Granat Date: Mon, 6 Jul 2026 17:38:59 +0300 Subject: [PATCH 2/5] fix formatting and failing tests --- python/private/pypi/pip_compile.bzl | 1 - tests/integration/compile_pip_requirements/BUILD.bazel | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/python/private/pypi/pip_compile.bzl b/python/private/pypi/pip_compile.bzl index df512a53d0..58f7ba3a59 100644 --- a/python/private/pypi/pip_compile.bzl +++ b/python/private/pypi/pip_compile.bzl @@ -31,7 +31,6 @@ def _dedupe_data(data): res.append(d) return res - def pip_compile( name, srcs = None, diff --git a/tests/integration/compile_pip_requirements/BUILD.bazel b/tests/integration/compile_pip_requirements/BUILD.bazel index 7a96eccc47..d0fa32a3e5 100644 --- a/tests/integration/compile_pip_requirements/BUILD.bazel +++ b/tests/integration/compile_pip_requirements/BUILD.bazel @@ -32,8 +32,8 @@ compile_pip_requirements( genquery( name = "requirements_update_data", expression = """ -some(labels(data, //:requirements.update) intersect //:requirements.in) union -some(labels(data, //:requirements.update) intersect //:requirements_extra.in) +some(labels(data, :requirements.update) intersect :requirements.in) union +some(labels(data, :requirements.update) intersect :requirements_extra.in) """, scope = [ ":requirements.update", From 2cac45649ad39af7379a4a247e3f92d2325dafbe Mon Sep 17 00:00:00 2001 From: Artemy Granat Date: Mon, 6 Jul 2026 17:45:23 +0300 Subject: [PATCH 3/5] trying to fix test --- tests/integration/compile_pip_requirements/BUILD.bazel | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/integration/compile_pip_requirements/BUILD.bazel b/tests/integration/compile_pip_requirements/BUILD.bazel index d0fa32a3e5..b0ec7e297d 100644 --- a/tests/integration/compile_pip_requirements/BUILD.bazel +++ b/tests/integration/compile_pip_requirements/BUILD.bazel @@ -37,6 +37,8 @@ some(labels(data, :requirements.update) intersect :requirements_extra.in) """, scope = [ ":requirements.update", + ":requirements.in", + ":requirements_extra.in", ], ) From b9487f71c486698f583c1d276d7532af7780ff30 Mon Sep 17 00:00:00 2001 From: Artemy Granat Date: Mon, 6 Jul 2026 17:51:07 +0300 Subject: [PATCH 4/5] use absolute labels --- tests/integration/compile_pip_requirements/BUILD.bazel | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/integration/compile_pip_requirements/BUILD.bazel b/tests/integration/compile_pip_requirements/BUILD.bazel index b0ec7e297d..b1f398c4a8 100644 --- a/tests/integration/compile_pip_requirements/BUILD.bazel +++ b/tests/integration/compile_pip_requirements/BUILD.bazel @@ -32,13 +32,13 @@ compile_pip_requirements( genquery( name = "requirements_update_data", expression = """ -some(labels(data, :requirements.update) intersect :requirements.in) union -some(labels(data, :requirements.update) intersect :requirements_extra.in) +some(labels(data, @compile_pip_requirements//:requirements.update) intersect @compile_pip_requirements//:requirements.in) union +some(labels(data, @compile_pip_requirements//:requirements.update) intersect @compile_pip_requirements//:requirements_extra.in) """, scope = [ - ":requirements.update", - ":requirements.in", - ":requirements_extra.in", + "@compile_pip_requirements//:requirements.update", + "@compile_pip_requirements//:requirements.in", + "@compile_pip_requirements//:requirements_extra.in", ], ) From c08942ff01cbfbdeb7c37a9868b57fd5ea36fc34 Mon Sep 17 00:00:00 2001 From: Artemy Granat Date: Wed, 8 Jul 2026 15:32:22 +0300 Subject: [PATCH 5/5] create local compile_pip_requirements repo --- tests/integration/compile_pip_requirements/WORKSPACE | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/integration/compile_pip_requirements/WORKSPACE b/tests/integration/compile_pip_requirements/WORKSPACE index 0eeab2067c..397d828261 100644 --- a/tests/integration/compile_pip_requirements/WORKSPACE +++ b/tests/integration/compile_pip_requirements/WORKSPACE @@ -3,6 +3,11 @@ local_repository( path = "../../..", ) +local_repository( + name = "compile_pip_requirements", + path = ".", +) + load("@rules_python//python:repositories.bzl", "py_repositories", "python_register_toolchains") py_repositories()