fix(cache): keep the \\?\ long-path spelling out of cache identity
99b8da7andff8ddf5(#4925, #4937) open files through os_long_path(), but on Windows its \\?\ spelling also leaked into cache identity. file_hash() and cached_word_count() bound the stat index to the prefixed root, so the plain cache_root that extract() passes read as a second root. Every Windows `graphify update` that re-extracts warned "stat index switched" (#3989) and flushed mid-run, and since no key was relative to the prefixed anchor, stat-index.json was written with absolute keys and a moved checkout re-hashed every file (#2199). The walked-parent match compared a plain path with the prefixed root and never matched, so under a junctioned or symlinked scan root an in-root alias took its target's salt and digest (#2832), and the out-of-root salt carried a //?/ prefix. _normalize_path() also stripped only \\?\, so the UNC form \\?\UNC\server\share became a relative key that the flush pruned, and a corpus on a network or WSL share always wrote an empty stat index. Bind the index to the normalized root as before #4925, normalize the resolved path and root before they become keys, salts or comparisons, and map \\?\UNC\ back to \\. File I/O still goes through os_long_path(). CI runs on Linux, where both helpers are the identity, so this only showed on Windows. Co-Authored-By: Claude <noreply@anthropic.com> (cherry picked from commit49616eb09c)
This commit is contained in:
1 parent
5212304ff8
commit
43360efb0c
3 files changed
+67
-8
No files matched your search
+14
-8
@@ -510,7 +510,9 @@ def _normalize_path(path: Path) -> Path:
|
||||
if sys.platform != "win32":
|
||||
return path
|
||||
s = str(path)
|
||||
if s.startswith("\\\\?\\"):
|
||||
if s.startswith("\\\\?\\UNC\\"):
|
||||
s = "\\\\" + s[8:] # \\?\UNC\server\share -> \\server\share
|
||||
elif s.startswith("\\\\?\\"):
|
||||
s = s[4:] # strip extended-length prefix \\?\
|
||||
return Path(os.path.normpath(s))
|
||||
|
||||
@@ -548,9 +550,14 @@ def file_hash(path: Path, root: Path = Path("."), cache_root: "Path | None" = No
|
||||
# (cache_root), not the key-anchor root — otherwise it leaves a stray
|
||||
# graphify-out/cache/stat-index.json inside the analyzed source tree even when
|
||||
# the AST cache itself is redirected to CWD (#1774 completion).
|
||||
_ensure_stat_index(root_src, cache_root=cache_root)
|
||||
resolved = src.resolve()
|
||||
abs_key = str(_normalize_path(resolved))
|
||||
#
|
||||
# The index root, key and salt use the normalized spelling. The \\?\ form
|
||||
# from os_long_path() is for I/O only: as the root it made load_cached()'s
|
||||
# plain cache_root look like a second root (#3989), and no key was relative
|
||||
# to it, so every key was written absolute (#2199).
|
||||
_ensure_stat_index(_normalize_path(raw_root), cache_root=cache_root)
|
||||
resolved = _normalize_path(src.resolve())
|
||||
abs_key = str(resolved)
|
||||
# The salt is the path component that enters the digest (relative to root, or
|
||||
# the absolute-path fallback). The stat-index memo MUST be keyed by it too:
|
||||
# the same file hashed under two different roots yields two different digests
|
||||
@@ -558,7 +565,7 @@ def file_hash(path: Path, root: Path = Path("."), cache_root: "Path | None" = No
|
||||
# path served whichever was computed first — making file_hash order-dependent
|
||||
# and poisoning the persisted stat-index across runs (#1989). Store one digest
|
||||
# per salt so alternating roots don't force re-reads.
|
||||
resolved_root = root_src.resolve()
|
||||
resolved_root = _normalize_path(root_src.resolve())
|
||||
try:
|
||||
resolved_rel = resolved.relative_to(resolved_root)
|
||||
except ValueError:
|
||||
@@ -579,7 +586,7 @@ def file_hash(path: Path, root: Path = Path("."), cache_root: "Path | None" = No
|
||||
walked_rel = None
|
||||
for parent in walked.parents:
|
||||
try:
|
||||
if parent.resolve() == resolved_root:
|
||||
if _normalize_path(parent.resolve()) == resolved_root:
|
||||
walked_rel = walked.relative_to(parent)
|
||||
break
|
||||
except OSError:
|
||||
@@ -643,8 +650,7 @@ def cached_word_count(path: Path, root: Path, compute, cache_root: "Path | None"
|
||||
raw_path = Path(path)
|
||||
raw_root = Path(root)
|
||||
src = Path(_os_long_path(raw_path))
|
||||
root_src = Path(_os_long_path(raw_root))
|
||||
_ensure_stat_index(root_src, cache_root=cache_root)
|
||||
_ensure_stat_index(_normalize_path(raw_root), cache_root=cache_root)
|
||||
abs_key = str(_normalize_path(src.resolve()))
|
||||
st: "os.stat_result | None" = None
|
||||
try:
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
"""Tests for graphify/cache.py."""
|
||||
import os
|
||||
import pytest
|
||||
from pathlib import Path
|
||||
from graphify.cache import file_hash, cache_dir, load_cached, save_cached, cached_files, clear_cache, _body_content
|
||||
@@ -851,6 +852,27 @@ def test_warm_cache_keeps_target_and_symlink_sources_distinct(
|
||||
assert {Path(source).name for source in cold_counts} == {"real.py", "link.py"}
|
||||
|
||||
|
||||
@pytest.mark.skipif(os.name != "nt", reason="directory junctions are Windows-only")
|
||||
def test_file_hash_distinguishes_walked_junction_paths(tmp_path):
|
||||
r"""#2832 with junctions, which need no symlink privilege. Under a junctioned
|
||||
scan root the walked-parent match compared a plain spelling with the
|
||||
``\\?\`` form of the root (#4925), never matched, and the in-root alias fell
|
||||
back to its target's salt and shared its digest."""
|
||||
import subprocess
|
||||
|
||||
_reset_stat_index()
|
||||
root = tmp_path / "repo"
|
||||
(root / "sub").mkdir(parents=True)
|
||||
target = root / "sub" / "g.py"
|
||||
target.write_text("def g():\n return 1\n")
|
||||
for link, dest in ((root / "alias", root / "sub"), (tmp_path / "scan", root)):
|
||||
subprocess.run(["cmd", "/c", "mklink", "/J", str(link), str(dest)],
|
||||
check=True, capture_output=True)
|
||||
|
||||
walked_alias = tmp_path / "scan" / "alias" / "g.py"
|
||||
assert file_hash(walked_alias, root) != file_hash(target, root)
|
||||
|
||||
|
||||
def test_semantic_cache_self_heals_legacy_symlink_collision(
|
||||
requires_symlinks, tmp_path
|
||||
):
|
||||
|
||||
@@ -18,8 +18,11 @@ import hashlib
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import warnings
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from graphify import cache
|
||||
|
||||
|
||||
@@ -223,6 +226,34 @@ def test_relative_key_wins_over_colliding_legacy_absolute(tmp_path):
|
||||
assert cache._stat_index[str(f1.resolve())]["hashes"]["f1.py"] == "fresh"
|
||||
|
||||
|
||||
def test_normalize_path_undoes_the_unc_long_path_prefix(monkeypatch):
|
||||
r"""Since #4925 keys are normalized from the os_long_path() spelling, which
|
||||
for a UNC share is ``\\?\UNC\server\share``. Stripping only ``\\?\`` left a
|
||||
relative ``UNC\server\...`` key that the flush pruned as missing, so a
|
||||
network or WSL share always wrote an empty stat-index.json."""
|
||||
monkeypatch.setattr("sys.platform", "win32")
|
||||
got = cache._normalize_path(Path("\\\\?\\UNC\\server\\share\\repo\\a.py"))
|
||||
assert str(got) == "\\\\server\\share\\repo\\a.py"
|
||||
|
||||
|
||||
@pytest.mark.skipif(os.name != "nt", reason="Windows extended-length paths")
|
||||
def test_long_path_spelling_does_not_switch_the_root(tmp_path):
|
||||
r"""Files are opened through os_long_path() (#4925), which spells the root
|
||||
``\\?\C:\...``. Binding the stat index to that spelling made the plain
|
||||
cache_root that extract() passes look like a second root: a #3989 warning
|
||||
and a mid-run flush on every Windows update."""
|
||||
_reset_stat_index()
|
||||
f1 = tmp_path / "f1.py"
|
||||
f1.write_text("x = 1\n")
|
||||
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings("error", message="stat index switched")
|
||||
cache.cached_word_count(f1, tmp_path, lambda p: 2) # detect()
|
||||
cache.file_hash(f1, tmp_path)
|
||||
cache.load_cached(f1, tmp_path, cache_root=tmp_path) # extract()
|
||||
assert cache._stat_index_root == tmp_path.resolve()
|
||||
|
||||
|
||||
def test_semantic_cache_normalizes_absolute_source_file(tmp_path):
|
||||
"""#2197: an item whose source_file is absolute is persisted root-relative
|
||||
posix, and the caller's dict is not mutated."""
|
||||
|
||||
Reference in new issue
Block a user