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(editor-files): keep a malformed manifest from crashing the tech stack scan (#2608)

Raghav Chamadiyaf6e01612aecb
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 577ada740..ca757cc12 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
@@ -173,8 +173,9 @@ def _manifest_fingerprint(repo_path: Path) -> tuple:
def detect_tech_stack(repo_path: Path) -> list[TechStackItem]:
"""Detect languages, frameworks, and infra tools from manifest files.
- Scans repo root and one level deep for common manifest files.
- Returns items sorted by category then name.
+ Reads root manifests, looks for tsconfig.json up to two levels down, and
+ walks up to five levels for .csproj files. A malformed manifest contributes
+ what it can and never raises. Returns items sorted by category then name.
Memoized on the root inputs' stat signature: a single ``repowise update``
asks twice (the graph's framework edges, then the knowledge-graph refresh)
@@ -267,11 +268,19 @@ def _read_package_json(repo_path: Path) -> object:
return None
+def _read_manifest_text(path: Path) -> str | None:
+ """Text of a manifest, or None when absent. Undecodable bytes are replaced."""
+ try:
+ return path.read_text(encoding="utf-8", errors="replace")
+ except OSError:
+ return None
+
+
def _node_items(repo_path: Path) -> Iterator[_Item]:
pkg = _read_package_json(repo_path)
if not isinstance(pkg, dict):
return
- all_deps = {**(pkg.get("dependencies") or {}), **(pkg.get("devDependencies") or {})}
+ all_deps = {**_dep_table(pkg, "dependencies"), **_dep_table(pkg, "devDependencies")}
if _is_node_app(pkg, all_deps):
yield "Node.js", _node_engine(pkg), "language"
yield from _node_framework_items(all_deps)
@@ -286,14 +295,21 @@ def _node_framework_items(all_deps: dict) -> Iterator[_Item]:
yield display, _dep_version(all_deps, dep_key), cat
+def _dep_table(pkg: dict, field: str) -> dict:
+ table = pkg.get(field)
+ return table if isinstance(table, dict) else {}
+
+
def _dep_version(all_deps: dict, name: str) -> str | None:
"""The declared version with its range operator stripped, or None."""
- return all_deps.get(name, "").lstrip("^~>=") or None
+ version = all_deps.get(name)
+ return (version.lstrip("^~>=") or None) if isinstance(version, str) else None
def _node_engine(pkg: dict) -> str | None:
engines = pkg.get("engines")
- return engines.get("node") if isinstance(engines, dict) else None
+ node = engines.get("node") if isinstance(engines, dict) else None
+ return node if isinstance(node, str) else None
def _is_node_app(pkg: dict, all_deps: dict) -> bool:
@@ -330,18 +346,16 @@ def _python_items(repo_path: Path) -> Iterator[_Item]:
if not (pyproject.exists() or (repo_path / "setup.py").exists()):
return
yield "Python", None, "language"
- if not pyproject.exists():
- return
- text = pyproject.read_text(encoding="utf-8").lower()
+ text = (_read_manifest_text(pyproject) or "").lower()
for dep_key, (display, cat) in _PYTHON_FRAMEWORKS.items():
if dep_key in text:
yield display, None, cat
def _go_items(repo_path: Path) -> Iterator[_Item]:
- go_mod = repo_path / "go.mod"
- if go_mod.exists():
- ver_match = re.search(r"^go\s+(\S+)", go_mod.read_text(encoding="utf-8"), re.MULTILINE)
+ text = _read_manifest_text(repo_path / "go.mod")
+ if text is not None:
+ ver_match = re.search(r"^go\s+(\S+)", text, re.MULTILINE)
yield "Go", ver_match.group(1) if ver_match else None, "language"
@@ -503,10 +517,9 @@ def _package_script_commands(repo_path: Path) -> dict[str, str]:
def _add_pyproject_commands(repo_path: Path, commands: dict[str, str]) -> None:
- pyproject = repo_path / "pyproject.toml"
- if not pyproject.exists():
+ text = _read_manifest_text(repo_path / "pyproject.toml")
+ if text is None:
return
- text = pyproject.read_text(encoding="utf-8")
if "pytest" in text:
commands.setdefault("test", "pytest")
if "ruff" in text:
@@ -518,8 +531,7 @@ def _add_pyproject_commands(repo_path: Path, commands: dict[str, str]) -> None:
def _make_commands(repo_path: Path) -> dict[str, str]:
- try:
- text = (repo_path / "Makefile").read_text(encoding="utf-8")
- except (OSError, ValueError):
+ text = _read_manifest_text(repo_path / "Makefile")
+ if text is None:
return {}
return _first_available(_MAKE_TARGETS, set(_MAKE_TARGET_RE.findall(text)), "make")
diff --git a/tests/unit/generation/test_tech_stack.py b/tests/unit/generation/test_tech_stack.py
index d9a93e3ad..a387711eb 100644
--- a/tests/unit/generation/test_tech_stack.py
+++ b/tests/unit/generation/test_tech_stack.py
@@ -269,6 +269,45 @@ def test_malformed_composer_does_not_crash(tmp_path):
assert "PHP" in names # PHP language still added (file existed)
+ "pkg",
+ [
+ {"main": "index.js", "dependencies": ["react"]},
+ {"main": "index.js", "devDependencies": "react"},
+ ],
+)
+def test_non_object_dependency_table_does_not_crash(tmp_path, pkg):
+ (tmp_path / "package.json").write_text(json.dumps(pkg), encoding="utf-8")
+ names = [i.name for i in detect_tech_stack(tmp_path)]
+ assert "Node.js" in names
+ assert "React" not in names
+
+
+def test_non_string_versions_yield_no_version(tmp_path):
+ pkg = {"engines": {"node": 20}, "dependencies": {"typescript": 5, "react": None}}
+ (tmp_path / "package.json").write_text(json.dumps(pkg), encoding="utf-8")
+ versions = {i.name: i.version for i in detect_tech_stack(tmp_path)}
+ assert versions["Node.js"] is None
+ assert versions["TypeScript"] is None
+ assert versions["React"] is None
+
+
+def test_non_utf8_pyproject_does_not_crash(tmp_path):
+ (tmp_path / "pyproject.toml").write_bytes(
+ b'[project]\n# caf\xe9\ndependencies = ["fastapi", "pytest"]\n'
+ )
+ names = [i.name for i in detect_tech_stack(tmp_path)]
+ assert "Python" in names
+ assert "FastAPI" in names
+ assert detect_build_commands(tmp_path)["test"] == "pytest"
+
+
+def test_non_utf8_go_mod_does_not_crash(tmp_path):
+ (tmp_path / "go.mod").write_bytes(b"module example.com/caf\xe9\n\ngo 1.22\n")
+ versions = {i.name: i.version for i in detect_tech_stack(tmp_path)}
+ assert versions["Go"] == "1.22"
+
+
# ---------------------------------------------------------------------------
# detect_tech_stack memoisation
# ---------------------------------------------------------------------------