Repository navigation
Closed
Labels
Description
🔎 Search Terms
"panic handling request textDocument/hover: runtime error: slice bounds out of range", "panic handling request textDocument/inlayHint: runtime error: slice bounds out of range"
🕗 Version & Regression Information
Checked on the latest nightly (typescript@7.1.0-dev.20261116.1)
⏯ Playground Link
No response
💻 Code
mkdir -p ts-lsp-panic/mapper ts-lsp-panic/src && cd ts-lsp-panic
cat > package.json <<'EOF'
{
"private": true,
"dependencies": {
"throwing-mapper": "file:./mapper",
"typescript": "7.1.0-dev.20261116.1",
"vscode-languageserver-protocol": "3.18.5"
}
}
EOF
cat > tsconfig.json <<'EOF'
{
"include": ["src"],
"contentMappers": [{ "package": "throwing-mapper", "extensions": [".box"] }]
}
EOF
cat > mapper/package.json <<'EOF'
{
"name": "throwing-mapper",
"version": "1.0.0",
"typescript": {
"contentMapper": { "exec": ["node", "server.js"], "dynamicConfig": true }
}
}
EOF
cat > mapper/server.js <<'EOF'
const rpc = require("vscode-jsonrpc/node")
const conn = rpc.createMessageConnection(new rpc.StreamMessageReader(process.stdin), new rpc.StreamMessageWriter(process.stdout))
conn.onRequest("initialize", () => ({ positionEncoding: "utf-16", diagnosticSource: "fake" }))
conn.onRequest("openProject", () => { throw new Error("Cannot find module 'typescript/ast'") })
conn.listen()
EOF
printf 'é\nhover here\n' > src/App.box
cat > repro.mjs <<'EOF'
import { spawn } from "node:child_process"
import { readFileSync, realpathSync } from "node:fs"
import { pathToFileURL } from "node:url"
import { createProtocolConnection, StreamMessageReader, StreamMessageWriter } from "vscode-languageserver-protocol/node"
const root = realpathSync(".")
const uri = pathToFileURL(`${root}/src/App.box`).href
const lsp = spawn("node_modules/.bin/tsc", ["--lsp", "--stdio"], { stdio: ["pipe", "pipe", "inherit"] })
const conn = createProtocolConnection(new StreamMessageReader(lsp.stdout), new StreamMessageWriter(lsp.stdin))
conn.onRequest(() => null)
conn.onNotification("window/logMessage", ({ type, message }) => console.log('LOG:', type, message))
conn.listen()
await conn.sendRequest("initialize", { processId: process.pid, rootUri: pathToFileURL(root).href, capabilities: {}, initializationOptions: { runExternalCode: true, logVerbosity: 1 } })
conn.sendNotification("initialized", {})
conn.sendNotification("textDocument/didOpen", { textDocument: { uri, languageId: "plaintext", version: 1, text: readFileSync("src/App.box", "utf8") } })
try {
console.log("hover:", await conn.sendRequest("textDocument/hover", { textDocument: { uri }, position: { line: 1, character: 2 } }))
} catch (e) {
console.log("hover error:", e.message)
}
lsp.kill()
EOF
npm install
node repro.mjs🙁 Actual behavior
> node repro.mjs
LOG: 3 Resolved client capabilities: {}
LOG: 3 file watching: using builtin in-process watcher (client lacks dynamic watch registration)
LOG: 3 Processed 1 file changes in 11.5µs
LOG: 3 [content mapper: throwing-mapper] send: {"jsonrpc":"2.0","id":"api1","method":"initialize","params":{"positionEncodings":["utf-8","utf-16"]}}
LOG: 3 [content mapper: throwing-mapper] receive: {"jsonrpc":"2.0","id":"api1","result":{"positionEncoding":"utf-16","diagnosticSource":"fake"}}
LOG: 3 [content mapper: throwing-mapper] send: {"jsonrpc":"2.0","id":"api2","method":"openProject","params":{"configFileName":"/private/tmp/aaa/ts-lsp-panic/tsconfig.json","projectHandle":"throwing-mapper@1.0.0:0","compilerOptions":{"configFilePath":"/private/tmp/aaa/ts-lsp-panic/tsconfig.json","runExternalCode":true}}}
LOG: 3 [content mapper: throwing-mapper] receive: {"jsonrpc":"2.0","id":"api2","error":{"code":-32603,"message":"Request openProject failed with message: Cannot find module 'typescript/ast'"}}
LOG: 3 [content mapper: throwing-mapper] send: {"jsonrpc":"2.0","id":"api3","method":"openProject","params":{"configFileName":"/private/tmp/aaa/ts-lsp-panic/tsconfig.json","projectHandle":"throwing-mapper@1.0.0:0","compilerOptions":{"configFilePath":"/private/tmp/aaa/ts-lsp-panic/tsconfig.json","runExternalCode":true}}}
LOG: 3 [content mapper: throwing-mapper] receive: {"jsonrpc":"2.0","id":"api3","error":{"code":-32603,"message":"Request openProject failed with message: Cannot find module 'typescript/ast'"}}
LOG: 3 [content mapper: throwing-mapper] send: {"jsonrpc":"2.0","id":"api4","method":"openProject","params":{"configFileName":"/private/tmp/aaa/ts-lsp-panic/tsconfig.json","projectHandle":"throwing-mapper@1.0.0:0","compilerOptions":{"configFilePath":"/private/tmp/aaa/ts-lsp-panic/tsconfig.json","runExternalCode":true}}}
LOG: 3 [content mapper: throwing-mapper] receive: {"jsonrpc":"2.0","id":"api4","error":{"code":-32603,"message":"Request openProject failed with message: Cannot find module 'typescript/ast'"}}
LOG: 3 Adopted snapshot 1 (parent 0) as current session snapshot (replacing 0)
LOG: 3 Adopted snapshot 2 (parent 1) as current session snapshot (replacing 1)
LOG: 1 panic handling request textDocument/hover: runtime error: slice bounds out of range [3:0]
goroutine 231 [running]:
runtime/debug.Stack()
runtime/debug/stack.go:26 +0x64
github.com/microsoft/TypeScript/tsc/internal/lsp.(*Server).recover(0x261fb9c3e008, 0x261fb9be7200)
github.com/microsoft/TypeScript/tsc/internal/lsp/server.go:1533 +0x3c
panic({0x1024f3368?, 0x261fb9a32ab0?})
runtime/panic.go:859 +0x120
github.com/microsoft/TypeScript/tsc/internal/ls/lsconv.(*Converters).lineAndCharacterToPosition(0x261fba4b2360, {0x1025527f8, 0x261fba26c708}, {0x0?, 0x0?})
github.com/microsoft/TypeScript/tsc/internal/ls/lsconv/converters.go:399 +0x214
github.com/microsoft/TypeScript/tsc/internal/ls/lsconv.(*Converters).lspPositionToVirtual(0x261fba4b2360, {0x1025527f8, 0x261fba26c708}, {0xf68d60?, 0x1?}, 0x1)
github.com/microsoft/TypeScript/tsc/internal/ls/lsconv/converters.go:252 +0xdc
github.com/microsoft/TypeScript/tsc/internal/ls/lsconv.(*Converters).lspPositionToVirtualForScripts[...](0x1021827a0, {0x261fbb105678, 0x1, 0x261fb9c2a8d0}, {0xee1334?, 0x1?}, 0x1?)
github.com/microsoft/TypeScript/tsc/internal/ls/lsconv/converters.go:242 +0xb0
github.com/microsoft/TypeScript/tsc/internal/ls/lsconv.(*Converters).FromLSPPositionForSourceFile(0x261fba4b2360, 0x261fb9c2c180?, {0x25eade0?, 0x1?}, 0x1)
github.com/microsoft/TypeScript/tsc/internal/ls/lsconv/converters.go:220 +0x16c
github.com/microsoft/TypeScript/tsc/internal/ls.(*LanguageService).ProvideHover(0x261fbb188200, {0x102552110, 0x261fbb0efaa0}, 0x261fbb0efbc0)
github.com/microsoft/TypeScript/tsc/internal/ls/hover.go:37 +0xfc
github.com/microsoft/TypeScript/tsc/internal/lsp.(*Server).handleHover(0x261fb99e4768?, {0x102552110?, 0x261fbb0efaa0?}, 0x261fb9d74060?, 0x261fb99e4700?)
github.com/microsoft/TypeScript/tsc/internal/lsp/server.go:2003 +0x2c
github.com/microsoft/TypeScript/tsc/internal/lsp.handlerMap.registerLanguageServiceDocumentRequestHandler[...].1.1()
github.com/microsoft/TypeScript/tsc/internal/lsp/server.go:1407 +0x78
github.com/microsoft/TypeScript/tsc/internal/lsp.(*Server).handleRequestOrNotification.func1()
github.com/microsoft/TypeScript/tsc/internal/lsp/server.go:1225 +0x50
github.com/microsoft/TypeScript/tsc/internal/lsp.(*Server).dispatchLoop.func3()
github.com/microsoft/TypeScript/tsc/internal/lsp/server.go:1072 +0x34
created by github.com/microsoft/TypeScript/tsc/internal/lsp.(*Server).dispatchLoop in goroutine 34
github.com/microsoft/TypeScript/tsc/internal/lsp/server.go:1071 +0x3d8
hover error: InternalError: panic handling request textDocument/hover: runtime error: slice bounds out of range [3:0]
context canceled
🙂 Expected behavior
It shouldn't panic.
Additional information about the issue
Caught this in a project where @vue/content-mapper couldn't resolve typescript/ast import. In this case, it throws an error, so the openProject request never succeeds:
[content mapper: throwing-mapper] send: {"jsonrpc":"2.0","id":"api2","method":"openProject","params":{"configFileName":"/private/tmp/aaa/ts-lsp-panic/tsconfig.json","projectHandle":"throwing-mapper@1.0.0:0","compilerOptions":{"configFilePath":"/private/tmp/aaa/ts-lsp-panic/tsconfig.json","runExternalCode":true}}}
[content mapper: throwing-mapper] receive: {"jsonrpc":"2.0","id":"api2","error":{"code":-32603,"message":"Request openProject failed with message: Cannot find module 'typescript/ast'"}}
It panics on files containing non-ASCII chars because there is a fast path for ASCII-only files:
TypeScript/tsc/internal/ls/lsconv/converters.go
Lines 386 to 388 in 6867202
| if lineMap.AsciiOnly || c.positionEncoding == lsproto.PositionEncodingKindUTF8 { | |
| return max(start, min(start+char, lineEnd)) | |
| } |
Reacted by Copilot
Activity