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.

refactor(core): move the ruff formatter check into a shared module (#3159)

JikSau-D284f1701b962
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 ca757cc12..502dd6b49 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
@@ -11,9 +11,10 @@ from collections.abc import Callable, Container, Iterator
from itertools import chain
from pathlib import Path
+from repowise.core.repo_formatters import declares_ruff_format
+
from ...ingestion.composer import COMPOSER_JSON, read_composer
from ...ingestion.framework_facts import detect_php_framework
-from ...precedent.structural import declares_ruff_format
from .data import TechStackItem
# package.json dependency -> (display name, category).
diff --git a/packages/core/src/repowise/core/precedent/structural.py b/packages/core/src/repowise/core/precedent/structural.py
index 5cfeb1801..4b9d33625 100644
--- a/packages/core/src/repowise/core/precedent/structural.py
+++ b/packages/core/src/repowise/core/precedent/structural.py
@@ -26,6 +26,8 @@ import subprocess
from pathlib import Path
from typing import Any
+from repowise.core.repo_formatters import declares_ruff_format
+
from .store import TIER_STRUCTURAL, Episode, EpisodeStore
_log = logging.getLogger(__name__)
@@ -435,45 +437,6 @@ def _formatter_drift(root: Path, _traverser: Any) -> list[Episode]:
]
-def declares_ruff_format(root: Path) -> bool:
- """True when the repo names ruff, and only ruff, as its formatter.
-
- 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,
- 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):
- return False
- if "[tool.ruff.format]" in pyproject:
- return True
- # An explicit invocation anywhere a project records its own commands.
- for candidate in ("Makefile", "package.json", ".pre-commit-config.yaml", "justfile"):
- text = _read_text(root / candidate)
- if "ruff format" in text or "ruff-format" in text:
- return True
- return False
-
-
-#: Declaring one of these means the repo formats with something else.
-_COMPETING_FORMATTERS: tuple[str, ...] = ("[tool.black]", "[tool.blue]", "[tool.yapf]")
-
-
-def _read_text(path: Path) -> str:
- try:
- return path.read_text(encoding="utf-8", errors="replace")
- except OSError:
- return ""
-
-
def _head_commit(root: Path) -> str | None:
"""The commit the checkout is on, or None. Best-effort, never fatal."""
try:
diff --git a/packages/core/src/repowise/core/repo_formatters.py b/packages/core/src/repowise/core/repo_formatters.py
new file mode 100644
index 000000000..392cc9528
--- /dev/null
+++ b/packages/core/src/repowise/core/repo_formatters.py
@@ -0,0 +1,41 @@
+"""Whether a repository declares ruff as its formatter.
+
+Two readers ask the same question of the same checkout: the formatter-drift
+episode in ``precedent/structural.py`` and the ``Format:`` line in generated
+agent files (``generation/editor_files/tech_stack.py``). The rules live here
+so the two cannot drift apart.
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+#: Declaring one of these means the repo formats with something else.
+_COMPETING_FORMATTERS: tuple[str, ...] = ("[tool.black]", "[tool.blue]", "[tool.yapf]")
+
+
+def declares_ruff_format(root: Path) -> bool:
+ """True when the repo names ruff, and only ruff, as its formatter.
+
+ A ``[tool.ruff.format]`` section is a declaration, as is an explicit
+ ``ruff format`` or ``ruff-format`` invocation in a file where a project
+ records its commands. Ruff beside a competing formatter is lint
+ configuration, not a formatter choice.
+ """
+ pyproject = _read_text(root / "pyproject.toml")
+ if any(marker in pyproject for marker in _COMPETING_FORMATTERS):
+ return False
+ if "[tool.ruff.format]" in pyproject:
+ return True
+ for candidate in ("Makefile", "package.json", ".pre-commit-config.yaml", "justfile"):
+ text = _read_text(root / candidate)
+ if "ruff format" in text or "ruff-format" in text:
+ return True
+ return False
+
+
+def _read_text(path: Path) -> str:
+ try:
+ return path.read_text(encoding="utf-8", errors="replace")
+ except OSError:
+ return ""
diff --git a/tests/unit/generation/test_tech_stack.py b/tests/unit/generation/test_tech_stack.py
index a387711eb..fa89080ca 100644
--- a/tests/unit/generation/test_tech_stack.py
+++ b/tests/unit/generation/test_tech_stack.py
@@ -434,6 +434,15 @@ def test_a_competing_formatter_suppresses_the_ruff_format_command(tmp_path):
assert "format" not in cmds
+def test_explicit_ruff_format_in_pre_commit_sets_the_format_command(tmp_path):
+ (tmp_path / "pyproject.toml").write_text("[tool.ruff]\n", encoding="utf-8")
+ (tmp_path / ".pre-commit-config.yaml").write_text(
+ "repos:\n - hooks:\n - id: ruff-format\n", encoding="utf-8"
+ )
+ cmds = detect_build_commands(tmp_path)
+ assert cmds.get("format") == "ruff format ."
+
+
def test_detects_npm_scripts(tmp_path):
pkg = {
"name": "myapp",