Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(generation): stop a formatter comment from prescribing ruff format (#2444)

Amana21cc41b89fb
diff --git a/packages/core/src/repowise/core/generation/editor_files/tech_stack.py b/packages/core/src/repowise/core/generation/editor_files/tech_stack.py
index 5859984de..0d1491876 100644
--- a/packages/core/src/repowise/core/generation/editor_files/tech_stack.py
+++ b/packages/core/src/repowise/core/generation/editor_files/tech_stack.py
@@ -9,6 +9,7 @@ import json
import re
from pathlib import Path
+from ...precedent.structural import declares_ruff_format
from .data import TechStackItem
# Node.js framework/library signatures to detect from package.json dependencies
@@ -455,7 +456,7 @@ def detect_build_commands(repo_path: Path) -> dict[str, str]:
commands["test"] = "pytest"
if "lint" not in commands and "ruff" in text:
commands["lint"] = "ruff check ."
- if "format" not in commands and "ruff" in text and "format" in text:
+ if "format" not in commands and declares_ruff_format(repo_path):
commands["format"] = "ruff format ."
if "typecheck" not in commands and "mypy" in text:
commands["typecheck"] = "mypy ."
diff --git a/packages/core/src/repowise/core/precedent/structural.py b/packages/core/src/repowise/core/precedent/structural.py
index 27e102390..c708703ba 100644
--- a/packages/core/src/repowise/core/precedent/structural.py
+++ b/packages/core/src/repowise/core/precedent/structural.py
@@ -387,7 +387,7 @@ def _formatter_drift(root: Path, _traverser: Any) -> list[Episode]:
executable, a timeout, a crash, unparsable output — yields no episode. A
budget that quietly produces a partial count is worse than no fact at all.
"""
- if not _declares_ruff_format(root):
+ if not declares_ruff_format(root):
return []
executable = _ruff_executable(root)
if executable is None:
@@ -434,16 +434,20 @@ def _formatter_drift(root: Path, _traverser: Any) -> list[Episode]:
]
-def _declares_ruff_format(root: Path) -> bool:
+def declares_ruff_format(root: Path) -> bool:
"""True when the repo names ruff, and only ruff, as its formatter.
- Deliberately stricter than :func:`detect_build_commands`, whose format
- inference fires on a ``pyproject.toml`` containing the words "ruff" and
- "format" anywhere. That is fine for a suggested command and wrong as the
- premise of a stored fact: ruff-as-linter beside black-as-formatter is a
+ Shared by :func:`detect_build_commands` (``generation/editor_files/
+ tech_stack.py``), which used to infer the format command from a
+ ``pyproject.toml`` containing the words "ruff" and "format" anywhere. A
+ comment like "Ruff -- linter + formatter" was enough to fire that
+ substring test, which is wrong as the premise of either a suggested
+ command or a stored fact: ruff-as-linter beside black-as-formatter is a
common pairing, and the two disagree, so the inference would tell a
- black-clean repo it is not formatter-clean by a formatter it never chose.
- A repo that declares any competing formatter is silent regardless.
+ black-clean repo it is not formatter-clean by a formatter it never chose,
+ or hand an agent a `ruff format .` instruction that rewrites the tree of
+ a repo that never configured the formatter (issue #2384). A repo that
+ declares any competing formatter is silent regardless.
"""
pyproject = _read_text(root / "pyproject.toml")
if any(marker in pyproject for marker in _COMPETING_FORMATTERS):
diff --git a/tests/unit/generation/test_tech_stack.py b/tests/unit/generation/test_tech_stack.py
index 2ec0463ee..d9a93e3ad 100644
--- a/tests/unit/generation/test_tech_stack.py
+++ b/tests/unit/generation/test_tech_stack.py
@@ -366,6 +366,35 @@ def test_detects_ruff_from_pyproject(tmp_path):
assert "ruff" in cmds["lint"]
+def test_a_comment_mentioning_formatter_does_not_prescribe_ruff_format(tmp_path):
+ # issue #2384: ruff configured as a linter only, with "formatter"
+ # appearing solely inside comments, must not emit a format command.
+ (tmp_path / "pyproject.toml").write_text(
+ "[tool.ruff]\n"
+ "line-length = 88 # Ruff - linter + formatter\n"
+ '"E501", # line too long (handled by formatter)\n',
+ encoding="utf-8",
+ )
+ cmds = detect_build_commands(tmp_path)
+ assert "format" not in cmds
+
+
+def test_a_declared_ruff_format_section_prescribes_ruff_format(tmp_path):
+ (tmp_path / "pyproject.toml").write_text(
+ "[tool.ruff]\nline-length = 88\n\n[tool.ruff.format]\n", encoding="utf-8"
+ )
+ cmds = detect_build_commands(tmp_path)
+ assert cmds.get("format") == "ruff format ."
+
+
+def test_a_competing_formatter_suppresses_the_ruff_format_command(tmp_path):
+ (tmp_path / "pyproject.toml").write_text(
+ "[tool.ruff]\nline-length = 88\n\n[tool.black]\n", encoding="utf-8"
+ )
+ cmds = detect_build_commands(tmp_path)
+ assert "format" not in cmds
+
+
def test_detects_npm_scripts(tmp_path):
pkg = {
"name": "myapp",