Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8244463

Browse files
committedNov 8, 2021
Mostly restore command-line parity
Restore most everything and remove the added server arguments. This will let us add and remove options after later so we can contain the number of breaking changes. To accomplish this a hard separation is added between the CLI arguments and the server arguments. The separation between user-provided arguments and arguments with defaults is also made more clear. The extra directory flags have been left out as they were buggy and should be implemented upstream although I think there are better solutions anyway. locale and install-source are unsupported with the web remote and are left removed. It is unclear whether they were used before anyway. Some restored flags still need to have their behavior re-implemented.
1 parent d03c9f5 commit 8244463

File tree

9 files changed

+216
-259
lines changed

9 files changed

+216
-259
lines changed
 

‎src/node/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ const listen = (server: http.Server, { host, port, socket }: ListenOptions) => {
4444
server.listen(socket, onListen)
4545
} else {
4646
// [] is the correct format when using :: but Node errors with them.
47-
server.listen(parseInt(port, 10), host.replace(/^\[|\]$/g, ""), onListen)
47+
server.listen(port, host.replace(/^\[|\]$/g, ""), onListen)
4848
}
4949
})
5050
}

‎src/node/cli.ts

Lines changed: 105 additions & 97 deletions
Large diffs are not rendered by default.

‎src/node/entry.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ async function entry(): Promise<void> {
4141
return
4242
}
4343

44-
const cliArgs = await parse(process.argv.slice(2))
44+
const cliArgs = parse(process.argv.slice(2))
4545
const configArgs = await readConfigFile(cliArgs.config)
4646
const args = await setDefaults(cliArgs, configArgs)
4747

@@ -74,12 +74,14 @@ async function entry(): Promise<void> {
7474
return
7575
}
7676

77-
if (await shouldSpawnCliProcess(args)) {
77+
if (shouldSpawnCliProcess(args)) {
78+
logger.debug("Found VS Code arguments; spawning VS Code CLI")
7879
return runVsCodeCli(args)
7980
}
8081

8182
const socketPath = await shouldOpenInExistingInstance(cliArgs)
8283
if (socketPath) {
84+
logger.debug("Trying to open in existing instance")
8385
return openInExistingInstance(args, socketPath)
8486
}
8587

‎src/node/main.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@ import path from "path"
44
import { Disposable } from "../common/emitter"
55
import { plural } from "../common/util"
66
import { createApp, ensureAddress } from "./app"
7-
import { AuthType, DefaultedArgs, Feature } from "./cli"
7+
import { AuthType, DefaultedArgs, Feature, UserProvidedArgs } from "./cli"
88
import { coderCloudBind } from "./coder_cloud"
99
import { commit, version } from "./constants"
1010
import { register } from "./routes"
1111
import { humanPath, isFile, loadAMDModule, open } from "./util"
1212

13-
export const shouldSpawnCliProcess = async (args: CodeServerLib.ServerParsedArgs): Promise<boolean> => {
13+
/**
14+
* Return true if the user passed an extension-related VS Code flag.
15+
*/
16+
export const shouldSpawnCliProcess = (args: UserProvidedArgs): boolean => {
1417
return (
15-
!args["start-server"] &&
16-
(!!args["list-extensions"] ||
17-
!!args["install-extension"] ||
18-
!!args["install-builtin-extension"] ||
19-
!!args["uninstall-extension"] ||
20-
!!args["locate-extension"])
18+
!!args["list-extensions"] ||
19+
!!args["install-extension"] ||
20+
!!args["uninstall-extension"] ||
21+
!!args["locate-extension"]
2122
)
2223
}
2324

@@ -33,7 +34,11 @@ export const runVsCodeCli = async (args: DefaultedArgs): Promise<void> => {
3334
const spawnCli = await loadAMDModule<CodeServerLib.SpawnCli>("vs/server/remoteExtensionHostAgent", "spawnCli")
3435

3536
try {
36-
await spawnCli(args)
37+
await spawnCli({
38+
...args,
39+
// For some reason VS Code takes the port as a string.
40+
port: typeof args.port !== "undefined" ? args.port.toString() : undefined,
41+
})
3742
} catch (error: any) {
3843
logger.error("Got error from VS Code", error)
3944
}
@@ -49,8 +54,9 @@ export const openInExistingInstance = async (args: DefaultedArgs, socketPath: st
4954
forceReuseWindow: args["reuse-window"],
5055
forceNewWindow: args["new-window"],
5156
}
52-
for (let i = 0; i < args._.length; i++) {
53-
const fp = path.resolve(args._[i])
57+
const paths = args._ || []
58+
for (let i = 0; i < paths.length; i++) {
59+
const fp = path.resolve(paths[i])
5460
if (await isFile(fp)) {
5561
pipeArgs.fileURIs.push(fp)
5662
} else {

‎src/node/routes/vscode.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ export const createVSServerRouter = async (args: DefaultedArgs): Promise<VSServe
1919
)
2020

2121
const codeServerMain = await createVSServer(null, {
22-
...args,
23-
protocol: args["cert"] || args.link ? "https:" : "http:",
2422
connectionToken: "0000",
23+
...args,
24+
// For some reason VS Code takes the port as a string.
25+
port: typeof args.port !== "undefined" ? args.port.toString() : undefined,
2526
})
2627

2728
const router = express.Router()

‎test/unit/node/app.test.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import * as http from "http"
44
import * as https from "https"
55
import * as path from "path"
66
import { createApp, ensureAddress, handleArgsSocketCatchError, handleServerError } from "../../../src/node/app"
7-
import { createDefaultArgs, OptionalString, setDefaults } from "../../../src/node/cli"
7+
import { OptionalString, setDefaults } from "../../../src/node/cli"
88
import { generateCertificate } from "../../../src/node/util"
99
import { getAvailablePort, tmpdir } from "../../utils/helpers"
1010

1111
describe("createApp", () => {
1212
let spy: jest.SpyInstance
1313
let unlinkSpy: jest.SpyInstance
14-
let port: string
14+
let port: number
1515
let tmpDirPath: string
1616
let tmpFilePath: string
1717

@@ -29,7 +29,7 @@ describe("createApp", () => {
2929
// then you can spy on those modules methods, like unlink.
3030
// See: https://github.com/aelbore/esbuild-jest/issues/26#issuecomment-893763840
3131
unlinkSpy = jest.spyOn(promises, "unlink")
32-
port = (await getAvailablePort()).toString()
32+
port = await getAvailablePort()
3333
})
3434

3535
afterEach(() => {
@@ -44,7 +44,6 @@ describe("createApp", () => {
4444

4545
it("should return an Express app, a WebSockets Express app and an http server", async () => {
4646
const defaultArgs = await setDefaults({
47-
...createDefaultArgs(),
4847
port,
4948
})
5049
const app = await createApp(defaultArgs)
@@ -61,7 +60,6 @@ describe("createApp", () => {
6160

6261
it("should handle error events on the server", async () => {
6362
const defaultArgs = await setDefaults({
64-
...createDefaultArgs(),
6563
port,
6664
})
6765

@@ -82,9 +80,8 @@ describe("createApp", () => {
8280
it("should reject errors that happen before the server can listen", async () => {
8381
// We listen on an invalid port
8482
// causing the app to reject the Promise called at startup
85-
const port = "2"
83+
const port = 2
8684
const defaultArgs = await setDefaults({
87-
...createDefaultArgs(),
8885
port,
8986
})
9087

@@ -105,7 +102,6 @@ describe("createApp", () => {
105102
it("should unlink a socket before listening on the socket", async () => {
106103
await promises.writeFile(tmpFilePath, "")
107104
const defaultArgs = await setDefaults({
108-
...createDefaultArgs(),
109105
socket: tmpFilePath,
110106
})
111107

@@ -119,7 +115,6 @@ describe("createApp", () => {
119115
const testCertificate = await generateCertificate("localhost")
120116
const cert = new OptionalString(testCertificate.cert)
121117
const defaultArgs = await setDefaults({
122-
...createDefaultArgs(),
123118
port,
124119
cert,
125120
["cert-key"]: testCertificate.certKey,

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

Lines changed: 79 additions & 134 deletions
Large diffs are not rendered by default.

‎test/unit/node/plugin.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe("plugin", () => {
3434
_: [],
3535
auth: AuthType.None,
3636
host: "localhost",
37-
port: "8080",
37+
port: 8080,
3838
"proxy-domain": [],
3939
config: "~/.config/code-server/config.yaml",
4040
verbose: false,

‎test/utils/integration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import * as httpserver from "./httpserver"
55
export async function setup(argv: string[], configFile?: string): Promise<httpserver.HttpServer> {
66
argv = ["--bind-addr=localhost:0", "--log=warn", ...argv]
77

8-
const cliArgs = await parse(argv)
9-
const configArgs = await parseConfigFile(configFile || "", "test/integration.ts")
8+
const cliArgs = parse(argv)
9+
const configArgs = parseConfigFile(configFile || "", "test/integration.ts")
1010
const args = await setDefaults(cliArgs, configArgs)
1111

1212
const server = await runCodeServer(args)

0 commit comments

Comments
 (0)
Please sign in to comment.