Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Make run_ruff_format.py reject arguments it cannot honour instead of silently dropping them (#10578)

Daniel Han49295820991a
diff --git a/.pre-commit-ci.yaml b/.pre-commit-ci.yaml
deleted file mode 100644
index dbcf58ab763..00000000000
--- a/.pre-commit-ci.yaml
+++ /dev/null
@@ -1,6 +0,0 @@
-ci:
- autofix_prs: true
- autofix_prs_limit: 5
- autoupdate_schedule: monthly
- autoupdate_commit_msg: "chore: pre-commit autoupdate"
- skip: []
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 25000dd6e9b..7099a2de94a 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -1,3 +1,16 @@
+# pre-commit.ci reads its settings from here and only from here. These lived in a
+# root .pre-commit-ci.yaml for ten months, which is not a file pre-commit.ci looks
+# at, so none of them applied: every autoupdate commit on main used the default
+# message and arrived weekly, not monthly.
+#
+# autofix_prs is the default, but it is stated because the sync-allow-scripts-pins
+# hook below depends on it by name. skip and submodules are left out: they are
+# defaults that guard nothing, and unused settings are what produced this bug.
+ci:
+ autofix_prs: true
+ autoupdate_schedule: monthly
+ autoupdate_commit_msg: "chore: pre-commit autoupdate"
+
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.15.18
@@ -11,7 +24,11 @@ repos:
hooks:
- id: ruff-format-with-kwargs
name: Ruff format with kwarg spacing
- entry: scripts/run_ruff_format.py
+ # `python <script>` not a direct exec, for the reason spelled out on the
+ # hook below: an autofix commit can drop the executable bit, and a
+ # shebang-style entry then dies with "Executable not found". That is
+ # twice now (#10519, #10523); this makes the mode bit irrelevant.
+ entry: python scripts/run_ruff_format.py
language: python
types: [python]
# Mirror ruff's [tool.ruff] extend-exclude so this hook does not
diff --git a/scripts/run_ruff_format.py b/scripts/run_ruff_format.py
index 5af6dbd46fd..403d60ca59d 100755
--- a/scripts/run_ruff_format.py
+++ b/scripts/run_ruff_format.py
@@ -16,6 +16,7 @@ CONFIG = HERE.parent / ".pre-commit-config.yaml"
# Set to run against whatever ruff is installed. For a one-off experiment; a commit
# made under it will be reformatted by the hook and fail pre-commit.
ANY_VERSION_ENV = "UNSLOTH_RUFF_FORMAT_ANY_VERSION"
+USAGE = "usage: run_ruff_format.py FILE [FILE ...] (formats in place; no options)"
# `- ruff==0.6.9` under the hook's additional_dependencies. Read out of the config
# rather than copied here, because a second copy of the pin is a second thing to
@@ -58,10 +59,43 @@ def version_mismatch(pinned: str | None, installed: str | None) -> bool:
return bool(pinned and installed and pinned != installed)
+def parse_files(argv: list[str]) -> tuple[list[str], str | None]:
+ """The paths to format, or an empty list plus a message saying why not.
+
+ Every argument is a path to rewrite. Silently dropping the rest was worse
+ than it sounds: `--check FILE` dropped the flag, kept the file, and wrote
+ to it, and a typo'd path formatted nothing while exiting 0, which quietly
+ passes any "the formatter is a fixed point" check.
+ """
+ if not argv:
+ return [], f"no files given.\n{USAGE}"
+
+ options = [arg for arg in argv if arg.startswith("-")]
+ if options:
+ message = f"unsupported option{'s' if len(options) > 1 else ''}: {' '.join(options)}"
+ if any(opt in ("--check", "--diff") for opt in options):
+ message += (
+ "\n There is no check mode: this script always rewrites the files"
+ " it is given, and `ruff format --check` is not an equivalent."
+ "\n It checks the middle one of three passes, so a clean ruff says"
+ " nothing about the kwarg-spacing passes either side of it."
+ "\n To preview a run, copy the file aside, run this script on the"
+ " copy, and diff the two."
+ )
+ return [], f"{message}\n{USAGE}"
+
+ missing = [arg for arg in argv if not Path(arg).exists()]
+ if missing:
+ return [], f"no such file{'s' if len(missing) > 1 else ''}: {' '.join(missing)}\n{USAGE}"
+
+ return list(argv), None
+
+
def main(argv: list[str]) -> int:
- files = [arg for arg in argv if Path(arg).exists()]
- if not files:
- return 0
+ files, error = parse_files(argv)
+ if error is not None:
+ print(f"run_ruff_format: {error}", file = sys.stderr)
+ return 2
# Checked before anything is rewritten. ruff's own formatting is not stable
# across releases -- 0.9 changed which half of an `assert cond, "msg"` gets
diff --git a/tests/test_run_ruff_format_pin.py b/tests/test_run_ruff_format_pin.py
index 611a898b98b..4a6519d4af0 100644
--- a/tests/test_run_ruff_format_pin.py
+++ b/tests/test_run_ruff_format_pin.py
@@ -28,6 +28,7 @@ from run_ruff_format import ( # noqa: E402
CONFIG,
installed_ruff_version,
main,
+ parse_files,
pinned_ruff_version,
version_mismatch,
)
@@ -137,15 +138,95 @@ class TestRefusing:
# And it really ran: the post-pass is what puts the spaces in.
assert target.read_text(encoding = "utf-8") == "x = f(a = 1)\n"
- def test_no_files_is_still_a_no_op(self, monkeypatch):
- # pre-commit calls with the changed files; an unrelated commit passes none,
- # and must not be failed for a version it never used.
+ def test_no_files_is_rejected_before_the_version_is_consulted(self, monkeypatch):
+ # This used to assert `main([]) == 0`, on the premise that an unrelated
+ # commit calls the hook with no files. It does not: the hook is
+ # `types: [python]` without `pass_filenames: false`, so pre-commit skips
+ # it outright when nothing matches rather than running it empty. The
+ # silent success only ever reached humans, and told them the formatter
+ # was a fixed point when it had not run.
monkeypatch.setattr("run_ruff_format.installed_ruff_version", lambda *a, **k: "9.9.9")
- assert main([]) == 0
+ assert main([]) == 2
+
+
+class TestArgumentsAreTakenSeriously:
+ """Every argument is a file to rewrite, so anything else has to be an error.
+
+ The old handling kept `[arg for arg in argv if Path(arg).exists()]` and
+ dropped the rest without a word, which had three faces: no arguments exited
+ 0 having formatted nothing, a typo'd path formatted nothing just as quietly,
+ and `--check FILE` lost the flag, kept the file, and wrote to it.
+ """
+
+ def test_an_unknown_flag_is_refused_and_nothing_is_written(self, tmp_path, monkeypatch):
+ # The one that did damage: the flag was dropped, the file behind it was
+ # not, and a run asked to check rewrote the file instead.
+ target = tmp_path / "sample.py"
+ original = "x = f(a=1)\n"
+ target.write_text(original, encoding = "utf-8")
+ monkeypatch.setattr("run_ruff_format.installed_ruff_version", lambda *a, **k: "9.9.9")
+ assert main(["--check", str(target)]) == 2
+ assert target.read_text(encoding = "utf-8") == original
+
+ def test_check_says_what_the_script_actually_does(self):
+ # "unsupported option" alone invites a retry without the flag, which is
+ # the write the caller was trying to avoid.
+ _, error = parse_files(["--check", "any.py"])
+ assert error is not None
+ assert "--check" in error
+ assert "always rewrites" in error
+
+ def test_it_does_not_offer_ruff_as_a_read_only_equivalent(self):
+ # This script is enforce_kwargs_spacing --pre, then ruff, then
+ # enforce_kwargs_spacing again. `ruff format --check` covers the middle
+ # pass only, so a file can pass it cleanly and still be rewritten here.
+ # Sending people there would rebuild, in the error message, the same
+ # false green the argument handling above was fixed to stop producing.
+ _, error = parse_files(["--check", "any.py"])
+ assert error is not None
+ lowered = error.lower()
+ # Naming ruff is fine. Presenting it as the substitute is not, so if it
+ # is named it has to be disclaimed in the same breath.
+ if "ruff" in lowered:
+ assert "not an equivalent" in lowered
+ # And an honest alternative is offered rather than a partial one.
+ assert "diff" in lowered
+
+ @pytest.mark.parametrize("flag", ["-q", "--diff", "--fix", "--unknown"])
+ def test_options_are_refused_by_shape_not_by_a_list(self, flag):
+ _, error = parse_files([flag])
+ assert error is not None and flag in error
+
+ def test_a_missing_path_is_named(self, tmp_path):
+ # Previously this formatted nothing and exited 0, so a typo looked like a
+ # successful run over the file you meant.
+ missing = tmp_path / "typo.py"
+ files, error = parse_files([str(missing)])
+ assert files == []
+ assert error is not None and str(missing) in error
+
+ def test_a_missing_path_fails_even_beside_a_real_one(self, tmp_path, monkeypatch):
+ # Partial credit is the whole bug: formatting the file that exists and
+ # ignoring the one that does not still reports success.
+ real = tmp_path / "real.py"
+ real.write_text("x = 1\n", encoding = "utf-8")
+ missing = tmp_path / "nope.py"
+ monkeypatch.setattr("run_ruff_format.installed_ruff_version", lambda *a, **k: "9.9.9")
+ assert main([str(real), str(missing)]) == 2
+
+ def test_real_files_still_go_through_untouched_by_the_new_check(self, tmp_path):
+ files, error = parse_files([str(tmp_path)])
+ assert error is None
+ assert files == [str(tmp_path)]
class TestTheScriptStaysRunnable:
- """It is invoked as a program, so the mode bit matters, and a wholesale rewrite drops it invisibly."""
+ """The shebang and the mode bit are kept so it still runs as a program.
+
+ The hook no longer depends on them -- its entry is `python scripts/...` now,
+ because an autofix commit dropping the bit broke main twice -- but people do
+ run it directly, and a wholesale rewrite drops the bit invisibly.
+ """
@pytest.mark.skipif(sys.platform.startswith("win"), reason = "no POSIX mode bits")
def test_the_formatter_is_executable(self):