Skip to content

Fix panic in transformCommonJSExport for named non-default exports#3379

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-lsp-request-failure
Draft

Fix panic in transformCommonJSExport for named non-default exports#3379
Copilot wants to merge 2 commits intomainfrom
copilot/fix-lsp-request-failure

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 9, 2026

Named non-default CommonJS exports (e.g. exports.foo = ...) panic with "Diagnostic emitted without context" during declaration emit when the inferred type references an inaccessible symbol.

transformCommonJSExport has three branches. The default-export and non-identifier-name branches both set getSymbolAccessibilityDiagnostic before calling ensureType, but the named non-default branch did not. Since KindCommonJSExport is not in canProduceDiagnostics, ensureType doesn't override the handler either, leaving the initial throwDiagnostic panic handler active when TrackSymbol encounters a private symbol.

  • Fix: Set getSymbolAccessibilityDiagnostic with Exported_variable_0_has_or_is_using_private_name_1 in the missing branch, matching the pattern of the other two branches
  • Test: Added cjsModuleExportsNamedDeclarationEmit.ts exercising named non-default CJS export declaration emit

The second branch of transformCommonJSExport (named non-default exports)
was missing the getSymbolAccessibilityDiagnostic setter before calling
ensureType. When type serialization triggered TrackSymbol and found an
inaccessible symbol, it would call the default throwDiagnostic handler,
causing a panic. Added the appropriate diagnostic handler to match the
pattern used by the first (default export) and third (non-identifier name)
branches.

Agent-Logs-Url: https://github.com/microsoft/typescript-go/sessions/d88f9257-5e67-4eef-be7b-4574b5576717

Co-authored-by: DanielRosenwasser <972891+DanielRosenwasser@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix LSP request failure for diagnostics during type serialization Fix panic in transformCommonJSExport for named non-default exports Apr 9, 2026
Copilot AI requested a review from DanielRosenwasser April 9, 2026 19:35
Comment on lines +1100 to +1106
tx.state.getSymbolAccessibilityDiagnostic = func(_ printer.SymbolAccessibilityResult) *SymbolAccessibilityDiagnostic {
return &SymbolAccessibilityDiagnostic{
diagnosticMessage: diagnostics.Exported_variable_0_has_or_is_using_private_name_1,
errorNode: input,
typeName: name,
}
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is technically correct (I had the same change locally), but not complete, because while adding the error stops the crash, there shouldn't be an error in the first place, and stopping the error requires further changes related to JS emit and how we choose to reuse nodes / resolve things even though they're in JSDoc. I have that change locally, but it still felt hacky (hence not having sent anything yet).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LSP request failure for diagnostics during type serialization

3 participants