Skip to content

Commit fd72fd8

Browse files
authored
Handle windows with no folder in the session lookup (#8130)
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 <file>` from an outside shell failed with "Unexpected status code: 500" until code-server was restarted.
1 parent 15047ab commit fd72fd8

3 files changed

Lines changed: 64 additions & 2 deletions

File tree

‎src/node/vscodeSocket.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import { errorHandler } from "./routes/errors"
88
import { canConnect } from "./util"
99

1010
export interface EditorSessionEntry {
11-
workspace: {
11+
// Not set for a window that has no folder open.
12+
workspace?: {
1213
id: string
1314
folders: {
1415
uri: {
@@ -98,7 +99,7 @@ export class EditorSessionManager {
9899
if (matchCheckResults.has(entry.socketPath)) {
99100
return matchCheckResults.get(entry.socketPath)!
100101
}
101-
const result = entry.workspace.folders.some((folder) => filePath.startsWith(folder.uri.path + path.sep))
102+
const result = entry.workspace?.folders.some((folder) => filePath.startsWith(folder.uri.path + path.sep)) ?? false
102103
matchCheckResults.set(entry.socketPath, result)
103104
return result
104105
}

‎test/unit/node/cli.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -779,6 +779,42 @@ describe("cli", () => {
779779
vscodeSockets.close()
780780
})
781781

782+
it("should use existing if a window with no folder is also open", async () => {
783+
const sessionSocket = path.join(tmpDirPath, "session-socket")
784+
const server = await makeEditorSessionManagerServer(sessionSocket, new EditorSessionManager())
785+
786+
const client = new EditorSessionManagerClient(sessionSocket)
787+
const socketPath = path.join(tmpDirPath, "socket")
788+
await client.addSession({
789+
entry: {
790+
workspace: {
791+
id: "aaa",
792+
folders: [
793+
{
794+
uri: {
795+
path: "/aaa",
796+
},
797+
},
798+
],
799+
},
800+
socketPath,
801+
},
802+
})
803+
// Code sends no workspace for a window with no folder open.
804+
await client.addSession({
805+
entry: {
806+
socketPath: path.join(tmpDirPath, "socket-no-folder"),
807+
},
808+
})
809+
const vscodeSockets = listenOn(socketPath)
810+
811+
const args: UserProvidedArgs = { "reuse-window": true, _: ["/aaa/file"] }
812+
await expect(shouldOpenInExistingInstance(args, sessionSocket)).resolves.toStrictEqual(socketPath)
813+
814+
server.close()
815+
vscodeSockets.close()
816+
})
817+
782818
it("should prefer matching sessions for only the first path", async () => {
783819
const sessionSocket = path.join(tmpDirPath, "session-socket")
784820
const server = await makeEditorSessionManagerServer(sessionSocket, new EditorSessionManager())

‎test/unit/node/vscodeSocket.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,31 @@ describe("EditorSessionManager", () => {
169169
`${tmpDirPath}/vscode-ipc-aaa.sock`,
170170
])
171171
})
172+
it("should not fail on a window with no folder open", async () => {
173+
const manager = new EditorSessionManager()
174+
manager.addSession({
175+
workspace: {
176+
id: "aaa",
177+
folders: [
178+
{
179+
uri: {
180+
path: "/aaa",
181+
},
182+
},
183+
],
184+
},
185+
socketPath: `${tmpDirPath}/vscode-ipc-aaa.sock`,
186+
})
187+
// Code sends no workspace for a window with no folder open.
188+
manager.addSession({
189+
socketPath: `${tmpDirPath}/vscode-ipc-empty.sock`,
190+
})
191+
const entries = manager.getCandidatesForFile("/bbb/some-file:1:1")
192+
expect(entries.map((x) => x.socketPath)).toEqual([
193+
`${tmpDirPath}/vscode-ipc-empty.sock`,
194+
`${tmpDirPath}/vscode-ipc-aaa.sock`,
195+
])
196+
})
172197
})
173198

174199
describe("getConnectedSocketPath", () => {

0 commit comments

Comments
 (0)