Skip to content

Extraction cache keys on the resolved path, so a symlink displaces its target on rebuild #2832

Closed

Description

@duncancrawford

Extraction caches by resolved path, so a symlink and its target collide on any rebuild — and the link wins, silently reassigning the content of a real file to a link.

Reproduced on 0.9.45 (and 0.9.30) with no configuration, in three consecutive runs of the same extraction over the same two files:

from pathlib import Path
from graphify.extract import collect_files, extract
import collections

# repo/real.py  is a real file
# repo/sub/link.py -> ../real.py  is a symlink to it
root = Path("repo").resolve()
files = collect_files(root)
for run in (1, 2, 3):
    res = extract(files, cache_root=root)
    print(run, collections.Counter(n.get("source_file") for n in res["nodes"]))
run 1  {'real.py': 2, 'sub/link.py': 2}     <- both present
run 2  {'sub/link.py': 4}                   <- real.py has gone
run 3  {'sub/link.py': 4}

collect_files returns both paths every time. The difference is the cache: file_hash keys its stat index on str(resolved), so on a warm cache both paths hash to the same entry and one set of nodes is emitted — under whichever path was walked second.

Why this is worse than duplication

Run 1 is a visible problem: the same content twice under two paths, which inflates counts and can manufacture a hub. Runs 2 and 3 are an invisible one — the real file is absent from the graph, and its content is filed under a path that is a link.

The consequence for anything built on the graph:

  • "where is this declared?" returns the link
  • "what does real.py contain?" returns nothing at all
  • where several links share one target, every other link appears to contain nothing

So a graph built on a cold cache and one built on a warm cache disagree about which files exist, with no error, no warning, and identical node counts. The node total is the same in all three runs, which is why a count-based check cannot see it.

Suggested directions, in preference order

  1. Key the cache by walked path rather than resolved path — or include the walked path in the salt, as file_hash already does for the root. That preserves the cache's purpose while keeping the two paths distinct.
  2. Skip symlinks in collect_files by default, with a flag to follow them. Simpler, and arguably right: a symlink's target is almost always already in the scan.
  3. At minimum, warn when a cache hit arrives via a different path than the one that populated it, so the collision is visible rather than silent.

Happy to test a fix against the reproduction above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions