Repository navigation
Description
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 unresolvedExpected 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
TopFactoryand its constructor reference intop()tonew TopFactory(). Leavenested()byte-for-byte unchanged. The twonestededges then correctly targetapp.ts::nested::Factoryand itsrunmethod. - 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 diffsucceeds 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
Merge pull request #19 from columbia11/claude/issue-fixes
resolve: local JS classes, Rust smart-pointer fields, Python C3, Rust…