Skip to content

Commit 6709e72

Browse files
committed
fix: add explicit data attribute to compile_pip_requirements and forward it to the generated py_binary
1 parent 20db956 commit 6709e72

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

news/3858.fixed.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(compile_pip_requirements) Add the explicit `data` attribute and forward it
2+
directly to the generated `py_binary`, so files passed via `data` can be
3+
referenced from `extra_args` using `$(location ...)`.

python/private/pypi/pip_compile.bzl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ make it possible to have multiple tools inside the `pypi` directory
2222
load("//python:py_binary.bzl", _py_binary = "py_binary")
2323
load("//python:py_test.bzl", _py_test = "py_test")
2424

25+
def _dedupe(data):
26+
res = []
27+
for d in data:
28+
if d not in res:
29+
res.append(d)
30+
return res
31+
32+
2533
def pip_compile(
2634
name,
2735
srcs = None,
@@ -39,6 +47,7 @@ def pip_compile(
3947
visibility = ["//visibility:private"],
4048
tags = None,
4149
constraints = [],
50+
data = [],
4251
**kwargs):
4352
"""Generates targets for managing pip dependencies with pip-compile (piptools).
4453
@@ -81,6 +90,7 @@ def pip_compile(
8190
tags: tagging attribute common to all build rules, passed to both the _test and .update rules.
8291
visibility: passed to both the _test and .update rules.
8392
constraints: a list of files containing constraints to pass to pip-compile with `--constraint`.
93+
data: A list of labels to include as part of the `data` attribute in the generated `py_binary`.
8494
**kwargs: other bazel attributes passed to the "_test" rule.
8595
"""
8696
if len([x for x in [srcs, src, requirements_in] if x != None]) > 1:
@@ -95,16 +105,18 @@ def pip_compile(
95105

96106
requirements_txt = name + ".txt" if requirements_txt == None else requirements_txt
97107

108+
data = data or []
109+
98110
# "Default" target produced by this macro
99111
# Allow a compile_pip_requirements rule to include another one in the data
100112
# for a requirements file that does `-r ../other/requirements.txt`
101113
native.filegroup(
102114
name = name,
103-
srcs = kwargs.pop("data", []) + [requirements_txt],
115+
srcs = data + [requirements_txt],
104116
visibility = visibility,
105117
)
106118

107-
data = [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] + constraints
119+
data = _dedupe(data + [name, requirements_txt] + srcs + [f for f in (requirements_linux, requirements_darwin, requirements_windows) if f != None] + constraints)
108120

109121
# Use the Label constructor so this is expanded in the context of the file
110122
# where it appears, which is to say, in @rules_python

tests/integration/compile_pip_requirements/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ compile_pip_requirements(
2929
requirements_txt = "requirements_lock.txt",
3030
)
3131

32+
genquery(
33+
name = "requirements_update_data",
34+
expression = """
35+
some(labels(data, //:requirements.update) intersect //:requirements.in) union
36+
some(labels(data, //:requirements.update) intersect //:requirements_extra.in)
37+
""",
38+
scope = [
39+
":requirements.update",
40+
],
41+
)
42+
3243
compile_pip_requirements(
3344
name = "requirements_nohashes",
3445
src = "requirements.txt",

0 commit comments

Comments
 (0)