Skip to content

Commit 026caf4

Browse files
committed
Add separate function for VS Code arguments
The problem before was that the pop() caused the open in existing instance functionality to break because the arguments no longer contained the file. We could simply remove the pop() but since `workspace` and `folder` are not CLI arguments I think it makes sense to handle them in a separate function which can be called at the point where they are needed. This also lets us de-duplicate some logic since we create these arguments in two spots and lets us skip this logic when we do not need it. The pop() is still avoided because manipulating a passed-in object in-place seems like a risky move. If we really need to do this we should copy the positional argument array instead.
1 parent 4b4ec37 commit 026caf4

File tree

3 files changed

+39
-38
lines changed

3 files changed

+39
-38
lines changed

src/node/cli.ts

+35-20
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,10 @@ export const parse = (
405405
throw new Error("--cert-key is missing")
406406
}
407407

408-
logger.debug(() => ["parsed command line", field("args", { ...args, password: undefined })])
408+
logger.debug(() => [
409+
`parsed ${opts?.configFile ? "config" : "command line"}`,
410+
field("args", { ...args, password: undefined }),
411+
])
409412

410413
return args
411414
}
@@ -430,8 +433,6 @@ export interface DefaultedArgs extends ConfigArgs {
430433
"user-data-dir": string
431434
/* Positional arguments. */
432435
_: []
433-
folder: string
434-
workspace: string
435436
}
436437

437438
/**
@@ -539,25 +540,8 @@ export async function setDefaults(cliArgs: UserProvidedArgs, configArgs?: Config
539540
args._ = []
540541
}
541542

542-
let workspace = ""
543-
let folder = ""
544-
if (args._.length) {
545-
const lastEntry = path.resolve(process.cwd(), args._[args._.length - 1])
546-
const entryIsFile = await isFile(lastEntry)
547-
548-
if (entryIsFile && path.extname(lastEntry) === ".code-workspace") {
549-
workspace = lastEntry
550-
args._.pop()
551-
} else if (!entryIsFile) {
552-
folder = lastEntry
553-
args._.pop()
554-
}
555-
}
556-
557543
return {
558544
...args,
559-
workspace,
560-
folder,
561545
usingEnvPassword,
562546
usingEnvHashedPassword,
563547
} as DefaultedArgs // TODO: Technically no guarantee this is fulfilled.
@@ -760,3 +744,34 @@ export const shouldOpenInExistingInstance = async (args: UserProvidedArgs): Prom
760744

761745
return undefined
762746
}
747+
748+
/**
749+
* Convert our arguments to VS Code server arguments.
750+
*/
751+
export const toVsCodeArgs = async (args: DefaultedArgs): Promise<CodeServerLib.ServerParsedArgs> => {
752+
let workspace = ""
753+
let folder = ""
754+
if (args._.length) {
755+
const lastEntry = path.resolve(args._[args._.length - 1])
756+
const entryIsFile = await isFile(lastEntry)
757+
if (entryIsFile && path.extname(lastEntry) === ".code-workspace") {
758+
workspace = lastEntry
759+
} else if (!entryIsFile) {
760+
folder = lastEntry
761+
}
762+
// Otherwise it is a regular file. Spawning VS Code with a file is not yet
763+
// supported but it can be done separately after code-server spawns.
764+
}
765+
766+
return {
767+
"connection-token": "0000",
768+
...args,
769+
workspace,
770+
folder,
771+
"accept-server-license-terms": true,
772+
/** Type casting. */
773+
help: !!args.help,
774+
version: !!args.version,
775+
port: args.port?.toString(),
776+
}
777+
}

src/node/main.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import path from "path"
55
import { Disposable } from "../common/emitter"
66
import { plural } from "../common/util"
77
import { createApp, ensureAddress } from "./app"
8-
import { AuthType, DefaultedArgs, Feature, UserProvidedArgs } from "./cli"
8+
import { AuthType, DefaultedArgs, Feature, toVsCodeArgs, UserProvidedArgs } from "./cli"
99
import { coderCloudBind } from "./coder_cloud"
1010
import { commit, version } from "./constants"
1111
import { register } from "./routes"
@@ -35,14 +35,7 @@ export const runVsCodeCli = async (args: DefaultedArgs): Promise<void> => {
3535
const spawnCli = await loadAMDModule<CodeServerLib.SpawnCli>("vs/server/remoteExtensionHostAgent", "spawnCli")
3636

3737
try {
38-
await spawnCli({
39-
...args,
40-
/** Type casting. */
41-
"accept-server-license-terms": true,
42-
help: !!args.help,
43-
version: !!args.version,
44-
port: args.port?.toString(),
45-
})
38+
await spawnCli(await toVsCodeArgs(args))
4639
} catch (error: any) {
4740
logger.error("Got error from VS Code", error)
4841
}

src/node/routes/vscode.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as express from "express"
33
import { WebsocketRequest } from "../../../typings/pluginapi"
44
import { logError } from "../../common/util"
55
import { isDevMode } from "../constants"
6+
import { toVsCodeArgs } from "../cli"
67
import { ensureAuthenticated, authenticated, redirect } from "../http"
78
import { loadAMDModule, readCompilationStats } from "../util"
89
import { Router as WsRouter } from "../wsRouter"
@@ -87,15 +88,7 @@ export class CodeServerRouteWrapper {
8788
)
8889

8990
try {
90-
this._codeServerMain = await createVSServer(null, {
91-
"connection-token": "0000",
92-
"accept-server-license-terms": true,
93-
...args,
94-
/** Type casting. */
95-
help: !!args.help,
96-
version: !!args.version,
97-
port: args.port?.toString(),
98-
})
91+
this._codeServerMain = await createVSServer(null, await toVsCodeArgs(args))
9992
} catch (createServerError) {
10093
logError(logger, "CodeServerRouteWrapper", createServerError)
10194
return next(createServerError)

0 commit comments

Comments
 (0)