Skip to content
This repository was archived by the owner on Sep 1, 2026. It is now read-only.

Fix panic in declaration emit for named CommonJS exports from .cjs files - #3354

Closed
Jake Bailey (jakebailey) with Copilot wants to merge 6 commits into
mainfrom
copilot/fix-reexporting-micromatch-crash
Closed

Jake Bailey (jakebailey) with Copilot wants to merge 6 commits into
mainfrom
copilot/fix-reexporting-micromatch-crash

Conversation

Copilot AI commented Apr 6, 2026 •

Copy link
Copy Markdown
Contributor

Fix the missing diagnostic context in transformCommonJSExport for named non-default exports. Without this fix, declaration emit panics with "Diagnostic emitted without context" when a .cjs file re-exports a type that triggers a symbol accessibility check.

The test uses export = with a non-exported interface (ExtPkg), matching the original micromatch repro pattern. This triggers the symbol accessibility check that caused the panic. The test produces a TS4023 error due to a separate type serialization bug (tsgo emits ExtPkg instead of typeof import("extpkg")), but crucially no longer panics.

Verified: test panics without the fix and passes with it.

Copilot AI and others added 2 commits April 6, 2026 21:28
Panic: 'Diagnostic emitted without context' in declaration emit
for CommonJS non-default named exports from .cjs files.

Root cause: transformCommonJSExport's else branch (non-default
identifier exports) does not set getSymbolAccessibilityDiagnostic
before calling ensureType. When the type references symbols from
external modules that can't be named, throwDiagnostic panics.

The test reproduces the exact panic by creating a .cjs file that
re-exports a type whose expanded form references symbols from
another @types package.

Co-authored-by: jakebailey <[email protected]>
Copilot AI changed the title [WIP] Fix reexporting @types/[email protected] from a .cjs file crash Fix panic in declaration emit for named CommonJS exports from .cjs files Apr 6, 2026
Comment thread investigation_2522.md Outdated
@@ -0,0 +1,93 @@
# Investigation: Issue #2522 - Panic "Diagnostic emitted without context"

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.

Copilot delete this

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Deleted in 7a353b4.

@@ -0,0 +1,40 @@
/test.cjs(2,1): error TS4023: Exported variable 'extpkg' has or is using name 'ExtPkg' from external module "/node_modules/@types/extpkg/index" but cannot be named.

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.

Copilot The original repro compiled without errors. This one seemingly does not. Is there some other bug that is causing us to be unable to name this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, there's a separate bug. When a module uses export = extpkg where extpkg is typed as a non-exported interface (ExtPkg), tsgo's type serialization emits ExtPkg directly instead of typeof import("extpkg"). tsc correctly uses typeof import("extpkg") for the same scenario, avoiding the accessibility issue.

I've reworked the test in 5db02df to use directly-exported types (via export interface), which tsgo handles correctly. The test now compiles without errors, matching the original micromatch repro behavior. The export = type serialization issue is a separate bug.

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.

Copilot Except, your test does not crash if your changes to the internal dir are reverted! Think harder. You need a test that fails.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right - the simplified test with directly-exported types never triggered the symbol accessibility check, so it would never call getSymbolAccessibilityDiagnostic and never panic.

Reworked the test in 86e36b4 to use export = with a non-exported interface (ExtPkg), matching the original micromatch pattern. Verified it panics without the fix and passes with it. The TS4023 error it produces is from a separate type serialization bug (tsgo emits ExtPkg instead of typeof import("extpkg")).

@jakebailey
Jake Bailey (jakebailey) deleted the copilot/fix-reexporting-micromatch-crash branch June 2, 2026 05:23
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reexporting @types/[email protected] from a .cjs file crashes

3 participants