Skip to content

graphify update --no-cluster picks wrong import target on macOS for fully-qualified imports when two modules share a basename #949

Open

Description

@augustin-emburse

Bug — graphify update --no-cluster resolves fully-qualified Python imports to the wrong target on macOS when two modules share a basename

Observation

On a Python project with two <name>.py modules under different packages and a fully-qualified import of one of them, graphify update . --no-cluster produces different graph.json output on macOS vs Linux at the same git SHA, same graphifyy 0.8.13, same Python 3.12.

Minimal layout (full repro below):

repro/
  pkg_a/__init__.py
  pkg_a/settings.py        # class Settings
  pkg_b/__init__.py
  pkg_b/settings.py        # class Settings
  main.py                  # from pkg_a.settings import Settings

The import from pkg_a.settings import Settings is unambiguous: it refers to pkg_a/settings.py. After v0.8.13's directory-qualified stem fix, both files become distinct nodes (pkg_a_settings_settings and pkg_b_settings_settings) — no node-ID collision.

When the same SHA is rebuilt on a Linux GitHub Actions runner and diffed against a macOS-built graph.json committed by a developer, edges originating in pkg_a/* flip their target between the two same-basename nodes:

 {
   "source": "main_f",
-  "target": "pkg_b_settings_settings",
+  "target": "pkg_a_settings_settings",
   "relation": "uses",
   "source_file": "main.py",
   ...
 },

The + (Linux) target is correct. The − (macOS) target is wrong — pkg_a.settings.Settings and pkg_b.settings.Settings are different symbols.

The pattern repeats for every edge that touches one of these same-basename modules: the resolver lands on whichever <basename>.py came first in the filesystem walk, ignoring the import's package prefix.

Why this is unexpected after v0.8.13

The "Node ID collisions across same-named files in different directories" fix in 0.8.13 makes both files distinct nodes, but the import-target lookup appears to still match candidates by bare <basename> and pick the first hit. The package prefix on the import (pkg_a. vs pkg_b.) is not used to disambiguate.

Hypothesis: filesystem walk order from os.scandir() differs between APFS (macOS) and ext4 (Linux). Either order is fine in isolation, but it shouldn't affect the picked target for a fully-qualified import.

Practical impact

Teams that wire graphify update . --no-cluster into pre-commit + CI and commit graph.json to source control hit a noisy churn whenever a developer regenerates on macOS and CI (or another developer) regenerates on Linux at the next commit. Edges flip target purely from the OS-dependent resolver pick, drowning out the real change in the diff.

A git diff --exit-code gate against the committed graph isn't workable in this state, so we're falling back to graphify's own topology fast-path (No code-graph topology changes detected). That keeps CI green but means we ship a graph.json whose links[].target values are wrong on whichever OS lost the race.

Questions

  1. Is the resolver supposed to use the import's package prefix to disambiguate same-basename modules? If yes, this is a real bug and I'm happy to open a PR.
  2. If the answer to (1) is "--no-cluster was scoped to cross-process determinism, not cross-OS resolver stability," is there a recommended way to pin the resolver's pick (e.g. sorting scandir results, or a hash-stable tiebreak) so committed graph.json files round-trip across CI?
  3. Is there a documented pattern for teams that commit graph.json to source control? (e.g. "build only on Linux runners, devs query but don't regen") Happy to follow it instead of fighting the resolver.

Reproduction

mkdir -p repro/{pkg_a,pkg_b}
touch repro/pkg_a/__init__.py repro/pkg_b/__init__.py
cat > repro/pkg_a/settings.py <<'PY'
class Settings: ...
PY
cat > repro/pkg_b/settings.py <<'PY'
class Settings: ...
PY
cat > repro/main.py <<'PY'
from pkg_a.settings import Settings  # fully-qualified, unambiguous
def f() -> Settings: ...
PY

cd repro
graphify update . --no-cluster
python -c "import json; g=json.load(open('graphify-out/graph.json')); \
  print([(l['source'], l['target']) for l in g['links'] \
         if 'main' in l['source'] and 'settings' in l['target']])"

Expected on every OS: target ends in pkg_a_settings_settings.
Observed on a real codebase: macOS picks pkg_b_settings_settings for some edges; Linux picks pkg_a_settings_settings (correct).

(Disclaimer: I haven't run the 4-file repro above as a standalone repro yet — the diff observation comes from a larger codebase. Happy to bisect further if the minimal case doesn't reproduce out of the gate.)

Environment

  • graphifyy 0.8.13 (latest on PyPI as of 2026-05-20)
  • Python 3.12.x on both sides
  • macOS 14.x, APFS (case-insensitive)
  • Ubuntu 24.04, GitHub Actions runner, ext4

Activity

  1. ayushcodes10 commented on Sep 16, 2026

    @ayushcodes10
    Contributor

    This is already fixed on current v8 (well past 0.8.13). Reproduced the issue's own minimal repro directly:

    pkg_a/settings.py   # class Settings
    pkg_b/settings.py   # class Settings
    main.py             # from pkg_a.settings import Settings
    

    Running extract() on current code:

    imports main.py -> Settings | pkg_a_settings_settings
    calls f() -> Settings | pkg_a_settings_settings
    uses f() -> Settings | pkg_a_settings_settings
    

    All edges correctly resolve to pkg_a_settings_settings, matching the fully-qualified import — no basename-only match, no scan-order sensitivity. Closing this out rather than opening a duplicate PR; let me know if you can still reproduce a discrepancy on current code.

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