Skip to content

Dart part of files get absolute-path node IDs, splitting one library into two ID namespaces #3522

Open

Description

@Mkamel-Spec

Summary

In a Dart project that uses part / part of, the symbols declared in a part file are given node IDs derived from the absolute path of the parent library, while every non-part file gets IDs derived from the relative path. The result is that a single Dart library occupies two ID namespaces, and the part file's symbols end up disconnected from the library they belong to.

extract()'s id-canonicalization pass — the one specifically designed to keep on-disk locations out of persisted IDs (referenced as #502 in extractors/base.py) — cannot repair this case. Details below.

Version: graphifyy 0.9.60, Windows, Python 3.12.

Root cause

graphify/extractors/dart.py, in extract_dart():

stem = _file_stem(path)          # non-part file: caller's path, usually relative
file_nid = _make_id(str(path))

part_of_match = re.search(r"^\s*part\s+of\s+['\"]([^'\"]+)['\"]", src_clean, re.MULTILINE)
is_part = False
if part_of_match:
    parent_ref = part_of_match.group(1)
    if parent_ref.endswith(".dart"):
        try:
            parent_path = (path.parent / parent_ref).resolve()   # <-- absolute
            if parent_path.exists():
                stem = _file_stem(parent_path)                   # <-- absolute stem
                file_nid = _make_id(str(parent_path))
                is_part = True

.resolve() makes parent_path absolute, so every symbol in the part file is minted as _make_id(<absolute stem>, <symbol>), whereas the parent library's own symbols are minted from the relative path.

Why the existing canonicalization pass does not fix it

The pass in extract.py that rewrites absolute-derived IDs learns its remap keys from each item's own source_file:

keys = tuple({
    _make_id(str(sf_path)),
    _make_id(str(sf_resolved)),
    _make_id(_file_stem(sf_path)),
    _make_id(_file_stem(sf_resolved)),
})

Two things prevent it from matching here:

  1. For nodes produced from a part file, source_file is the child file (settlement/derive.dart), but the ID was minted from the parent (settlement/settlement.dart). No key derived from the child can ever match an ID derived from the parent.
  2. The block is gated on if sf_path.is_absolute():. When the extractor is called with relative paths, source_file is relative, so the pass skips these items entirely.

Reproduction

lib/core/settlement/settlement.dart      // declares: part 'derive.dart';
lib/core/settlement/derive.dart          // declares: part of 'settlement.dart';

Run extraction from the project root with relative paths:

from pathlib import Path
from graphify.extract import extract
result = extract([Path("lib/core/settlement/settlement.dart"),
                  Path("lib/core/settlement/derive.dart")],
                 cache_root=Path("."))
print({n["id"] for n in result["nodes"]})

Observed: IDs from derive.dart are prefixed with the encoded absolute path (c_users_me_projects_myapp_lib_core_settlement_...), IDs from settlement.dart are not.

Impact

On a real Flutter codebase (~56 code files), three part of files put 59 nodes into a separate namespace:

file nodes stranded
lib/core/settlement/derive.dart 42
lib/core/ledger/history_merge.dart 10
lib/core/settlement/history_merge.dart 7

Node IDs for the settlement module before the fix: {absolute-prefixed: 49, relative: 51} — i.e. the settlement engine was cut roughly in half, with deriveSettlements among the stranded nodes. After the fix: {relative: 100}.

Since the absolute prefix encodes the machine's directory layout, the emitted graph.json is also non-portable between machines and clones.

Suggested fix

Keep the parent in the same path-space as the incoming path, normalizing .. without absolutizing:

import os
...
            try:
                parent_path = Path(os.path.normpath(path.parent / parent_ref))
                if parent_path.exists() or (path.parent / parent_ref).resolve().exists():
                    stem = _file_stem(parent_path)
                    file_nid = _make_id(str(parent_path))
                    is_part = True
            except Exception:
                pass

os.path.normpath collapses any .. in the part of reference without touching the filesystem or making the path absolute, so a part file's IDs are minted in exactly the same form as its parent library's.

A broader alternative would be to have part-file nodes record the parent as source_file (or record the parent separately), which would let the existing canonicalization pass handle them; the change above is the smaller one.

Note on other languages

Anything else that redirects a fragment's IDs to a parent/owner file resolved with .resolve() would show the same split. I only verified Dart.

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