Set session socket into environment variable (#6282)
* 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. * Set session socket into environment variable While I was at it I added a CLI flag to override the default. I also swapped the default to --user-data-dir. The value is set on an environment variable so it can be used by the extension host similar to VSCODE_IPC_HOOK_CLI. * Add e2e test for opening files externally
This commit is contained in:
@ -117,40 +117,26 @@ export class CodeServer {
|
||||
* directories.
|
||||
*/
|
||||
private async spawn(): Promise<CodeServerProcess> {
|
||||
// This will be used both as the workspace and data directory to ensure
|
||||
// instances don't bleed into each other.
|
||||
const dir = await this.createWorkspace()
|
||||
|
||||
const args = await this.argsWithDefaults([
|
||||
"--auth",
|
||||
"none",
|
||||
// The workspace to open.
|
||||
...(this.args.includes("--ignore-last-opened") ? [] : [dir]),
|
||||
...this.args,
|
||||
// Using port zero will spawn on a random port.
|
||||
"--bind-addr",
|
||||
"127.0.0.1:0",
|
||||
])
|
||||
return new Promise((resolve, reject) => {
|
||||
const args = [
|
||||
this.entry,
|
||||
"--extensions-dir",
|
||||
path.join(dir, "extensions"),
|
||||
"--auth",
|
||||
"none",
|
||||
// The workspace to open.
|
||||
...(this.args.includes("--ignore-last-opened") ? [] : [dir]),
|
||||
...this.args,
|
||||
// Using port zero will spawn on a random port.
|
||||
"--bind-addr",
|
||||
"127.0.0.1:0",
|
||||
// Setting the XDG variables would be easier and more thorough but the
|
||||
// modules we import ignores those variables for non-Linux operating
|
||||
// systems so use these flags instead.
|
||||
"--config",
|
||||
path.join(dir, "config.yaml"),
|
||||
"--user-data-dir",
|
||||
dir,
|
||||
]
|
||||
this.logger.debug("spawning `node " + args.join(" ") + "`")
|
||||
const proc = cp.spawn("node", args, {
|
||||
cwd: path.join(__dirname, "../../.."),
|
||||
env: {
|
||||
...process.env,
|
||||
...this.env,
|
||||
// Set to empty string to prevent code-server from
|
||||
// using the existing instance when running the e2e tests
|
||||
// from an integrated terminal.
|
||||
// Prevent code-server from using the existing instance when running
|
||||
// the e2e tests from an integrated terminal.
|
||||
VSCODE_IPC_HOOK_CLI: "",
|
||||
PASSWORD,
|
||||
},
|
||||
@ -173,11 +159,15 @@ export class CodeServer {
|
||||
reject(error)
|
||||
})
|
||||
|
||||
// Tracks when the HTTP and session servers are ready.
|
||||
let httpAddress: string | undefined
|
||||
let sessionAddress: string | undefined
|
||||
|
||||
let resolved = false
|
||||
proc.stdout.setEncoding("utf8")
|
||||
onLine(proc, (line) => {
|
||||
// As long as we are actively getting input reset the timer. If we stop
|
||||
// getting input and still have not found the address the timer will
|
||||
// getting input and still have not found the addresses the timer will
|
||||
// reject.
|
||||
timer.reset()
|
||||
|
||||
@ -186,20 +176,69 @@ export class CodeServer {
|
||||
if (resolved) {
|
||||
return
|
||||
}
|
||||
const match = line.trim().match(/HTTPS? server listening on (https?:\/\/[.:\d]+)\/?$/)
|
||||
|
||||
let match = line.trim().match(/HTTPS? server listening on (https?:\/\/[.:\d]+)\/?$/)
|
||||
if (match) {
|
||||
// Cookies don't seem to work on IP address so swap to localhost.
|
||||
// Cookies don't seem to work on IP addresses so swap to localhost.
|
||||
// TODO: Investigate whether this is a bug with code-server.
|
||||
const address = match[1].replace("127.0.0.1", "localhost")
|
||||
this.logger.debug(`spawned on ${address}`)
|
||||
httpAddress = match[1].replace("127.0.0.1", "localhost")
|
||||
}
|
||||
|
||||
match = line.trim().match(/Session server listening on (.+)$/)
|
||||
if (match) {
|
||||
sessionAddress = match[1]
|
||||
}
|
||||
|
||||
if (typeof httpAddress !== "undefined" && typeof sessionAddress !== "undefined") {
|
||||
resolved = true
|
||||
timer.dispose()
|
||||
resolve({ process: proc, address })
|
||||
this.logger.debug(`code-server is ready: ${httpAddress} ${sessionAddress}`)
|
||||
resolve({ process: proc, address: httpAddress })
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a short-lived command.
|
||||
*/
|
||||
async run(args: string[]): Promise<void> {
|
||||
args = await this.argsWithDefaults(args)
|
||||
this.logger.debug("executing `node " + args.join(" ") + "`")
|
||||
await util.promisify(cp.exec)("node " + args.join(" "), {
|
||||
cwd: path.join(__dirname, "../../.."),
|
||||
env: {
|
||||
...process.env,
|
||||
...this.env,
|
||||
// Prevent code-server from using the existing instance when running
|
||||
// the e2e tests from an integrated terminal.
|
||||
VSCODE_IPC_HOOK_CLI: "",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Combine arguments with defaults.
|
||||
*/
|
||||
private async argsWithDefaults(args: string[]): Promise<string[]> {
|
||||
// This will be used both as the workspace and data directory to ensure
|
||||
// instances don't bleed into each other.
|
||||
const dir = await this.workspaceDir
|
||||
return [
|
||||
this.entry,
|
||||
"--extensions-dir",
|
||||
path.join(dir, "extensions"),
|
||||
...args,
|
||||
// Setting the XDG variables would be easier and more thorough but the
|
||||
// modules we import ignores those variables for non-Linux operating
|
||||
// systems so use these flags instead.
|
||||
"--config",
|
||||
path.join(dir, "config.yaml"),
|
||||
"--user-data-dir",
|
||||
dir,
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the code-server process.
|
||||
*/
|
||||
@ -364,6 +403,13 @@ export class CodeServerPage {
|
||||
await this.waitForTab(file)
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a file through an external command.
|
||||
*/
|
||||
async openFileExternally(file: string) {
|
||||
await this.codeServer.run(["--reuse-window", file])
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for a tab to open for the specified file.
|
||||
*/
|
||||
|
@ -35,13 +35,19 @@ describe("Integrated Terminal", ["--disable-workspace-trust"], {}, () => {
|
||||
const tmpFolderPath = await tmpdir(testName)
|
||||
const tmpFile = path.join(tmpFolderPath, "test-file")
|
||||
await fs.writeFile(tmpFile, "test")
|
||||
const fileName = path.basename(tmpFile)
|
||||
|
||||
await codeServerPage.focusTerminal()
|
||||
|
||||
await codeServerPage.page.keyboard.type(`code-server ${tmpFile}`)
|
||||
await codeServerPage.page.keyboard.press("Enter")
|
||||
|
||||
await codeServerPage.waitForTab(fileName)
|
||||
await codeServerPage.waitForTab(path.basename(tmpFile))
|
||||
|
||||
const externalTmpFile = path.join(tmpFolderPath, "test-external-file")
|
||||
await fs.writeFile(externalTmpFile, "foobar")
|
||||
|
||||
await codeServerPage.openFileExternally(externalTmpFile)
|
||||
|
||||
await codeServerPage.waitForTab(path.basename(externalTmpFile))
|
||||
})
|
||||
})
|
||||
|
@ -18,7 +18,6 @@ import {
|
||||
import { shouldSpawnCliProcess } from "../../../src/node/main"
|
||||
import { generatePassword, paths } from "../../../src/node/util"
|
||||
import {
|
||||
DEFAULT_SOCKET_PATH,
|
||||
EditorSessionManager,
|
||||
EditorSessionManagerClient,
|
||||
makeEditorSessionManagerServer,
|
||||
@ -37,6 +36,7 @@ const defaults = {
|
||||
usingEnvHashedPassword: false,
|
||||
"extensions-dir": path.join(paths.data, "extensions"),
|
||||
"user-data-dir": paths.data,
|
||||
"session-socket": path.join(paths.data, "code-server-ipc.sock"),
|
||||
_: [],
|
||||
}
|
||||
|
||||
@ -103,6 +103,8 @@ describe("parser", () => {
|
||||
|
||||
"--disable-getting-started-override",
|
||||
|
||||
["--session-socket", "/tmp/override-code-server-ipc-socket"],
|
||||
|
||||
["--host", "0.0.0.0"],
|
||||
"4",
|
||||
"--",
|
||||
@ -136,6 +138,7 @@ describe("parser", () => {
|
||||
"welcome-text": "welcome to code",
|
||||
version: true,
|
||||
"bind-addr": "192.169.0.1:8080",
|
||||
"session-socket": "/tmp/override-code-server-ipc-socket",
|
||||
})
|
||||
})
|
||||
|
||||
@ -504,22 +507,23 @@ describe("cli", () => {
|
||||
it("should use existing if inside code-server", async () => {
|
||||
process.env.VSCODE_IPC_HOOK_CLI = "test"
|
||||
const args: UserProvidedArgs = {}
|
||||
expect(await shouldOpenInExistingInstance(args)).toStrictEqual("test")
|
||||
expect(await shouldOpenInExistingInstance(args, "")).toStrictEqual("test")
|
||||
|
||||
args.port = 8081
|
||||
args._ = ["./file"]
|
||||
expect(await shouldOpenInExistingInstance(args)).toStrictEqual("test")
|
||||
expect(await shouldOpenInExistingInstance(args, "")).toStrictEqual("test")
|
||||
})
|
||||
|
||||
it("should use existing if --reuse-window is set", async () => {
|
||||
const server = await makeEditorSessionManagerServer(DEFAULT_SOCKET_PATH, new EditorSessionManager())
|
||||
const sessionSocket = path.join(tmpDirPath, "session-socket")
|
||||
const server = await makeEditorSessionManagerServer(sessionSocket, new EditorSessionManager())
|
||||
|
||||
const args: UserProvidedArgs = {}
|
||||
args["reuse-window"] = true
|
||||
await expect(shouldOpenInExistingInstance(args)).resolves.toStrictEqual(undefined)
|
||||
await expect(shouldOpenInExistingInstance(args, sessionSocket)).rejects.toThrow()
|
||||
|
||||
const socketPath = path.join(tmpDirPath, "socket")
|
||||
const client = new EditorSessionManagerClient(DEFAULT_SOCKET_PATH)
|
||||
const client = new EditorSessionManagerClient(sessionSocket)
|
||||
await client.addSession({
|
||||
entry: {
|
||||
workspace: {
|
||||
@ -537,24 +541,25 @@ describe("cli", () => {
|
||||
})
|
||||
const vscodeSockets = listenOn(socketPath)
|
||||
|
||||
await expect(shouldOpenInExistingInstance(args)).resolves.toStrictEqual(socketPath)
|
||||
await expect(shouldOpenInExistingInstance(args, sessionSocket)).resolves.toStrictEqual(socketPath)
|
||||
|
||||
args.port = 8081
|
||||
await expect(shouldOpenInExistingInstance(args)).resolves.toStrictEqual(socketPath)
|
||||
await expect(shouldOpenInExistingInstance(args, sessionSocket)).resolves.toStrictEqual(socketPath)
|
||||
|
||||
server.close()
|
||||
vscodeSockets.close()
|
||||
})
|
||||
|
||||
it("should use existing if --new-window is set", async () => {
|
||||
const server = await makeEditorSessionManagerServer(DEFAULT_SOCKET_PATH, new EditorSessionManager())
|
||||
const sessionSocket = path.join(tmpDirPath, "session-socket")
|
||||
const server = await makeEditorSessionManagerServer(sessionSocket, new EditorSessionManager())
|
||||
|
||||
const args: UserProvidedArgs = {}
|
||||
args["new-window"] = true
|
||||
await expect(shouldOpenInExistingInstance(args)).resolves.toStrictEqual(undefined)
|
||||
await expect(shouldOpenInExistingInstance(args, sessionSocket)).rejects.toThrow()
|
||||
|
||||
const socketPath = path.join(tmpDirPath, "socket")
|
||||
const client = new EditorSessionManagerClient(DEFAULT_SOCKET_PATH)
|
||||
const client = new EditorSessionManagerClient(sessionSocket)
|
||||
await client.addSession({
|
||||
entry: {
|
||||
workspace: {
|
||||
@ -572,25 +577,26 @@ describe("cli", () => {
|
||||
})
|
||||
const vscodeSockets = listenOn(socketPath)
|
||||
|
||||
expect(await shouldOpenInExistingInstance(args)).toStrictEqual(socketPath)
|
||||
expect(await shouldOpenInExistingInstance(args, sessionSocket)).toStrictEqual(socketPath)
|
||||
|
||||
args.port = 8081
|
||||
expect(await shouldOpenInExistingInstance(args)).toStrictEqual(socketPath)
|
||||
expect(await shouldOpenInExistingInstance(args, sessionSocket)).toStrictEqual(socketPath)
|
||||
|
||||
server.close()
|
||||
vscodeSockets.close()
|
||||
})
|
||||
|
||||
it("should use existing if no unrelated flags are set, has positional, and socket is active", async () => {
|
||||
const server = await makeEditorSessionManagerServer(DEFAULT_SOCKET_PATH, new EditorSessionManager())
|
||||
const sessionSocket = path.join(tmpDirPath, "session-socket")
|
||||
const server = await makeEditorSessionManagerServer(sessionSocket, new EditorSessionManager())
|
||||
|
||||
const args: UserProvidedArgs = {}
|
||||
expect(await shouldOpenInExistingInstance(args)).toStrictEqual(undefined)
|
||||
expect(await shouldOpenInExistingInstance(args, sessionSocket)).toStrictEqual(undefined)
|
||||
|
||||
args._ = ["./file"]
|
||||
expect(await shouldOpenInExistingInstance(args)).toStrictEqual(undefined)
|
||||
expect(await shouldOpenInExistingInstance(args, sessionSocket)).toStrictEqual(undefined)
|
||||
|
||||
const client = new EditorSessionManagerClient(DEFAULT_SOCKET_PATH)
|
||||
const client = new EditorSessionManagerClient(sessionSocket)
|
||||
const socketPath = path.join(tmpDirPath, "socket")
|
||||
await client.addSession({
|
||||
entry: {
|
||||
@ -609,18 +615,19 @@ describe("cli", () => {
|
||||
})
|
||||
const vscodeSockets = listenOn(socketPath)
|
||||
|
||||
expect(await shouldOpenInExistingInstance(args)).toStrictEqual(socketPath)
|
||||
expect(await shouldOpenInExistingInstance(args, sessionSocket)).toStrictEqual(socketPath)
|
||||
|
||||
args.port = 8081
|
||||
expect(await shouldOpenInExistingInstance(args)).toStrictEqual(undefined)
|
||||
expect(await shouldOpenInExistingInstance(args, sessionSocket)).toStrictEqual(undefined)
|
||||
|
||||
server.close()
|
||||
vscodeSockets.close()
|
||||
})
|
||||
|
||||
it("should prefer matching sessions for only the first path", async () => {
|
||||
const server = await makeEditorSessionManagerServer(DEFAULT_SOCKET_PATH, new EditorSessionManager())
|
||||
const client = new EditorSessionManagerClient(DEFAULT_SOCKET_PATH)
|
||||
const sessionSocket = path.join(tmpDirPath, "session-socket")
|
||||
const server = await makeEditorSessionManagerServer(sessionSocket, new EditorSessionManager())
|
||||
const client = new EditorSessionManagerClient(sessionSocket)
|
||||
await client.addSession({
|
||||
entry: {
|
||||
workspace: {
|
||||
@ -655,7 +662,7 @@ describe("cli", () => {
|
||||
|
||||
const args: UserProvidedArgs = {}
|
||||
args._ = ["/aaa/file", "/bbb/file"]
|
||||
expect(await shouldOpenInExistingInstance(args)).toStrictEqual(`${tmpDirPath}/vscode-ipc-aaa.sock`)
|
||||
expect(await shouldOpenInExistingInstance(args, sessionSocket)).toStrictEqual(`${tmpDirPath}/vscode-ipc-aaa.sock`)
|
||||
|
||||
server.close()
|
||||
})
|
||||
|
@ -43,6 +43,7 @@ describe("plugin", () => {
|
||||
usingEnvHashedPassword: false,
|
||||
"extensions-dir": "",
|
||||
"user-data-dir": "",
|
||||
"session-socket": "",
|
||||
}
|
||||
next()
|
||||
}
|
||||
|
@ -1,19 +1,8 @@
|
||||
import { logger } from "@coder/logger"
|
||||
import * as app from "../../../src/node/app"
|
||||
import { paths } from "../../../src/node/util"
|
||||
import {
|
||||
DEFAULT_SOCKET_PATH,
|
||||
EditorSessionManager,
|
||||
makeEditorSessionManagerServer,
|
||||
} from "../../../src/node/vscodeSocket"
|
||||
import { EditorSessionManager, makeEditorSessionManagerServer } from "../../../src/node/vscodeSocket"
|
||||
import { clean, tmpdir, listenOn, mockLogger } from "../../utils/helpers"
|
||||
|
||||
describe("DEFAULT_SOCKET_PATH", () => {
|
||||
it("should be a unique path per user", () => {
|
||||
expect(DEFAULT_SOCKET_PATH.startsWith(paths.data)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("makeEditorSessionManagerServer", () => {
|
||||
let tmpDirPath: string
|
||||
|
||||
|
Reference in New Issue
Block a user