Skip to content

Commit

Permalink
feat: Support requirements overrides files (#186)
Browse files Browse the repository at this point in the history
Closes #128

#128 gives a workaround, but it only works when the overrides file is at
the root because `args` does not seem to support [Predefined
source/output path
variables](https://bazel.build/reference/be/make-variables#predefined_label_variables).

This PR makes `requirements_overrides` as a dedicated attr so in the
rule can access its `short_path`.
  • Loading branch information
honnix authored Jan 27, 2025
1 parent 925aebd commit ceb7f10
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Additionally, you can specify the following optional args:
- `data`: pass additional files to be present when generating and testing requirements txt files (see also [examples/multiple-inputs](examples/multiple-inputs/))
- `tags`: tags to apply to the test target
- `target_compatible_with`: restrict targets to running on the specified Bazel platform
- `requirements_overrides`: a label for the file that is used to override dependencies (passed to uv via `--overrides`)

### create_venv

Expand Down
4 changes: 4 additions & 0 deletions uv/pip.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ load("//uv/private:pip.bzl", "pip_compile_test", _pip_compile = "pip_compile")
def pip_compile(
name,
requirements_in = None,
requirements_overrides = None,
requirements_txt = None,
target_compatible_with = None,
python_platform = None,
Expand All @@ -23,6 +24,7 @@ def pip_compile(
name: name of the primary compilation target.
requirements_in: (optional, default "//:requirements.in") a label for the requirements.in file.
May also be provided as a list of strings which represent the requirements file lines.
requirements_overrides: (optional, default None) a label for the file that is used to override dependencies.
requirements_txt: (optional, default "//:requirements.txt") a label for the requirements.txt file.
python_platform: (optional) a uv pip compile compatible value for --python-platform.
universal: (optional, default False) use uv's `--universal` option
Expand Down Expand Up @@ -58,6 +60,7 @@ def pip_compile(
_pip_compile(
name = name,
requirements_in = requirements_in,
requirements_overrides = requirements_overrides,
requirements_txt = requirements_txt,
python_platform = python_platform,
universal = universal,
Expand All @@ -78,6 +81,7 @@ def pip_compile(
name = name + "_test",
generator_label = name,
requirements_in = requirements_in,
requirements_overrides = requirements_overrides,
requirements_txt = requirements_txt,
python_platform = python_platform or "",
universal = universal,
Expand Down
6 changes: 5 additions & 1 deletion uv/private/pip.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ _DEFAULT_ARGS = [

_COMMON_ATTRS = {
"requirements_in": attr.label(mandatory = True, allow_single_file = True),
"requirements_overrides": attr.label(mandatory = False, allow_single_file = True),
"requirements_txt": attr.label(mandatory = True, allow_single_file = True),
"python_platform": attr.string(),
"universal": attr.bool(),
Expand Down Expand Up @@ -55,6 +56,8 @@ def _uv_pip_compile(
args.append("--python-platform={platform}".format(platform = ctx.attr.python_platform))
elif ctx.attr.universal:
args.append("--universal")
if ctx.attr.requirements_overrides:
args.append("--overrides={overrides_file}".format(overrides_file = ctx.file.requirements_overrides.short_path))

ctx.actions.expand_template(
template = template,
Expand All @@ -70,8 +73,9 @@ def _uv_pip_compile(

def _runfiles(ctx):
py3_runtime = _python_runtime(ctx)
overrides_file = [ctx.file.requirements_overrides] if ctx.attr.requirements_overrides else []
runfiles = ctx.runfiles(
files = [ctx.file.requirements_in, ctx.file.requirements_txt] + ctx.files.data,
files = [ctx.file.requirements_in, ctx.file.requirements_txt] + overrides_file + ctx.files.data,
transitive_files = py3_runtime.files,
)
runfiles = runfiles.merge(ctx.attr._uv[0].default_runfiles)
Expand Down

0 comments on commit ceb7f10

Please sign in to comment.