Skip to content

Go: a package's type is fragmented into one node per file that declares a method on it #3399

Closed

Description

@xiongjianxu

Net-new over v8 (verified @6478eb71, 0.9.80)

v8 has no Go decl/def fold: graphify/extractors/resolution.py has _merge_decl_def_classes
(C/C++/ObjC) but no _merge_go_package_types, and nothing collapses the per-file Go type
fragmentation before _disambiguate_colliding_node_ids salts the colliding ids by path.
The bug reproduces exactly as described below.

Still missing, and what the paired PR adds: _merge_go_package_types, invoked right after
_merge_decl_def_classes and before disambiguation, folding same-id bare-label .go type
nodes in one directory into a single node (declaration site wins; cross-directory
same-named packages and _test.go members excluded). Fully net-new.


Summary

A Go type whose methods live in other files of the same package becomes several nodes, one
per file, each owning only the methods declared there. The declaring file's node owns none of
them. Every downstream single-definition guard then reads the set as an ambiguity and bails, so
an idiomatic multi-file package loses type-level resolution.

C/C++/ObjC hit the same id-collision between a header declaration and its impl and avoid this
through _merge_decl_def_classes (graphify/extractors/resolution.py), which folds the
collision before _disambiguate_colliding_node_ids splits it. Go has no equivalent.

Reproducer

svc/a.go   package svc; type Server struct{}; func Run(srv *Server) { srv.Close() }
svc/b.go   package svc; func (s *Server) Close() {}
svc/c.go   package svc; func (s *Server) Save() {}
$ graphify extract svc/

Actual — three Server nodes, and the methods hang off two of them:

nodes labelled Server:
    ..._svc_a_go_svc_server | a.go
    ..._svc_b_go_svc_server | b.go
    ..._svc_c_go_svc_server | c.go
edges:
    a.go   -> Server    | contains
    Run()  -> Server    | references (parameter_type)
    Server -> .Close()  | method      <- the b.go node
    Server -> .Save()   | method      <- the c.go node

Expected — one Server, sourced at its declaration, owning both methods:

    svc_server | a.go
    a.go   -> Server   | contains
    Run()  -> Server   | references (parameter_type)
    Server -> .Close() | method
    Server -> .Save()  | method

Why

extract_go mints the receiver's type node in every file that declares a method on it
(graphify/extractors/go.py, the method_declaration branch), keyed
_make_id(pkg_scope, receiver_type) where pkg_scope is the package directory's name. All those
nodes therefore share one id and differ only in source_file — which is exactly the input
_disambiguate_colliding_node_ids is built to split, so it salts each one with its own path.

Impact

  • references to the type land on whichever fragment the referring file produced, so a type's
    incoming uses and its methods are no longer reachable from one node.
  • The id is salted with a file path, so it is neither the canonical {pkg}_{type} form nor
    stable across machines for the affected types.
  • Every resolver that requires exactly one declaration of a name (member-call resolution, the
    Go type-reference rewire) bails, which is silent under-reporting rather than a visible error.

Splitting one Go type into two files is not an edge case; it is how any package larger than a
single file is written.

Fix

Fold the id-collision before disambiguation, mirroring _merge_decl_def_classes. The nodes
already share an id, so no edge re-pointing is needed — only the redundant duplicates are
dropped and the declaring file's node survives.

Guards against a false merge: all members of the collision must be .go files in one
directory
(the id folds in only the directory's name, so a/svc and b/svc collide while
being different packages), and a _test.go member stops the fold, since its package clause may
be the separate external svc_test package declaring a type of the same name — extract_go
does not parse the package clause, so same-directory is not evidence of same package there.

PR follows.

Activity

  1. xiongjianxu commented on Sep 24, 2026

    @xiongjianxu
    ContributorAuthor

    Self-contained, runnable reproduction (driven only by graphify on PYTHONPATH, so the same script runs against pristine v8 and against the PR branch). One repo, one package svc: the type Server is declared in a.go while its methods live in b.go / c.go.

    # repro_3399.py  —  python repro_3399.py   (exit 0 = fixed, 1 = fragmented)
    import sys, tempfile
    from pathlib import Path
    from graphify.extract import extract
    
    FILES = {
        "svc/a.go": ("package svc\n\ntype Server struct{}\n\n"
                     "func Run(srv *Server) { srv.Close() }\n"),
        "svc/b.go": "package svc\n\nfunc (s *Server) Close() {}\n",
        "svc/c.go": "package svc\n\nfunc (s *Server) Save() {}\n",
    }
    
    def main():
        with tempfile.TemporaryDirectory() as d:
            tmp = Path(d)
            for name, body in FILES.items():
                p = tmp / name
                p.parent.mkdir(parents=True, exist_ok=True)
                p.write_text(body)
            result = extract([tmp / n for n in FILES],
                             cache_root=tmp / "graphify-out", parallel=False)
            label = {n["id"]: n["label"] for n in result["nodes"]}
            servers = [n for n in result["nodes"] if n["label"] == "Server"]
            print("Server nodes:", len(servers))
            for n in servers:
                methods = {label.get(e["target"], e["target"]) for e in result["edges"]
                           if e["relation"] == "method" and e["source"] == n["id"]}
                print(f"  {Path(str(n['source_file'])).name}: methods={sorted(methods) or '(none)'}")
            return 0 if len(servers) == 1 else 1
    
    sys.exit(main())

    On pristine v8 (4c73561):

    Server nodes: 3
      a.go: methods=(none)
      b.go: methods=['.Close()']
      c.go: methods=['.Save()']          # exit 1
    

    extract_go keys the type on its package directory, so every file declaring a method mints Server again under the same id; disambiguation then splits them by path. The type fragments into three partial nodes, and the node that actually declares Server (a.go) owns none of its methods — so every single-definition guard downstream reads this as an ambiguity.

    With PR #3400:

    Server nodes: 1
      a.go: methods=['.Close()', '.Save()']   # exit 0
    

    _merge_go_package_types folds the fragments before disambiguation: one Server node (sourced at the declaring a.go) owns both methods, and its id returns to the un-salted canonical form. Because the fold keeps the same id, existing edges need no re-pointing.

    PR #3400 cherry-picks clean onto the current v8 tip 4c73561 (git merge-tree reports no conflicts; PR is MERGEABLE / CLEAN).

  2. safishamsi commented on Oct 13, 2026

    @safishamsi
    Member

    Fixed in v0.9.89 (graphifyy 0.9.89 on PyPI) via #3400.

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