Skip to content

bug: JS/TS nested constructors resolve to same-named outer classes #13

Closed

Description

@hongtaihu

new Factory() inside a function resolves to a same-named top-level class even when a function-local class Factory shadows it. Both the constructor edge and the following method call point to the wrong class. This reproduces with the real CLI on unmodified main 2c35e7aeeac215f79d00e9473cc575a40fefd8b2 (engine 9), in both TypeScript and JavaScript.

Minimal reproduction

Save this as app.ts in an otherwise empty directory:

class Factory {
  run() { return "outer"; }
}

export function top() {
  const value = new Factory();
  return value.run();
}

export function nested() {
  class Factory {
    run() { return "inner"; }
  }
  const value = new Factory();
  return value.run();
}
cgr index .
cgr status --json --verify --strict
cgr debug dump
cgr debug unresolved

Expected and actual

The local class lexically shadows the outer class inside nested. Its constructor and receiver therefore refer to nested::Factory:

app.ts::nested -calls-> app.ts::nested::Factory::run
app.ts::nested -instantiates-> app.ts::nested::Factory

Actual dump:

app.ts::nested -calls-> app.ts::Factory::run
app.ts::nested -instantiates-> app.ts::Factory
app.ts::top -calls-> app.ts::Factory::run
app.ts::top -instantiates-> app.ts::Factory

The index reports 4/4 references resolved, strict checksum verification succeeds, and debug unresolved is empty. This is a silently wrong target rather than an unresolved reference.

End-to-end controls

  • Execute the fixture as a JavaScript module in Node.js v24.11.1, then call both exported functions. It returns {"top":"outer","nested":"inner"}. The renamed control returns exactly the same results. This independently confirms which class each function actually uses; the fixtures contain no TypeScript-only syntax.
  • Rename only the top-level class to TopFactory and its constructor reference in top() to new TopFactory(). Leave nested() byte-for-byte unchanged. The two nested edges then correctly target app.ts::nested::Factory and its run method.
  • Save the identical source as app.js: the same wrong targets and successful renamed control reproduce.
  • With 48 unrelated filler files, change the renamed control into the shadowed version and run cgr sync. It uses a delta layer and produces the same two wrong targets as a fresh index. Reverting the rename restores the correct targets. cgr debug diff succeeds in both states for both extensions, so a stale incremental index is not the cause.

The revert reports 1 touched; 0 file(s) re-resolved: restoring the base contents allows reuse of the base facts. The resulting graph is still explicitly checked against a full rebuild.

No factory-return annotation, satisfies, external import, or PR #10 change is needed for this reproduction.

Investigation

In crates/cg-resolve/src/resolve.rs, lookup_type resolves names with scope 0 and no source node. Initializer type inference and the Shape::New path use this lookup. A fix should carry the constructor call/binding's lexical scope and preserve the correct declaration identity instead of selecting a same-named outer class. The scope-loss explanation is a code-reading hypothesis; the wrong CLI edges above are directly reproduced.

Environment: Linux x86_64, Rust 1.99.1, cgr 0.1.0, engine 9, format 2. Built from a clean checkout of the exact main SHA above. Binary SHA-256: 2ac02a0d860bf9f3ef86fb91c1a1e5edac6738e7650d12fbd994c6c369b7dfdb.

Reproduced and investigated by OpenAI Codex on behalf of @hongtaihu.

Activity

  1. added a commit that references this issue on Nov 3, 2026
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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions