fix(uv): make lock .update refuse arguments instead of ignoring them (#4526) The `.update` target of `lock()` copies the lock file that the build action already produced into the source tree, so arguments given to it can never reach `uv`; the docstring says as much. It accepted and silently dropped them, though. `bazel run //:requirements.update -- --upgrade-package foo` exited 0 and reported a copy, and the package was not upgraded. `.update` now exits with status 2 when given any argument and prints the equivalent `.run` command, which passes the arguments to `uv`. Without arguments, `.update` and the test that wraps it behave as before.
diff --git a/news/uv-lock-update-arguments.fixed.md b/news/uv-lock-update-arguments.fixed.md new file mode 100644 index 0000000..854e9b3 --- /dev/null +++ b/news/uv-lock-update-arguments.fixed.md
@@ -0,0 +1,3 @@ +(uv) The `.update` target of {obj}`lock` now refuses arguments and prints the +equivalent `.run` command, which passes them to `uv`. It used to accept and drop +them, so `--upgrade-package` reported success without upgrading anything.
diff --git a/python/uv/private/template/lock_copier.py b/python/uv/private/template/lock_copier.py index 8756fc4..df2f11d 100644 --- a/python/uv/private/template/lock_copier.py +++ b/python/uv/private/template/lock_copier.py
@@ -1,3 +1,4 @@ +import shlex import sys from difflib import unified_diff from os import environ @@ -6,9 +7,30 @@ _LINE = "=" * 80 +def _quote(arg): + """Quotes arg for pasting into the user's shell.""" + quoted = shlex.quote(arg) + if sys.platform == "win32" and quoted != arg: + # CMD recognizes only double quotes. + return f'"{arg}"' + return quoted + + def main(): src = "{{src}}" dst = "{{dst}}" + update_target = "{{update_target}}" + + if len(sys.argv) > 1: + # This copies what the lock build action produced, and no argument can + # reach that action; the `.run` target passes them to uv instead. + run_target = update_target.removesuffix(".update") + ".run" + print( + f"{update_target} takes no arguments. To pass them to uv, run:\n\n" + f" bazel run {run_target} -- {' '.join(map(_quote, sys.argv[1:]))}", + file=sys.stderr, + ) + return 2 src = Path(src) if not src.exists():
diff --git a/tests/uv/lock/lock_run_test.py b/tests/uv/lock/lock_run_test.py index 5e077ae..124166b 100644 --- a/tests/uv/lock/lock_run_test.py +++ b/tests/uv/lock/lock_run_test.py
@@ -43,14 +43,14 @@ raise ValueError(f"Could not find file in runfiles: {path}") -def _run_binary(path: Path, **kwargs): +def _run_binary(path: Path, *args: str, **kwargs): """Run a binary, handling Windows .bat files.""" if os.name == "nt": return subprocess.run( - ["cmd.exe", "/c", str(path)], + ["cmd.exe", "/c", str(path), *args], **kwargs, ) - return subprocess.run(path, **kwargs) + return subprocess.run([path, *args], **kwargs) class LockTests(unittest.TestCase): @@ -212,6 +212,43 @@ self.assertTrue(want_path.exists(), "The path should exist after the test") self.assertNotEqual(want_path.read_text(), "") + def test_requirements_updating_refuses_arguments(self): + # Given + copier_path = _relative_rpath("requirements.update") + + # When + with tempfile.TemporaryDirectory() as dir: + workspace_dir = Path(dir) + want_path = ( + workspace_dir + / "tests" + / "uv" + / "lock" + / "testdata" + / "requirements.txt" + ) + want_path.parent.mkdir(parents=True) + + output = _run_binary( + copier_path, + "--upgrade-package", + "foo >=2", + capture_output=True, + env=self._subprocess_env(workspace_dir), + ) + + # Then + self.assertEqual(2, output.returncode, output.stderr) + # Quoted for the shell the command is pasted into: CMD on Windows + # recognizes only double quotes. + quoted = '"foo >=2"' if os.name == "nt" else "'foo >=2'" + self.assertIn( + "bazel run //tests/uv/lock:requirements.run -- --upgrade-package " + + quoted, + output.stderr.decode("utf-8"), + ) + self.assertFalse(want_path.exists(), "Nothing should be copied") + def test_requirements_run_on_the_first_time(self): # Given copier_path = _relative_rpath("requirements_new_file.run")