Skip to content

Commit 03201c7

Browse files
committed
Avoid spawning code-server with --reuse-window and --new-window
These flags mean the user explicitly wants to open in an existing instance so if the socket is down it should error rather than try to spawn code-server normally.
1 parent 56d10d8 commit 03201c7

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

src/node/cli.ts

+12-7
Original file line numberDiff line numberDiff line change
@@ -749,27 +749,32 @@ export const shouldOpenInExistingInstance = async (args: UserProvidedArgs): Prom
749749
const paths = getResolvedPathsFromArgs(args)
750750
const client = new EditorSessionManagerClient(DEFAULT_SOCKET_PATH)
751751

752-
// If we can't connect to the socket then there's no existing instance.
753-
if (!(await client.canConnect())) {
754-
return undefined
755-
}
756-
757752
// If these flags are set then assume the user is trying to open in an
758-
// existing instance since these flags have no effect otherwise.
753+
// existing instance since these flags have no effect otherwise. That means
754+
// if there is no existing instance we should error rather than falling back
755+
// to spawning code-server normally.
759756
const openInFlagCount = ["reuse-window", "new-window"].reduce((prev, cur) => {
760757
return args[cur as keyof UserProvidedArgs] ? prev + 1 : prev
761758
}, 0)
762759
if (openInFlagCount > 0) {
763760
logger.debug("Found --reuse-window or --new-window")
764-
return await client.getConnectedSocketPath(paths[0])
761+
const socketPath = await client.getConnectedSocketPath(paths[0])
762+
if (!socketPath) {
763+
throw new Error(`No opened code-server instances found to handle ${paths[0]}`)
764+
}
765+
return socketPath
765766
}
766767

767768
// It's possible the user is trying to spawn another instance of code-server.
768769
// 1. Check if any unrelated flags are set (this should only run when
769770
// code-server is invoked exactly like this: `code-server my-file`).
770771
// 2. That a file or directory was passed.
771772
// 3. That the socket is active.
773+
// 4. That an instance exists to handle the path (implied by #3).
772774
if (Object.keys(args).length === 1 && typeof args._ !== "undefined" && args._.length > 0) {
775+
if (!(await client.canConnect())) {
776+
return undefined
777+
}
773778
const socketPath = await client.getConnectedSocketPath(paths[0])
774779
if (socketPath) {
775780
logger.debug("Found existing code-server socket")

test/unit/node/cli.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ describe("cli", () => {
516516

517517
const args: UserProvidedArgs = {}
518518
args["reuse-window"] = true
519-
await expect(shouldOpenInExistingInstance(args)).resolves.toStrictEqual(undefined)
519+
await expect(shouldOpenInExistingInstance(args)).rejects.toThrow()
520520

521521
const socketPath = path.join(tmpDirPath, "socket")
522522
const client = new EditorSessionManagerClient(DEFAULT_SOCKET_PATH)
@@ -551,7 +551,7 @@ describe("cli", () => {
551551

552552
const args: UserProvidedArgs = {}
553553
args["new-window"] = true
554-
await expect(shouldOpenInExistingInstance(args)).resolves.toStrictEqual(undefined)
554+
await expect(shouldOpenInExistingInstance(args)).rejects.toThrow()
555555

556556
const socketPath = path.join(tmpDirPath, "socket")
557557
const client = new EditorSessionManagerClient(DEFAULT_SOCKET_PATH)

0 commit comments

Comments
 (0)