Skip to content

Commit

Permalink
Pass generator label explicitly (#41)
Browse files Browse the repository at this point in the history
Follow-up to #35: pass the label of the generator target to the test target explicitly. In #35 we went with a solution that messed with the label string, but passing an explicit label should be safer w/r/t refactors.
  • Loading branch information
mark-thm authored May 22, 2024
1 parent 6965c28 commit 13d8978
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 6 additions & 4 deletions uv/private/pip.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def _python_platform(maybe_python_platform):
return ""
return "--python-platform {python_platform}".format(python_platform = maybe_python_platform)

def _uv_pip_compile(ctx, template, executable):
def _uv_pip_compile(ctx, template, executable, generator_label):
py_toolchain = ctx.toolchains[_PY_TOOLCHAIN]
ctx.actions.expand_template(
template = template,
Expand All @@ -25,7 +25,7 @@ def _uv_pip_compile(ctx, template, executable):
"{{requirements_txt}}": ctx.file.requirements_txt.short_path,
"{{resolved_python}}": py_toolchain.py3_runtime.interpreter.short_path,
"{{python_platform}}": _python_platform(ctx.attr.python_platform),
"{{label}}": str(ctx.label),
"{{label}}": str(generator_label),
},
)

Expand All @@ -40,7 +40,7 @@ def _runfiles(ctx):

def _pip_compile_impl(ctx):
executable = ctx.actions.declare_file(ctx.attr.name)
_uv_pip_compile(ctx, ctx.file._template, executable)
_uv_pip_compile(ctx, ctx.file._template, executable, ctx.label)
return DefaultInfo(
executable = executable,
runfiles = _runfiles(ctx),
Expand All @@ -57,14 +57,15 @@ _pip_compile = rule(

def _pip_compile_test_impl(ctx):
executable = ctx.actions.declare_file(ctx.attr.name)
_uv_pip_compile(ctx, ctx.file._template, executable)
_uv_pip_compile(ctx, ctx.file._template, executable, ctx.attr.generator_label.label)
return DefaultInfo(
executable = executable,
runfiles = _runfiles(ctx),
)

_pip_compile_test = rule(
attrs = _common_attrs | {
"generator_label": attr.label(mandatory = True),
"_template": attr.label(default = "//uv/private:pip_compile_test.sh", allow_single_file = True),
},
toolchains = [_PY_TOOLCHAIN],
Expand All @@ -85,6 +86,7 @@ def pip_compile(name, requirements_in = None, requirements_txt = None, target_co

_pip_compile_test(
name = name + "_diff_test",
generator_label = name,
requirements_in = requirements_in or "//:requirements.in",
requirements_txt = requirements_txt or "//:requirements.txt",
python_platform = python_platform or "",
Expand Down
8 changes: 5 additions & 3 deletions uv/private/pip_compile_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ $UV pip compile \
--generate-hashes \
--emit-index-url \
--no-strip-extras \
--custom-compile-command "bazel run ${LABEL%_diff_test}" \
--custom-compile-command "bazel run ${LABEL}" \
--python-version=$PYTHON_VERSION \
$(echo $PYTHON_PLATFORM) \
-o __updated__ \
$REQUIREMENTS_IN

# check files match
if ! diff "$REQUIREMENTS_TXT" "__updated__" > /dev/null
DIFF="$(diff "$REQUIREMENTS_TXT" "__updated__" || true)"
if [ "$DIFF" != "" ]
then
echo >&2 "FAIL: $REQUIREMENTS_TXT needs to be re-generated."
echo >&2 "FAIL: $REQUIREMENTS_TXT is out-of-date. Run 'bazel run $LABEL' to update."
echo >&2 "$DIFF"
exit 1
fi

0 comments on commit 13d8978

Please sign in to comment.