Repository navigation
fix(go): reference the types a package-level var initialiser names (#2360) - #4932
Conversation
…raphify-Labs#2360) Graphify-Labs#2360 walks a package-level `var` initialiser for calls owned by the file, but the types named there were still dropped: `new(T)`, `new(*pkg.T)`, a composite literal `pkg.T{}` / `&T{}`, and the declared `var x pkg.T`. A file holding only a google/wire set, `wire.Bind(new(Iface), new(*Impl))`, then referenced nothing and was reported as yielding no symbols. The file now gets a `references` edge (context `variable_type`) to each such type, collected with the same _go_collect_type_refs and ensure_named_node a struct field uses, so `pkg.T` resolves through the import and an external type is parked on its import path exactly as a field type is. A closure body inside the initialiser is function code and is left alone.
|
Thanks for the pull request, @nothariharan. A maintainer will review it soon. Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions. A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic. |
There was a problem hiding this comment.
Graphify reviewed this change.
Worth a look — the grounded gate found no coupling regressions or blocking issues, but 1 advisory finding(s) below merit a look before merge.
Not checked: tests were not run; formal verification proved 0 of 1 changed function(s) (1 sampled, not proven).
Formal verification. PR-changed functions: 1/1 verified (0 proven, 1 may-equivalent, 0 distinguished) · 0 not verified.
Graphify review — findings
Adds file-level references edges for the types a package-level Go var names, whether in its declared type, a composite literal, or a new(...)/make(...) type argument. A file holding only a wire set like wire.Bind(new(Iface), new(*Impl)) now references both types instead of yielding nothing. External types stay parked on their import path rather than binding to a same-named local type, and the external wire calls mint no call nodes. The walk in emit_go_package_var_type_refs skips func_literal nodes entirely, leaving closure bodies to the existing call walk. That also skips the closure's own signature, so parameter and return types there are never credited to the file.
Worth a look
- Skipping whole func literals drops signature type references —
graphify/extractors/go.py:435· Escalate · medium- NOT verified (no proof, no reproducing execution) — needs human review
Analysis details — impact, health, verification
Impact & health
Graphify review
Impact — 68 functions depend on the 42 functions this change touches.
Health — this change adds coupling hotspots:
- worse:
walk()— 1 callers, 14 callees
Verification — 68 functions in the blast radius were not formally verified this run (proofs are advisory here).
Gate & verification
graphify gate
PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.
Advisory (not blocking):
- verification_scope: 68 function(s) in the blast radius were not formally verified this run
Test selection
Test selection
4 of 488 test file(s) selected (1%) via static blast radius.
tests/test_go_package_var_type_refs.py— impact, changed-testtests/test_go_type_alias.py— impacttests/test_languages.py— impacttests/test_multilang.py— impact
Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.
Formal verification
No difference found (not proven): No behavior difference found in extract\_go (not a proof).
The verifier ran both versions of extract\_go on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.
Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.
Note: An input the sampler did not try could still differ.
· 1 more finding(s) on lines outside this diff (see the check run).
… fixes #4933 (python bare-call), #4931 (go method-value via field) and #4932 (go var-init type refs) were merged on GitHub but each changes AST-cached output and deferred its schema bump; one combined 33->34 bump so cached Python/Go extractions re-run. #4929 (namespace packages) is a post-cache pass and needs none. Opens the unreleased 0.9.101 changelog. Co-Authored-By: Claude Opus 4.8 <[email protected]>
What does this PR do?
Follow-up to #2360 (commit 1c4c3f2), which credits calls in a Go package-level
varinitialiser to the file. The types named in the same place were still dropped, so a file that only holds a google/wire set referenced nothing.Minimal repro (
go.mod:module example.com/app):On v8 (f5155ba),
extract(...)warns1 code file(s) yielded no symbols ... server/wire.go, andwire.gohas only its twoimports_fromedges: nothing toDataSourceRequestValidatororOSSDataSourceRequestValidator. The same shape is Grafana'spkg/server/wire.go.Root cause: in
extract_go,walkhands a package-levelvar_declarationtowalk_calls(#2360), which only looks atcall_expressions. Type references are emitted only for struct fields, interface elements and signatures (emit_go_method_refs). Nothing reads the types in avardeclaration's declared type, a composite literal, or anew/maketype argument.The change: a new
emit_go_package_var_type_refs, called from the samevar_declarationbranch, walks the declaration and collects:var_specdeclared type (var x pkg.T,var _ Iface = ...),composite_literaltype (pkg.T{},&T{},[]*T{{}}),argument_listchild that parses as a type node (new(T),new(*pkg.T),make(map[string]pkg.T)).Each type goes through the same
_go_collect_type_refs+ensure_named_nodepath a struct field uses. The file node gets areferencesedge with contextvariable_type, orgeneric_argfor a generic instantiation's type argument (pkg.List[T]{}), as on a field. That file node is the owner #2360 already gives these calls. Because the path is shared,pkg.Tresolves through the file's import in_resolve_go_type_references. An external type such ashttp.Clientis parked on its import path (net/http.Client), the same as an external field type, and is never bound to a same-named local type.wire.NewSet/wire.Bindare unchanged and add no nodes or edges. With the patch, the repro giveswire.go -references-> DataSourceRequestValidatorandwire.go -references-> OSSDataSourceRequestValidator, both on the nodes declared invalidator/validator.go, and the "no symbols" warning goes away.Type of change
Verification & Invariants
Invariant: a type named in Go source gets a
referencesedge from its owner, and that edge goes to the declaration the import names, or to a parked external stub. It never goes to a same-named local type. Struct fields already worked this way. A package-level initialiser's owner is the file (#2360).Scope limits, stated explicitly:
A closure body inside the initialiser (
var f = func() { _ = &T{} }) is function code. It emits no body type references, like any other function body; a test locks this in.A package-qualified function value passed as an argument (
validator.ProvideValidatorabove) is still not linked. The Extend indirect_call to remaining dispatch patterns (dict tables, assignments, other languages) #1566 method-value path covers only non-package receivers, andtest_a_package_function_value_is_not_read_as_a_method_valuekeeps a package function value from becoming anindirect_call. Changing that would also affect function bodies, so it is left out here.A conversion such as
var _ Iface = (*Impl)(nil)linksIface(the declared type) but notImpl, because the grammar parses(*Impl)as an expression._AST_CACHE_SCHEMAis not bumped. Cached Go extractions keep the old output until the next schema bump. I left that for the release batch, the way the recent schema bumps were done.Read the CONTRIBUTING.md guide.
Reproduced the issue and identified the invariant.
Made the smallest fix necessary.
Added a regression test (if bug fix) or isolated boundary test.
Kept the PR description synchronized with the final implementation.
Documented any limitations / unsupported cases explicitly.
How was this tested?
New file
tests/test_go_package_var_type_refs.py, usingtmp_pathandextract(..., root=tmp_path, parallel=False):new(pkg.Iface)andnew(*pkg.Impl)reach the declarations invalidator.go;pkg.T{}, a barenew(Registry),[]*Entry{{}}and a declaredvar x pkg.Ifacein avar (...)group;new(http.Doer)/new(*http.Client)park asnet/http.*and never bind to the localtype Client; noNewSet/Bindnodes and nocalls/indirect_calledges fromwire.go;varclosure body is not credited to the file.Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (No skill sources touched;--checkpasses.)