Repository navigation
tsgo LSP crashes in WebStorm when opening .mts file in mixed CJS/ESM repo #4180
Description
I hit this in a mixed CJS/ESM codebase after enabling tsgo LSP in WebStorm.
The package has a bunch of normal TS/TSX in a package without "type": "module", plus a few ESM config files converted to .mts.
As soon as I opened the first .mts file, the server crashed:
panic: ScriptKind must be specified when parsing source file: .../config/webpack-dev-server.config.mts
github.com/microsoft/typescript-go/internal/parser.ParseSourceFile(..., 0x0)
github.com/microsoft/typescript-go/internal/project.NewSession.NewParseCache.func3
github.com/microsoft/typescript-go/internal/project.(*compilerHost).GetSourceFile
...
StdIO connection closed
Exit code: 2
Version:
@typescript/native-preview 7.0.0-dev.20260421.2
tsgo --lsp --stdio
I reduced it a bit. Disk .mts handling seems fine; GetScriptKindFromFileName knows about .mts.
The crash happens when the open-file overlay stores an unknown script kind from the LSP didOpen.languageId. In my repro:
textDocument/didOpenwithlanguageId: "typescript"does not crashtextDocument/didOpenwithlanguageId: "mts"crashes
That goes through:
// internal/project/overlayfs.go
lsconv.LanguageKindToScriptKind(events.openChange.LanguageKind)LanguageKindToScriptKind("mts") returns ScriptKindUnknown, then the overlay shadows the disk file, and later parsecache calls ParseSourceFile(..., ScriptKindUnknown).
Seems like the overlay path should probably fall back to the file extension when the client language id is unknown:
scriptKind := lsconv.LanguageKindToScriptKind(events.openChange.LanguageKind)
if scriptKind == core.ScriptKindUnknown {
scriptKind = core.GetScriptKindFromFileName(uri.FileName())
}Full diff
diff --git a/internal/project/overlayfs.go b/internal/project/overlayfs.go
index 8d3fbf8a..1b300a01 100644
--- a/internal/project/overlayfs.go
+++ b/internal/project/overlayfs.go
@@ -310,11 +310,15 @@ func (fs *overlayFS) processChanges(changes []FileChange) (FileChangeSummary, ma
} else {
result.Reopened = uri
}
+ scriptKind := lsconv.LanguageKindToScriptKind(events.openChange.LanguageKind)
+ if scriptKind == core.ScriptKindUnknown {
+ scriptKind = core.GetScriptKindFromFileName(uri.FileName())
+ }
newOverlays[path] = newOverlay(
uri.FileName(),
events.openChange.Content,
events.openChange.Version,
- lsconv.LanguageKindToScriptKind(events.openChange.LanguageKind),
+ scriptKind,
)
continue
}
diff --git a/internal/project/overlayfs_test.go b/internal/project/overlayfs_test.go
index 878fbdb3..4659530f 100644
--- a/internal/project/overlayfs_test.go
+++ b/internal/project/overlayfs_test.go
@@ -3,6 +3,7 @@ package project
import (
"testing"
+ "github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/lsp/lsproto"
"github.com/microsoft/typescript-go/internal/tspath"
"github.com/microsoft/typescript-go/internal/vfs/vfstest"
@@ -162,6 +163,26 @@ func TestProcessChanges(t *testing.T) {
assert.Assert(t, fh.MatchesDiskText())
})
+ t.Run("open falls back to file extension for unknown language kind", func(t *testing.T) {
+ t.Parallel()
+ fs := createOverlayFS()
+ uri := lsproto.DocumentUri("file:///test1.mts")
+
+ fs.processChanges([]FileChange{
+ {
+ Kind: FileChangeKindOpen,
+ URI: uri,
+ Version: 1,
+ Content: "export const x = 1;",
+ LanguageKind: lsproto.LanguageKind("mts"),
+ },
+ })
+
+ fh := fs.getFile(uri.FileName())
+ assert.Assert(t, fh != nil)
+ assert.Equal(t, fh.Kind(), core.ScriptKindTS)
+ })
+
t.Run("watch change on overlay marks as not matching disk", func(t *testing.T) {
t.Parallel()
fs := createOverlayFS()Small repro script (interactions of WebStorm <=2026.1.2 Build #WS-261.24374.125, built on May 13, 2026)
import {spawn} from "node:child_process";
import {readFileSync} from "node:fs";
import {pathToFileURL} from "node:url";
const [,, tsgo, fileName, languageId = "mts"] = process.argv;
const uri = pathToFileURL(fileName).href;
const text = readFileSync(fileName, "utf8");
const child = spawn(tsgo, ["--lsp", "--stdio"], {
cwd: fileName.slice(0, fileName.lastIndexOf("/")),
stdio: ["pipe", "pipe", "pipe"],
});
child.stderr.pipe(process.stderr);
child.stdout.on("data", (chunk) => process.stdout.write(chunk));
child.on("exit", (code, signal) => {
console.error(`\n[tsgo exited code=${code} signal=${signal}]`);
});
let id = 1;
function send(message) {
const payload = JSON.stringify(message);
child.stdin.write(`Content-Length: ${Buffer.byteLength(payload)}\r\n\r\n${payload}`);
}
send({
jsonrpc: "2.0",
id: id++,
method: "initialize",
params: {
processId: null,
rootUri: pathToFileURL(process.cwd()).href,
capabilities: {},
workspaceFolders: [{uri: pathToFileURL(process.cwd()).href, name: "repro"}],
},
});
setTimeout(() => {
send({jsonrpc: "2.0", method: "initialized", params: {}});
send({
jsonrpc: "2.0",
method: "textDocument/didOpen",
params: {
textDocument: {uri, languageId, version: 1, text},
},
});
send({
jsonrpc: "2.0",
id: id++,
method: "textDocument/hover",
params: {textDocument: {uri}, position: {line: 0, character: 0}},
});
}, 100);
setTimeout(() => child.kill(), 5000);
Activity
jakebailey commentedon Jun 3, 2026
mts isn't the right language ID at all; why is WebStorm sending it?