From f43f9ff2f7d2b8998b42b33e3cf897bf44a54ac7 Mon Sep 17 00:00:00 2001 From: b84330808 Date: Sun, 15 Nov 2026 04:44:58 +0900 Subject: [PATCH] Handle windows with no folder in the session lookup Code registers a window that has no folder open without a workspace, so once one is registered alongside another window, every session lookup threw and `code-server ` from an outside shell failed with "Unexpected status code: 500" until code-server was restarted. Fixes #6977 --- src/node/vscodeSocket.ts | 5 ++-- test/unit/node/cli.test.ts | 36 +++++++++++++++++++++++++++++ test/unit/node/vscodeSocket.test.ts | 25 ++++++++++++++++++++ 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/src/node/vscodeSocket.ts b/src/node/vscodeSocket.ts index 70603e50b392..0dec6b8e9399 100644 --- a/src/node/vscodeSocket.ts +++ b/src/node/vscodeSocket.ts @@ -8,7 +8,8 @@ import { errorHandler } from "./routes/errors" import { canConnect } from "./util" export interface EditorSessionEntry { - workspace: { + // Not set for a window that has no folder open. + workspace?: { id: string folders: { uri: { @@ -98,7 +99,7 @@ export class EditorSessionManager { if (matchCheckResults.has(entry.socketPath)) { return matchCheckResults.get(entry.socketPath)! } - const result = entry.workspace.folders.some((folder) => filePath.startsWith(folder.uri.path + path.sep)) + const result = entry.workspace?.folders.some((folder) => filePath.startsWith(folder.uri.path + path.sep)) ?? false matchCheckResults.set(entry.socketPath, result) return result } diff --git a/test/unit/node/cli.test.ts b/test/unit/node/cli.test.ts index ebd0ab58a61a..cbbb66a6daa3 100644 --- a/test/unit/node/cli.test.ts +++ b/test/unit/node/cli.test.ts @@ -779,6 +779,42 @@ describe("cli", () => { vscodeSockets.close() }) + it("should use existing if a window with no folder is also open", async () => { + const sessionSocket = path.join(tmpDirPath, "session-socket") + const server = await makeEditorSessionManagerServer(sessionSocket, new EditorSessionManager()) + + const client = new EditorSessionManagerClient(sessionSocket) + const socketPath = path.join(tmpDirPath, "socket") + await client.addSession({ + entry: { + workspace: { + id: "aaa", + folders: [ + { + uri: { + path: "/aaa", + }, + }, + ], + }, + socketPath, + }, + }) + // Code sends no workspace for a window with no folder open. + await client.addSession({ + entry: { + socketPath: path.join(tmpDirPath, "socket-no-folder"), + }, + }) + const vscodeSockets = listenOn(socketPath) + + const args: UserProvidedArgs = { "reuse-window": true, _: ["/aaa/file"] } + await expect(shouldOpenInExistingInstance(args, sessionSocket)).resolves.toStrictEqual(socketPath) + + server.close() + vscodeSockets.close() + }) + it("should prefer matching sessions for only the first path", async () => { const sessionSocket = path.join(tmpDirPath, "session-socket") const server = await makeEditorSessionManagerServer(sessionSocket, new EditorSessionManager()) diff --git a/test/unit/node/vscodeSocket.test.ts b/test/unit/node/vscodeSocket.test.ts index 75357c76bf08..8111e09dbcb7 100644 --- a/test/unit/node/vscodeSocket.test.ts +++ b/test/unit/node/vscodeSocket.test.ts @@ -169,6 +169,31 @@ describe("EditorSessionManager", () => { `${tmpDirPath}/vscode-ipc-aaa.sock`, ]) }) + it("should not fail on a window with no folder open", async () => { + const manager = new EditorSessionManager() + manager.addSession({ + workspace: { + id: "aaa", + folders: [ + { + uri: { + path: "/aaa", + }, + }, + ], + }, + socketPath: `${tmpDirPath}/vscode-ipc-aaa.sock`, + }) + // Code sends no workspace for a window with no folder open. + manager.addSession({ + socketPath: `${tmpDirPath}/vscode-ipc-empty.sock`, + }) + const entries = manager.getCandidatesForFile("/bbb/some-file:1:1") + expect(entries.map((x) => x.socketPath)).toEqual([ + `${tmpDirPath}/vscode-ipc-empty.sock`, + `${tmpDirPath}/vscode-ipc-aaa.sock`, + ]) + }) }) describe("getConnectedSocketPath", () => {