Skip to content

TS/JS: #2552's union merges sibling closures' locals, so a multi-closure const initializer loses the very edges it recovers (v0.9.37) #2568

Closed

Description

@imagineers-tyler

Summary

The fix for #2552 (shipped in v0.9.37) registers every topmost closure in a const
initializer under one const_nid, and unions their local-name sets into the single
local_bound_names[const_nid] entry. Each closure is its own scope, so when an initializer
holds more than one closure, one closure's parameters shadow a sibling closure's body — and a
genuine by-name reference in that sibling is suppressed.

Net effect: the edges #2552 set out to recover are lost again whenever the initializer contains
more than one closure.

This is not a regression against v0.9.36 — before #2552 those bodies were not walked at all,
so no edge existed either way. The single-closure case #2552 targets does work. It is the
multi-closure case that its own union defeats.

Reproduction

Verified against pristine graphifyy==0.9.37 from PyPI.

// src/a.js
function alpha(x){ return x; }
function wrapper(a, b){ return a || b; }

export const handler = wrapper(
  (alpha) => { return alpha; },        // closure 1 — `alpha` is this closure's PARAMETER
  (pool)  => { pool.submit(alpha); }   // closure 2 — `alpha` here is the module function
);
from graphify.extract import extract
from pathlib import Path
r = extract([Path('src/a.js')], cache_root=Path('.c'), parallel=False)
print([e for e in r['edges'] if e['relation'] == 'indirect_call'])
closure 1's parameter name indirect_call for alpha
alpha (collides with the module function) none — edge lost
beta (no collision) emitted, as expected

Only the parameter name of an unrelated sibling closure differs between those two runs, which
isolates the union as the cause. Closure 1's parameter is not in scope inside closure 2.

Cause

graphify/extractors/engine.py, the #2552 block:

closures: list = []
_js_topmost_closures(inner, closures)
for closure in closures:
    if local_bound_names is not None:
        local_bound_names[const_nid] = (
            local_bound_names.get(const_nid, set())
            | _js_local_bound_names(closure, source))
    body = closure.child_by_field_name("body")
    if body:
        function_bodies.append((const_nid, body))

function_bodies gets one entry per closure, but local_bound_names gets one entry
total
. walk_calls(bodyN, const_nid) then looks up that shared entry, so every closure body
sees every sibling's locals.

Bare assignment instead of union would not fix it either — it would just invert the error
(last closure wins, so earlier closures lose their own parameters and fabricate edges rather
than lose them). The key is the problem: several bodies, one entry.

Scope

Rough scan for the shape (const initializer whose value is not itself a closure, containing
more than one topmost closure) over 7 JS projects:

const initializers containing topmost closures : 909
  ...with MORE THAN ONE (union merges them)    : 126
  ...and at least one has named parameters     : 106

e.g. closures=2 params=['p','r'], closures=4 params=['e','l']. The one-letter names are from
bundled output, where collision with an unrelated same-named callable is most likely.

Caveat: 126 is a candidate upper bound, not confirmed damage — my scan approximates
_js_topmost_closures and does not replicate every guard at the call site. The confirmed part
is the minimal repro above.

Note

Reporting rather than patching: the obvious fix is to key the per-function shadow set by the
body node (unique even when the node id is not, and already carried in function_bodies,
much like the existing receiver_types_by_body table) rather than by const_nid. I prototyped
that and it does fix this case — but it also makes every existing inaccuracy in
_js_local_bound_names load-bearing, because today's colliding/unioned entry accidentally masks
them. In my measurements that traded this bug for a different set of wrong edges in both
directions, so I do not think it is ready to propose as a fix here, and I did not want to attach
a patch I could not stand behind.

Activity

  1. safishamsi commented on Aug 9, 2026

    @safishamsi
    Member

    Fixed in v0.9.38 (graphifyy==0.9.38 on PyPI). The 0.9.37 #2552 callback-body fix unioned every sibling closure`s local names under the shared declaration, so a local declared in one callback suppressed a real indirect_call in a sibling callback. Local names are now scoped per callback body (keyed by body id, fed through the existing extra_locals path), so a real cross-callback call is no longer dropped. This can only restore edges, never fabricate; the #2552 single-callback capture is preserved. Thanks for the precise multi-callback repro.

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