Repository navigation
Fix declaration emit panic for CommonJS named exports - #3379
Daniel Rosenwasser (DanielRosenwasser) with Copilot wants to merge 4 commits into
Conversation
|
Copilot If you undo your fixes, your test doesn't actually crash. |
You're right — confirmed by reverting the fixes locally, the test passes without crashing. I tried several variations (inner class with JSDoc reference, zouying (@typedef) referencing private types, cross-file references) and none of them reproduced the panic in corsa, since the inner I've dropped the speculative |
|
Copilot merge main and try again. try as hard as you can to find a crash. do not guess at a fix. |
Merged main and tried again in e493717. Findings:
The committed fix is narrow, it preserves the export-assignment diagnostic context while serializing the synthetic |
Refocused the PR on #3378 in e791522: removed the unrelated object-form export-assignment change/test and left the named CJS export diagnostic-context fix with its regression test. I also ran |
e791522 to
7bec52f
Compare
| if ast.IsInJSFile(node.AsNode()) { | ||
| oldDiag := tx.state.getSymbolAccessibilityDiagnostic | ||
| tx.state.getSymbolAccessibilityDiagnostic = func(result printer.SymbolAccessibilityResult) *SymbolAccessibilityDiagnostic { | ||
| if result.ErrorNode != nil && canProduceDiagnostics(result.ErrorNode) { | ||
| return createGetSymbolAccessibilityDiagnosticForNode(result.ErrorNode)(result) | ||
| } | ||
| diagnosticMessage := diagnostics.Declaration_emit_for_this_file_requires_using_private_name_0_An_explicit_type_annotation_may_unblock_declaration_emit | ||
| if result.ErrorModuleName != "" { | ||
| diagnosticMessage = diagnostics.Declaration_emit_for_this_file_requires_using_private_name_0_from_module_1_An_explicit_type_annotation_may_unblock_declaration_emit | ||
| } | ||
| errorNode := result.ErrorNode | ||
| if errorNode == nil { | ||
| errorNode = node.AsNode() | ||
| } | ||
| return &SymbolAccessibilityDiagnostic{ | ||
| diagnosticMessage: diagnosticMessage, | ||
| errorNode: errorNode, | ||
| } | ||
| } | ||
| defer func() { | ||
| tx.state.getSymbolAccessibilityDiagnostic = oldDiag | ||
| }() | ||
| } |
There was a problem hiding this comment.
Pull request overview
Fixes a declaration emit panic when symbol accessibility diagnostics are reported for certain CommonJS export patterns in JavaScript/CommonJS modules, by ensuring the declaration transformer always has a valid diagnostic context in JS files.
Changes:
- Install a JS-file-scoped
getSymbolAccessibilityDiagnosticfallback inDeclarationTransformer.visitSourceFileto avoid panics when declaration emit reports private-name accessibility issues without a per-node diagnostic context. - Add a new compiler regression test covering
Object.defineProperty(exports, "...")exporting a value whose inferred type references a private name from another module. - Add reference baselines (
.types,.symbols,.errors.txt) for the new test.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
internal/transformers/declarations/transform.go |
Adds a JS-file-wide symbol accessibility diagnostic fallback to prevent declaration emit panics. |
testdata/tests/cases/compiler/cjsObjectDefinePropertyPrivateModuleDeclarationEmit.ts |
New regression test reproducing the private-name accessibility diagnostic path for a CJS export via Object.defineProperty. |
testdata/baselines/reference/compiler/cjsObjectDefinePropertyPrivateModuleDeclarationEmit.types |
Reference type baseline for the new test. |
testdata/baselines/reference/compiler/cjsObjectDefinePropertyPrivateModuleDeclarationEmit.symbols |
Reference symbol baseline for the new test. |
testdata/baselines/reference/compiler/cjsObjectDefinePropertyPrivateModuleDeclarationEmit.errors.txt |
Reference diagnostics baseline for the new test. |
| if ast.IsInJSFile(node.AsNode()) { | ||
| oldDiag := tx.state.getSymbolAccessibilityDiagnostic | ||
| tx.state.getSymbolAccessibilityDiagnostic = func(result printer.SymbolAccessibilityResult) *SymbolAccessibilityDiagnostic { | ||
| if result.ErrorNode != nil && canProduceDiagnostics(result.ErrorNode) { | ||
| return createGetSymbolAccessibilityDiagnosticForNode(result.ErrorNode)(result) | ||
| } | ||
| diagnosticMessage := diagnostics.Declaration_emit_for_this_file_requires_using_private_name_0_An_explicit_type_annotation_may_unblock_declaration_emit | ||
| if result.ErrorModuleName != "" { | ||
| diagnosticMessage = diagnostics.Declaration_emit_for_this_file_requires_using_private_name_0_from_module_1_An_explicit_type_annotation_may_unblock_declaration_emit |
There was a problem hiding this comment.
I reset this PR from scratch and did my own copiloting, so it's not up to date anymore unfortunately
There was a problem hiding this comment.
There is no way this is the right fix (and the review here is at least observing how it's wrong) - an assignment at the top level? No. It's supposed to be set inline before you invoke a resolver function - it's left as nil by default specifically so unhandled cases crash (ideally in test). (So we don't have a generic "there was an error somewhere" error.) I imagine this assignment should be much simpler and be inside one of the new expando transform functions instead...
There was a problem hiding this comment.
Copilot remove the global set and add the missing set before the resolver calls that need it set. It's intentionally set lazily so context can be provided for the diagnostic - see other resolver calls for context.
There was a problem hiding this comment.
Like, it's funny, because the PR description is basically right - the implementation that was here was just way too broad to match that.
Copilot To be clear, the root cause is since transformExpandoMember and transformExpandoHost no longer go thru transformDeclarationSubtree after recent fixes, the diagnostic handler doesn't get set. The set logic from transformDeclarationSubtree (everything from canProdiceDiagnostic up to the shouldEnterSuppressNewDiagnosticsContextContext block, plus the un-set block at the end) needs to get pulled out into a reusable function and used in those expando functions, too.
There was a problem hiding this comment.
(And transformCommonJSExport likely via transformExpressionStatement)
There was a problem hiding this comment.
Addressed in d67f7a4. I removed the global JS-file diagnostic-context assignment and added the missing lazy context setup directly before the CommonJS named-export resolver call (ensureType(...)), then restored the previous callback after the call.
Co-authored-by: weswigham <[email protected]>
Co-authored-by: weswigham <[email protected]>
CommonJS declaration emit could panic while reporting symbol accessibility diagnostics for inferred named export types that reference inaccessible symbols.
The fix keeps diagnostic context lazy and scoped to the exact resolver call that needs it in the named non-default CJS export path:
transformCommonJSExportnow setsgetSymbolAccessibilityDiagnosticimmediately before the relevantensureType(...)call and restores the previous callback afterward.result.ErrorNodeto preserve node-specific diagnostic context; otherwise it falls back to the JS declaration-emit private-name diagnostic for this path.Tests/baselines:
cjsObjectDefinePropertyPrivateModuleDeclarationEmit.tscovers the affected CommonJS named export path (Object.defineProperty(exports, "api", ...))..errors.txtbaseline to match the corrected diagnostic span.