fix(build): keep the reverse import of a two-file cycle in the undirected build (#4839)
The undirected build drops the reverse-direction duplicate of a same-relation
pair (#1061). For imports_from/re_exports that dropped edge is the second leg
of a two-file import cycle, so find_import_cycles never saw it and
GRAPH_REPORT.md said "None detected".
Stamp the kept edge with reverse_import when the dropped leg is a runtime
import, and have find_import_cycles add the reverse file leg for stamped
edges. The incremental reconcile treats the stamp as the target file's import,
so it is cleared or carried along with that file.
Co-Authored-By: Grok Bot
(cherry picked from commit 8a953f803a)
This commit is contained in:
1 parent
3ca53c70ac
commit
9b35e29ac5
4 files changed
+222
-14
No files matched your search
+9
-4
@@ -761,12 +761,14 @@ def find_import_cycles(
|
||||
|
||||
# Deferred `import(...)` edges are real dependencies but do not form a
|
||||
# hard file-level cycle, so they are excluded from cycle detection (#1241).
|
||||
if data.get("deferred"):
|
||||
continue
|
||||
# Type-only imports/re-exports (`import type` / `export type ... from`)
|
||||
# are erased at compile time - a cycle that closes through one cannot
|
||||
# exist at runtime (#3123). The edge itself stays in the graph.
|
||||
if data.get("type_only"):
|
||||
forward = not data.get("deferred") and not data.get("type_only")
|
||||
# An undirected build keeps one edge per file pair, so the other file's
|
||||
# runtime import back is only a stamp on this edge (#4839).
|
||||
reverse = bool(data.get("reverse_import"))
|
||||
if not forward and not reverse:
|
||||
continue
|
||||
|
||||
src_file_attr = data.get("source_file", "")
|
||||
@@ -791,7 +793,10 @@ def find_import_cycles(
|
||||
if not tgt_file:
|
||||
continue
|
||||
|
||||
file_graph.add_edge(src_file_attr, tgt_file)
|
||||
if forward:
|
||||
file_graph.add_edge(src_file_attr, tgt_file)
|
||||
if reverse:
|
||||
file_graph.add_edge(tgt_file, src_file_attr)
|
||||
|
||||
if not file_graph.edges():
|
||||
return []
|
||||
|
||||
@@ -1970,6 +1970,15 @@ def build_from_json(extraction: dict, *, directed: bool = False, root: str | Pat
|
||||
incoming_rel = attrs.get("relation")
|
||||
reverse = existing.get("_src") == tgt and existing.get("_tgt") == src
|
||||
if reverse and existing_rel == incoming_rel:
|
||||
# For an import the dropped leg is the other half of a two-file
|
||||
# cycle. Stamp the kept edge so find_import_cycles still sees it
|
||||
# (#4839); a type-only or deferred import closes no cycle.
|
||||
if (
|
||||
incoming_rel in ("imports_from", "re_exports")
|
||||
and not attrs.get("type_only")
|
||||
and not attrs.get("deferred")
|
||||
):
|
||||
existing["reverse_import"] = True
|
||||
continue
|
||||
# Different relations in opposite directions are two facts that only
|
||||
# share a pair because the graph is undirected. A type's
|
||||
|
||||
+41
-10
@@ -1246,21 +1246,52 @@ def _reconcile_existing_graph(
|
||||
# until a semantic re-extraction supersedes them. Same provenance rule
|
||||
# the node reconciliation above applies via _origin (#1865). Deletion
|
||||
# eviction stays provenance-blind.
|
||||
def _edge_evicted(edge: dict) -> bool:
|
||||
return not source_paths.is_evicted(edge, excluded_alive_files) and (
|
||||
source_paths.is_evicted(edge, edge_evicted_source_identities)
|
||||
or (
|
||||
_is_ast_tier(edge)
|
||||
and source_paths.is_evicted(edge, rebuilt_source_identities)
|
||||
)
|
||||
)
|
||||
|
||||
existing_edges = existing.get("links", existing.get("edges", []))
|
||||
preserved_edges = [
|
||||
edge
|
||||
for edge in existing.get("links", existing.get("edges", []))
|
||||
for edge in existing_edges
|
||||
if edge.get("source") in all_ids
|
||||
and edge.get("target") in all_ids
|
||||
and not (
|
||||
source_paths.is_evicted(edge, edge_evicted_source_identities)
|
||||
and not source_paths.is_evicted(edge, excluded_alive_files)
|
||||
)
|
||||
and not (
|
||||
_is_ast_tier(edge)
|
||||
and source_paths.is_evicted(edge, rebuilt_source_identities)
|
||||
and not source_paths.is_evicted(edge, excluded_alive_files)
|
||||
)
|
||||
and not _edge_evicted(edge)
|
||||
]
|
||||
# `reverse_import` stands for the target file's import back, which the
|
||||
# undirected build dropped in favour of this edge (#4839). That import
|
||||
# is the target file's, so it goes when an edge from that file would:
|
||||
# clear the stamp then (re-extracting the file stamps it again), and
|
||||
# move it onto the fresh edge when only this edge's own file was
|
||||
# re-extracted.
|
||||
node_files = {
|
||||
n.get("id"): n.get("source_file")
|
||||
for n in existing.get("nodes", []) if isinstance(n, dict)
|
||||
}
|
||||
|
||||
def _reverse_leg_evicted(edge: dict) -> bool:
|
||||
return _edge_evicted({**edge, "source_file": node_files.get(edge.get("target"))})
|
||||
|
||||
preserved_edges = [
|
||||
{k: v for k, v in edge.items() if k != "reverse_import"}
|
||||
if edge.get("reverse_import") and _reverse_leg_evicted(edge) else edge
|
||||
for edge in preserved_edges
|
||||
]
|
||||
carried_reverse = {
|
||||
(edge.get("source"), edge.get("target"), edge.get("relation"))
|
||||
for edge in existing_edges
|
||||
if edge.get("reverse_import")
|
||||
and _edge_evicted(edge)
|
||||
and not _reverse_leg_evicted(edge)
|
||||
}
|
||||
for edge in result["edges"]:
|
||||
if (edge.get("source"), edge.get("target"), edge.get("relation")) in carried_reverse:
|
||||
edge["reverse_import"] = True
|
||||
|
||||
preserved_edges = _reconcile_markdown_links(
|
||||
result,
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
"""Two files importing each other are an Import Cycle in the default build (#4839).
|
||||
|
||||
The undirected build keeps one edge per file pair and drops the reverse-direction
|
||||
duplicate (#1061). For `imports_from`/`re_exports` that dropped leg is the other
|
||||
half of the cycle, so it is kept as a `reverse_import` stamp on the surviving
|
||||
edge and `find_import_cycles` reads it back.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from graphify.analyze import find_import_cycles
|
||||
from graphify.build import build_from_json
|
||||
from graphify.export import to_json
|
||||
from graphify.extract import extract
|
||||
|
||||
|
||||
def _extract(tmp_path, files: dict[str, str]):
|
||||
for name, body in files.items():
|
||||
p = tmp_path / name
|
||||
p.parent.mkdir(parents=True, exist_ok=True)
|
||||
p.write_text(body, encoding="utf-8")
|
||||
return extract([tmp_path / n for n in files], cache_root=Path(tempfile.mkdtemp()),
|
||||
root=tmp_path, parallel=False)
|
||||
|
||||
|
||||
def _build(tmp_path, files, directed=False):
|
||||
r = _extract(tmp_path, files)
|
||||
return build_from_json({"nodes": r["nodes"], "edges": r["edges"], "hyperedges": []},
|
||||
root=str(tmp_path), directed=directed)
|
||||
|
||||
|
||||
def _names(cycles):
|
||||
return sorted(sorted(Path(f).name for f in c["cycle"]) for c in cycles)
|
||||
|
||||
|
||||
_TS_CYCLE = {
|
||||
"src/a.ts": "import { B } from './b';\nexport class A { b?: B; }\n",
|
||||
"src/b.ts": "import { A } from './a';\nexport class B { a?: A; }\n",
|
||||
}
|
||||
_PY_CYCLE = {
|
||||
"a.py": "from b import B\n\nclass A:\n b: B = None\n",
|
||||
"b.py": "from a import A\n\nclass B:\n a: A = None\n",
|
||||
}
|
||||
|
||||
|
||||
@pytest.mark.parametrize("files, expected", [
|
||||
(_TS_CYCLE, [["a.ts", "b.ts"]]),
|
||||
(_PY_CYCLE, [["a.py", "b.py"]]),
|
||||
], ids=["ts", "python"])
|
||||
def test_two_file_cycle_is_reported_in_the_undirected_build(tmp_path, files, expected):
|
||||
G = _build(tmp_path, files)
|
||||
imports = [d for *_e, d in G.edges(data=True) if d.get("relation") == "imports_from"]
|
||||
assert len(imports) == 1 # still one stored edge per pair (#1061)
|
||||
assert imports[0].get("reverse_import") is True
|
||||
assert _names(find_import_cycles(G)) == expected
|
||||
|
||||
|
||||
def test_a_one_way_import_is_not_a_cycle(tmp_path):
|
||||
G = _build(tmp_path, {
|
||||
"a.py": "from b import B\n\nclass A:\n b: B = None\n",
|
||||
"b.py": "class B:\n pass\n",
|
||||
})
|
||||
assert not any(d.get("reverse_import") for *_e, d in G.edges(data=True))
|
||||
assert find_import_cycles(G) == []
|
||||
|
||||
|
||||
def test_longer_cycles_are_still_reported_beside_a_two_file_one(tmp_path):
|
||||
G = _build(tmp_path, {
|
||||
"a.py": "from b import B\nclass A: pass\n",
|
||||
"b.py": "from a import A\nfrom c import C\nclass B: pass\n",
|
||||
"c.py": "from a import A\nclass C: pass\n",
|
||||
})
|
||||
assert _names(find_import_cycles(G)) == [["a.py", "b.py"], ["a.py", "b.py", "c.py"]]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("files", [
|
||||
{"a.ts": "import { B } from './b';\nexport type AT = number;\nexport const a = 1;\n",
|
||||
"b.ts": "import type { AT } from './a';\nexport class B {}\n"},
|
||||
{"a.py": "from b import B\n\nclass A:\n b: B = None\n",
|
||||
"b.py": ("from typing import TYPE_CHECKING\nif TYPE_CHECKING:\n from a import A\n\n"
|
||||
"class B:\n a: 'A' = None\n")},
|
||||
], ids=["ts-import-type", "python-type-checking"])
|
||||
def test_a_type_only_reverse_import_closes_no_cycle(tmp_path, files):
|
||||
G = _build(tmp_path, files)
|
||||
assert not any(d.get("reverse_import") for *_e, d in G.edges(data=True))
|
||||
assert find_import_cycles(G) == []
|
||||
|
||||
|
||||
def _edge(src, tgt, rel="imports_from", **extra):
|
||||
return {"source": src, "target": tgt, "relation": rel, "confidence": "EXTRACTED",
|
||||
"source_file": f"{src}.py", "source_location": "L1", **extra}
|
||||
|
||||
|
||||
def _graph(edges, directed=False):
|
||||
nodes = [{"id": n, "label": f"{n}.py", "source_file": f"{n}.py", "file_type": "code"}
|
||||
for n in ("a", "b")]
|
||||
return build_from_json({"nodes": nodes, "edges": edges, "hyperedges": []}, directed=directed)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("reverse", [
|
||||
_edge("b", "a", "calls"),
|
||||
_edge("b", "a", "imports"),
|
||||
_edge("b", "a", deferred=True),
|
||||
_edge("b", "a", type_only=True),
|
||||
], ids=["other-relation", "symbol-imports", "deferred", "type-only"])
|
||||
def test_only_a_runtime_reverse_import_stamps_the_kept_edge(reverse):
|
||||
G = _graph([_edge("a", "b"), reverse])
|
||||
assert not any(d.get("reverse_import") for *_e, d in G.edges(data=True))
|
||||
assert find_import_cycles(G) == []
|
||||
|
||||
|
||||
def test_the_directed_build_is_unchanged():
|
||||
G = _graph([_edge("a", "b"), _edge("b", "a")], directed=True)
|
||||
assert G.number_of_edges() == 2
|
||||
assert not any("reverse_import" in d for *_e, d in G.edges(data=True))
|
||||
assert _names(find_import_cycles(G)) == [["a.py", "b.py"]]
|
||||
|
||||
|
||||
def test_the_cycle_survives_a_graph_json_round_trip(tmp_path):
|
||||
G = _build(tmp_path, _TS_CYCLE)
|
||||
graph_path = tmp_path / "graph.json"
|
||||
assert to_json(G, {}, str(graph_path), force=True)
|
||||
data = json.loads(graph_path.read_text(encoding="utf-8"))
|
||||
G2 = build_from_json({"nodes": data["nodes"], "edges": data["links"], "hyperedges": []},
|
||||
directed=bool(data.get("directed")))
|
||||
assert _names(find_import_cycles(G2)) == [["a.ts", "b.ts"]]
|
||||
|
||||
|
||||
# Incremental (hook) rebuilds ------------------------------------------------
|
||||
|
||||
def _rebuild_cycles(root, changed=None):
|
||||
from graphify.watch import _rebuild_code
|
||||
|
||||
assert _rebuild_code(root, changed_paths=changed, acquire_lock=False, force=True)
|
||||
data = json.loads((root / "graphify-out" / "graph.json").read_text(encoding="utf-8"))
|
||||
G = build_from_json({"nodes": data["nodes"], "edges": data["links"], "hyperedges": []},
|
||||
directed=bool(data.get("directed")))
|
||||
return _names(find_import_cycles(G))
|
||||
|
||||
|
||||
def test_an_incremental_rebuild_drops_the_stamp_with_the_reverse_import(tmp_path):
|
||||
"""The stamp is b.py's import, stored on a.py's edge: when b.py stops
|
||||
importing a.py, re-extracting b.py alone must not leave a stale cycle."""
|
||||
for name, body in _PY_CYCLE.items():
|
||||
(tmp_path / name).write_text(body, encoding="utf-8")
|
||||
assert _rebuild_cycles(tmp_path) == [["a.py", "b.py"]]
|
||||
(tmp_path / "b.py").write_text("class B:\n pass\n", encoding="utf-8")
|
||||
assert _rebuild_cycles(tmp_path, [tmp_path / "b.py"]) == []
|
||||
|
||||
|
||||
def test_an_incremental_rebuild_of_the_kept_edges_file_keeps_the_cycle(tmp_path):
|
||||
for name, body in _PY_CYCLE.items():
|
||||
(tmp_path / name).write_text(body, encoding="utf-8")
|
||||
assert _rebuild_cycles(tmp_path) == [["a.py", "b.py"]]
|
||||
(tmp_path / "a.py").write_text("# edited\n" + _PY_CYCLE["a.py"], encoding="utf-8")
|
||||
assert _rebuild_cycles(tmp_path, [tmp_path / "a.py"]) == [["a.py", "b.py"]]
|
||||
(tmp_path / "a.py").write_text("class A:\n pass\n", encoding="utf-8")
|
||||
assert _rebuild_cycles(tmp_path, [tmp_path / "a.py"]) == []
|
||||
Reference in new issue
Block a user