fix: preserve cross-file edges during incremental updates
(cherry picked from commit b6f3adffc1)
This commit is contained in:
1 parent
8595749021
commit
89280317ed
3 files changed
+149
-1
No files matched your search
@@ -7489,6 +7489,7 @@ def extract(
|
||||
_augment_symbol_resolution_edges(
|
||||
paths, all_nodes, all_edges, root,
|
||||
ambiguous_python_modules=ambiguous_python_modules,
|
||||
resolution_context_nodes=resolution_context_nodes,
|
||||
)
|
||||
|
||||
# Merge a header-declared class (and its methods) with its sibling-impl
|
||||
|
||||
@@ -1117,6 +1117,7 @@ def _apply_symbol_resolution_facts(
|
||||
edges: list[dict],
|
||||
root: Path,
|
||||
facts: _SymbolResolutionFacts,
|
||||
resolution_context_nodes: list[dict] | None = None,
|
||||
) -> None:
|
||||
"""Apply language-provided import/export/use facts to graph edges."""
|
||||
if not (
|
||||
@@ -1140,6 +1141,30 @@ def _apply_symbol_resolution_facts(
|
||||
# name to a class/interface member (#3436). Track those keys separately
|
||||
# and never let a member shadow a same-named top-level symbol.
|
||||
member_symbol_keys: set[tuple[Path, str]] = set()
|
||||
|
||||
if resolution_context_nodes:
|
||||
_fresh_node_ids = {n.get("id") for n in nodes if n.get("id")}
|
||||
_fresh_paths = {_resolve_cached(p) for p in paths}
|
||||
for node in resolution_context_nodes:
|
||||
nid = node.get("id")
|
||||
if not nid or nid in _fresh_node_ids:
|
||||
continue
|
||||
source_path = _js_source_path(str(node.get("source_file", "")), root)
|
||||
if source_path is None or source_path in _fresh_paths:
|
||||
continue
|
||||
raw_label = str(node.get("label", "")).strip()
|
||||
label = raw_label.strip("()").lstrip(".")
|
||||
if not label:
|
||||
continue
|
||||
key = (source_path, label)
|
||||
if raw_label.startswith("."):
|
||||
if key in symbol_nodes:
|
||||
continue
|
||||
member_symbol_keys.add(key)
|
||||
else:
|
||||
member_symbol_keys.discard(key)
|
||||
symbol_nodes[key] = str(nid)
|
||||
|
||||
for node in nodes:
|
||||
source_path = _js_source_path(str(node.get("source_file", "")), root)
|
||||
if source_path is None:
|
||||
@@ -2479,6 +2504,7 @@ def _augment_symbol_resolution_edges(
|
||||
edges: list[dict],
|
||||
root: Path,
|
||||
ambiguous_python_modules: set[str] | None = None,
|
||||
resolution_context_nodes: list[dict] | None = None,
|
||||
) -> None:
|
||||
facts = _SymbolResolutionFacts()
|
||||
_collect_js_symbol_resolution_facts(paths, facts)
|
||||
@@ -2486,7 +2512,13 @@ def _augment_symbol_resolution_edges(
|
||||
paths, root, facts,
|
||||
ambiguous_python_modules=ambiguous_python_modules,
|
||||
)
|
||||
_apply_symbol_resolution_facts(paths, nodes, edges, root, facts)
|
||||
if resolution_context_nodes:
|
||||
_apply_symbol_resolution_facts(
|
||||
paths, nodes, edges, root, facts,
|
||||
resolution_context_nodes=resolution_context_nodes,
|
||||
)
|
||||
else:
|
||||
_apply_symbol_resolution_facts(paths, nodes, edges, root, facts)
|
||||
|
||||
def _resolve_cross_file_imports(
|
||||
per_file: list[dict],
|
||||
|
||||
@@ -394,3 +394,118 @@ def test_update_prunes_a_removed_imports_edge(tmp_path):
|
||||
if e.get("relation") in ("imports", "imports_from")
|
||||
and str(e.get("source_file", "")).endswith("a.py")]
|
||||
assert not stale, f"removed import's edge survived update (stale): {stale}"
|
||||
|
||||
|
||||
def test_update_preserves_cross_file_imports_and_calls_to_unchanged_file(tmp_path):
|
||||
"""#3776: incremental update must resolve symbols defined in unchanged files."""
|
||||
proj = tmp_path / "proj"
|
||||
proj.mkdir()
|
||||
(proj / "b.py").write_text("def helper():\n return 1\n", encoding="utf-8")
|
||||
a = proj / "a.py"
|
||||
a.write_text(
|
||||
"from b import helper\n\n"
|
||||
"def use():\n"
|
||||
" return helper()\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
r1 = _run(["extract", str(proj), "--no-cluster"], tmp_path)
|
||||
assert r1.returncode == 0, r1.stderr
|
||||
gj = proj / "graphify-out" / "graph.json"
|
||||
before = _edges(gj)
|
||||
|
||||
# Initial build must contain both the imports edge and the calls edge
|
||||
has_import_before = any(
|
||||
e.get("relation") == "imports"
|
||||
and e.get("source") == "a"
|
||||
and e.get("target") == "b_helper"
|
||||
for e in before
|
||||
)
|
||||
assert has_import_before, f"expected initial imports edge a -> b_helper: {before}"
|
||||
|
||||
has_call_before = any(
|
||||
e.get("relation") == "calls"
|
||||
and e.get("source") == "a_use"
|
||||
and e.get("target") == "b_helper"
|
||||
for e in before
|
||||
)
|
||||
assert has_call_before, f"expected initial calls edge a_use -> b_helper: {before}"
|
||||
|
||||
# Modify only a.py with an unrelated change
|
||||
a.write_text(
|
||||
"from b import helper\n\n"
|
||||
"def use():\n"
|
||||
" # unrelated change\n"
|
||||
" return helper()\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
r2 = _run(["extract", str(proj), "--code-only", "--no-cluster"], tmp_path)
|
||||
assert r2.returncode == 0, r2.stderr
|
||||
after = _edges(gj)
|
||||
|
||||
# Incremental update must preserve the imports edge to the unchanged file
|
||||
has_import_after = any(
|
||||
e.get("relation") == "imports"
|
||||
and e.get("source") == "a"
|
||||
and e.get("target") == "b_helper"
|
||||
for e in after
|
||||
)
|
||||
assert has_import_after, f"imports edge a -> b_helper lost after incremental update: {after}"
|
||||
|
||||
# Incremental update must preserve the calls edge to the unchanged file
|
||||
has_call_after = any(
|
||||
e.get("relation") == "calls"
|
||||
and e.get("source") == "a_use"
|
||||
and e.get("target") == "b_helper"
|
||||
for e in after
|
||||
)
|
||||
assert has_call_after, f"calls edge a_use -> b_helper lost after incremental update: {after}"
|
||||
|
||||
|
||||
def test_update_does_not_resurrect_a_dropped_cross_file_edge_via_context(tmp_path):
|
||||
"""#3776 negative: the resolution-context index widens the *target* view with
|
||||
unchanged files, so it must not resurrect an edge the edited file no longer
|
||||
warrants. Here b.py (with `helper`) stays unchanged and a.py drops its import
|
||||
and call — the a -> b_helper edges must NOT survive just because b_helper is
|
||||
still visible through the context index."""
|
||||
proj = tmp_path / "proj"
|
||||
proj.mkdir()
|
||||
(proj / "b.py").write_text("def helper():\n return 1\n", encoding="utf-8")
|
||||
a = proj / "a.py"
|
||||
a.write_text(
|
||||
"from b import helper\n\n"
|
||||
"def use():\n"
|
||||
" return helper()\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
r1 = _run(["extract", str(proj), "--no-cluster"], tmp_path)
|
||||
assert r1.returncode == 0, r1.stderr
|
||||
gj = proj / "graphify-out" / "graph.json"
|
||||
before = _edges(gj)
|
||||
assert any(
|
||||
e.get("relation") == "imports"
|
||||
and e.get("source") == "a"
|
||||
and e.get("target") == "b_helper"
|
||||
for e in before
|
||||
), f"expected initial imports edge a -> b_helper: {before}"
|
||||
|
||||
# Edit ONLY a.py to drop the import and the call; b.py (and b_helper) unchanged.
|
||||
a.write_text(
|
||||
"def use():\n"
|
||||
" return 1\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
r2 = _run(["extract", str(proj), "--code-only", "--no-cluster"], tmp_path)
|
||||
assert r2.returncode == 0, r2.stderr
|
||||
after = _edges(gj)
|
||||
|
||||
stale = [
|
||||
e for e in after
|
||||
if e.get("source") in ("a", "a_use")
|
||||
and e.get("target") == "b_helper"
|
||||
and e.get("relation") in ("imports", "calls")
|
||||
]
|
||||
assert not stale, f"dropped cross-file edge resurrected via context index: {stale}"
|
||||
Reference in new issue
Block a user