fix(tree): support src and file aliases to prevent empty tree viewer

(cherry picked from commit 035b0ada94)
This commit is contained in:
Aditya kumar singh authored and safishamsi committed 2026-11-07 22:33:55 +00:00
1 parent 2ce70ce17d
commit c018fc9f97
4 files changed
+29 -1

No files matched your search

+1 -1
View File
@@ -683,7 +683,7 @@ def _fold_node_aliases(node: dict) -> None:
if not node.get("label") and isinstance(node.get("name"), str) and node["name"]:
node["label"] = node.pop("name")
if not node.get("source_file"):
for alias in ("file", "path", "filepath"):
for alias in ("file", "path", "filepath", "src"):
val = node.get(alias)
if isinstance(val, str) and val.strip():
node["source_file"] = node.pop(alias)
+7
View File
@@ -214,6 +214,13 @@ def build_tree(
raise ValueError("--max-children must be nonnegative")
directory_ids: set[int] = set()
nodes: List[Dict[str, Any]] = list(graph.get("nodes", []))
for n in nodes:
if not n.get("source_file"):
for alias in ("src", "file", "path", "filepath"):
val = n.get(alias)
if isinstance(val, str) and val.strip():
n["source_file"] = val.strip()
break
file_nodes = [n for n in nodes if n.get("source_file")]
if not file_nodes:
return {"name": "(empty graph)", "total_count": 0, "children": []}
+7
View File
@@ -45,6 +45,13 @@ def test_node_filepath_folds_to_source_file():
assert node["source_file"] == "baz.go"
def test_node_src_folds_to_source_file():
node = {"id": "a", "label": "A", "src": "src/engine.py"}
_fold_node_aliases(node)
assert node["source_file"] == "src/engine.py"
assert "src" not in node
def test_node_source_file_wins_over_alias():
node = {"id": "a", "label": "A", "source_file": "real.py", "file": "other.py"}
_fold_node_aliases(node)
+14
View File
@@ -51,6 +51,20 @@ def _write_graph(tmp_path: Path, graph: dict) -> Path:
# ── build_tree unit tests ─────────────────────────────────────────────────────
def test_build_tree_accepts_src_and_file_aliases():
"""build_tree resolves file paths from aliases like `src` or `file` so graphs
without explicit `source_file` do not render an empty tree."""
graph = {
"nodes": [
{"id": "a", "label": "fn_a", "src": "pkg/sub/a.py"},
{"id": "b", "label": "fn_b", "file": "pkg/sub/b.py"},
]
}
tree = build_tree(graph)
assert tree["name"] != "(empty graph)"
assert tree["total_count"] == 2
def test_build_tree_explicit_root_matching_nothing_raises():
graph = _graph({"pkg/a.py": "func_a", "pkg/sub/b.py": "func_b"})
with pytest.raises(ValueError, match=r"matched 0 of 2 source files"):