fix(dedup): no id-collision warning for shared module nodes
Every Swift file that imports a module (`import Foundation`) mints the
same module node id by design, and the extractors already exempt
module/namespace nodes from collision disambiguation.
deduplicate_entities still reported each such group as a lost-node ID
collision, printing a spurious WARNING for every extra importing file.
That noise trains users to ignore the real data-loss warning.
_report_id_collision now stays silent only when every member of the
group is a module (or namespace) node of the same type under the same
label. A module colliding with a namespace, with any other type, or
with a differently named module still warns.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 0565d66408)
This commit is contained in:
1 parent
5cd4edcc24
commit
12f0fd6f15
2 files changed
+86
No files matched your search
@@ -609,6 +609,9 @@ def _merge_missing_attributes(survivor: dict, duplicate: dict) -> dict:
|
||||
return merged
|
||||
|
||||
|
||||
_SHARED_ID_TYPES = ("module", "namespace")
|
||||
|
||||
|
||||
def _report_id_collision(nid: str, survivor: dict, losers: list[dict]) -> None:
|
||||
"""Report an ID collision in proportion to what dropping the loser actually costs.
|
||||
|
||||
@@ -619,6 +622,18 @@ def _report_id_collision(nid: str, survivor: dict, losers: list[dict]) -> None:
|
||||
and one is genuinely lost — warn, and point at the extraction split that keeps them
|
||||
apart (#1504).
|
||||
"""
|
||||
# A module/namespace node (`import Foundation`) is one shared entity that every
|
||||
# importing file legitimately mints — the same rule the extractors apply when
|
||||
# they exempt these types from collision disambiguation. Only a group of one
|
||||
# such type under one label is that shared entity; a module colliding with a
|
||||
# namespace, or two differently named modules, is still a real collision.
|
||||
group = (survivor, *losers)
|
||||
if (
|
||||
survivor.get("type") in _SHARED_ID_TYPES
|
||||
and all(n.get("type") == survivor.get("type") for n in group)
|
||||
and all(n.get("label") == survivor.get("label") for n in group)
|
||||
):
|
||||
return
|
||||
keep_file = survivor.get("source_file") or ""
|
||||
keep_label = survivor.get("label") or ""
|
||||
for loser in losers:
|
||||
|
||||
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
from pathlib import Path
|
||||
|
||||
from graphify.build import build_from_json
|
||||
from graphify.dedup import deduplicate_entities
|
||||
from graphify.extract import extract
|
||||
|
||||
|
||||
@@ -83,3 +84,73 @@ def test_swift_import_edges_survive_build(tmp_path: Path):
|
||||
assert len(import_edges) == 2
|
||||
# Both edges land on the same CoreKit module node.
|
||||
assert len({v for _, v in import_edges}) == 1
|
||||
|
||||
|
||||
def _foundation_nodes(tmp_path: Path) -> list[dict]:
|
||||
a = _write(tmp_path / "Sources/AKit/AKit.swift", "import Foundation\n\npublic struct AKit {}\n")
|
||||
b = _write(tmp_path / "Sources/BKit/BKit.swift", "import Foundation\n\npublic struct BKit {}\n")
|
||||
result = extract([a, b], cache_root=tmp_path)
|
||||
foundation = _module_nodes(result, "Foundation")
|
||||
# Precondition: two files mint the same module id from different source files.
|
||||
assert len(foundation) == 2
|
||||
assert len({n["id"] for n in foundation}) == 1
|
||||
assert len({n["source_file"] for n in foundation}) == 2
|
||||
return result["nodes"]
|
||||
|
||||
|
||||
def test_shared_module_import_prints_no_collision_warning(tmp_path: Path, capsys):
|
||||
# Every importing file mints the same module id by design, so
|
||||
# deduplicate_entities must not report it as a lost-node ID collision.
|
||||
# extract() never reaches deduplicate_entities (only build() does), so call it directly.
|
||||
nodes = _foundation_nodes(tmp_path)
|
||||
capsys.readouterr()
|
||||
|
||||
deduplicate_entities(nodes, [], communities={})
|
||||
|
||||
err = capsys.readouterr().err
|
||||
assert "minted by two different files" not in err
|
||||
assert "WARNING" not in err
|
||||
|
||||
|
||||
def test_mixed_module_collision_still_warns(tmp_path: Path, capsys):
|
||||
# Control: a module node colliding with a non-module node is a real collision.
|
||||
nodes = _foundation_nodes(tmp_path)
|
||||
module_id = next(n["id"] for n in nodes if n.get("type") == "module")
|
||||
nodes.append({
|
||||
"id": module_id,
|
||||
"label": "Foundation",
|
||||
"type": "class",
|
||||
"source_file": str(tmp_path / "Sources/CKit/CKit.swift"),
|
||||
})
|
||||
capsys.readouterr()
|
||||
|
||||
deduplicate_entities(nodes, [], communities={})
|
||||
|
||||
err = capsys.readouterr().err
|
||||
assert f"node '{module_id}' is minted by two different files" in err
|
||||
|
||||
|
||||
def test_module_namespace_collision_still_warns(capsys):
|
||||
# Regression: a module and a namespace sharing an id are distinct entities;
|
||||
# only a group of one shared type under one label is exempt.
|
||||
nodes = [
|
||||
{"id": "shared_id", "type": "module", "label": "Foundation", "source_file": "a.swift"},
|
||||
{"id": "shared_id", "type": "namespace", "label": "Other", "source_file": "b.cs"},
|
||||
]
|
||||
|
||||
deduplicate_entities(nodes, [], communities={})
|
||||
|
||||
err = capsys.readouterr().err
|
||||
assert "node 'shared_id' is minted by two different files" in err
|
||||
|
||||
|
||||
def test_differently_named_modules_collision_still_warns(capsys):
|
||||
nodes = [
|
||||
{"id": "shared_id", "type": "module", "label": "Foundation", "source_file": "a.swift"},
|
||||
{"id": "shared_id", "type": "module", "label": "Other", "source_file": "b.swift"},
|
||||
]
|
||||
|
||||
deduplicate_entities(nodes, [], communities={})
|
||||
|
||||
err = capsys.readouterr().err
|
||||
assert "node 'shared_id' is minted by two different files" in err
|
||||
Reference in new issue
Block a user