Skip to content

Commit ce942e1

Browse files
rajarshidattapysafishamsi
authored andcommitted
fix(extract): canonicalize source_file to POSIX separators (#2627)
1 parent 33283f5 commit ce942e1

3 files changed

Lines changed: 84 additions & 5 deletions

File tree

‎graphify/extract.py‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import textwrap
1111
from collections import Counter
1212
from dataclasses import dataclass, field
13-
from pathlib import Path
13+
from pathlib import Path, PurePath
1414
from typing import Any, Callable
1515

1616
from .cache import load_cached, save_cached
@@ -6574,6 +6574,31 @@ def _canon(nid: str) -> str:
65746574
for e in all_edges:
65756575
e["_origin"] = "ast"
65766576

6577+
# Canonicalize source_file to POSIX on every node AND edge (#2625).
6578+
#
6579+
# Extractors build source_file from the Path they were handed, so a run
6580+
# given RELATIVE inputs keeps the native separator on Windows. Only the
6581+
# relativizing branch of _sf_entry above ever calls as_posix(), so a single
6582+
# extraction could emit `src\lib\content.ts` and `src/pages/index.astro`
6583+
# side by side. source_file is compared as a STRING downstream
6584+
# (build._norm_source_file keying, _derive_prune_root, dedup, and
6585+
# analyze.find_import_cycles, which matches an edge's source_file against a
6586+
# node's by equality), so two spellings are two different files — the
6587+
# fragmentation of #683, and the reason the CLI path (which passes an
6588+
# explicit root) looked correct while the library entry point did not.
6589+
#
6590+
# Safe as a final pass: ids are minted through make_id, which collapses
6591+
# every non-word character — `\` and `/` alike — to `_`, so canonicalizing
6592+
# the separator here cannot desync an id from its source_file.
6593+
#
6594+
# PurePath is the NATIVE flavour on purpose: on POSIX a backslash is a legal
6595+
# filename character and must be left alone, so this only rewrites paths on
6596+
# the platform where `\` is actually a separator.
6597+
for _item in (*all_nodes, *all_edges):
6598+
_sf = _item.get("source_file")
6599+
if _sf and "\\" in str(_sf):
6600+
_item["source_file"] = PurePath(_sf).as_posix()
6601+
65776602
return {
65786603
"nodes": all_nodes,
65796604
"edges": all_edges,

‎tests/test_extract.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3328,3 +3328,54 @@ def test_rewire_does_not_bind_supertype_stub_to_function():
33283328
"source_file": "store.py", "weight": 1.0}]
33293329
_rewire_unique_stub_nodes(nodes, edges)
33303330
assert edges[0]["target"] == "BookStore" # inherits stub not bound to function
3331+
3332+
3333+
def test_extract_emits_posix_source_file_for_relative_inputs(tmp_path):
3334+
r"""source_file must be canonical POSIX on every node AND edge, whatever
3335+
separator the caller's input paths used.
3336+
3337+
Extractors build source_file from the Path handed to them, and only the
3338+
relativizing branch of extract()'s remap calls as_posix(), so a run given
3339+
relative inputs used to keep the native separator on Windows — mixing
3340+
`src\lib\content.ts` and `src/pages/index.astro` in one extraction.
3341+
source_file is compared as a string downstream (build keying, prune-root
3342+
derivation, dedup, analyze.find_import_cycles), so two spellings are two
3343+
different files (#683 / #2625).
3344+
3345+
Uses the relative-input form deliberately: passing an explicit ``root``
3346+
takes the branch that already normalized, and would make this vacuous.
3347+
"""
3348+
(tmp_path / "src" / "lib").mkdir(parents=True)
3349+
(tmp_path / "src" / "pages").mkdir(parents=True)
3350+
(tmp_path / "src" / "lib" / "content.ts").write_text(
3351+
"export function getPosts() { return []; }\n", encoding="utf-8"
3352+
)
3353+
(tmp_path / "src" / "pages" / "index.astro").write_text(
3354+
"---\nimport { getPosts } from '../lib/content';\n"
3355+
"const posts = getPosts();\n---\n<h1>{posts.length}</h1>\n",
3356+
encoding="utf-8",
3357+
)
3358+
3359+
cwd = os.getcwd()
3360+
os.chdir(tmp_path)
3361+
try:
3362+
result = extract([Path("src/lib/content.ts"), Path("src/pages/index.astro")])
3363+
finally:
3364+
os.chdir(cwd)
3365+
3366+
carriers = [
3367+
(kind, item.get("source_file"))
3368+
for kind, items in (("node", result["nodes"]), ("edge", result["edges"]))
3369+
for item in items
3370+
if item.get("source_file")
3371+
]
3372+
assert carriers, "fixture produced nothing with a source_file; test would be vacuous"
3373+
3374+
offenders = [(kind, sf) for kind, sf in carriers if "\\" in sf]
3375+
assert not offenders, f"native separator survived into source_file: {offenders}"
3376+
3377+
# ...and both files are present under one spelling each, so the graph sees
3378+
# two files rather than four.
3379+
assert {sf for _, sf in carriers} == {
3380+
"src/lib/content.ts", "src/pages/index.astro",
3381+
}

‎tests/test_js_import_resolution.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1407,8 +1407,11 @@ def test_alias_import_does_not_remap_an_owned_symbol_id(tmp_path, monkeypatch):
14071407
}
14081408
target_symbol = _make_id(_file_stem(target), "formatDate")
14091409
mirror_symbol = _make_id(_file_stem(mirror), "formatDate")
1410-
assert symbols[str(target)] == target_symbol
1411-
assert symbols[str(mirror)] == mirror_symbol
1410+
# as_posix, not str: source_file is canonical POSIX in extract() output
1411+
# (#2625), while str(Path(...)) is the native spelling and so only matched
1412+
# on POSIX hosts.
1413+
assert symbols[target.as_posix()] == target_symbol
1414+
assert symbols[mirror.as_posix()] == mirror_symbol
14121415

14131416
imports = [
14141417
edge
@@ -1418,8 +1421,8 @@ def test_alias_import_does_not_remap_an_owned_symbol_id(tmp_path, monkeypatch):
14181421
by_source: dict[str, list[str]] = {}
14191422
for edge in imports:
14201423
by_source.setdefault(edge["source_file"], []).append(edge["target"])
1421-
assert by_source[str(button)] == [target_symbol]
1422-
assert by_source[str(mirror_user)] == [mirror_symbol]
1424+
assert by_source[button.as_posix()] == [target_symbol]
1425+
assert by_source[mirror_user.as_posix()] == [mirror_symbol]
14231426
assert all(edge["source"] in node_ids and edge["target"] in node_ids for edge in imports)
14241427

14251428

0 commit comments

Comments
 (0)