diff --git a/src/node/cli.ts b/src/node/cli.ts index 088431b7aa02..63076ee49db5 100644 --- a/src/node/cli.ts +++ b/src/node/cli.ts @@ -4,7 +4,9 @@ import yaml from "js-yaml" import * as os from "os" import * as path from "path" import { Args as VsArgs } from "../../typings/ipc" -import { canConnect, generateCertificate, generatePassword, humanPath, paths } from "./util" +import { canConnect, generateCertificate, generatePassword, humanPath, isNodeJSErrnoException, paths } from "./util" + +const DEFAULT_SOCKET_PATH = path.join(os.tmpdir(), "vscode-ipc") export enum Feature { /** Web socket compression. */ @@ -656,6 +658,26 @@ export const shouldRunVsCodeCli = (args: Args): boolean => { return extensionRelatedArgs.some((arg) => argKeys.includes(arg)) } +/** + * Reads the socketPath which defaults to a temporary directory + * with another directory called vscode-ipc. + * + * If it can't read the path, it throws an error and returns undefined. + */ +export async function readSocketPath(path: string): Promise { + try { + return await fs.readFile(path, "utf8") + } catch (error) { + // If it doesn't exist, we don't care. + // But if it fails for some reason, we should throw. + // We want to surface that to the user. + if (!isNodeJSErrnoException(error) || error.code !== "ENOENT") { + throw error + } + } + return undefined +} + /** * Determine if it looks like the user is trying to open a file or folder in an * existing instance. The arguments here should be the arguments the user @@ -667,24 +689,13 @@ export const shouldOpenInExistingInstance = async (args: Args): Promise => { - try { - return await fs.readFile(path.join(os.tmpdir(), "vscode-ipc"), "utf8") - } catch (error) { - if (error.code !== "ENOENT") { - throw error - } - } - return undefined - } - // If these flags are set then assume the user is trying to open in an // existing instance since these flags have no effect otherwise. const openInFlagCount = ["reuse-window", "new-window"].reduce((prev, cur) => { return args[cur as keyof Args] ? prev + 1 : prev }, 0) if (openInFlagCount > 0) { - return readSocketPath() + return readSocketPath(DEFAULT_SOCKET_PATH) } // It's possible the user is trying to spawn another instance of code-server. @@ -692,7 +703,7 @@ export const shouldOpenInExistingInstance = async (args: Args): Promise 0) { - const socketPath = await readSocketPath() + const socketPath = await readSocketPath(DEFAULT_SOCKET_PATH) if (socketPath && (await canConnect(socketPath))) { return socketPath } diff --git a/src/node/util.ts b/src/node/util.ts index ce92a3522535..e61be4ef8a8a 100644 --- a/src/node/util.ts +++ b/src/node/util.ts @@ -531,5 +531,5 @@ export function escapeHtml(unsafe: string): string { * it has a .code property. */ export function isNodeJSErrnoException(error: unknown): error is NodeJS.ErrnoException { - return error instanceof Error && (error as NodeJS.ErrnoException).code !== undefined + return error !== undefined && (error as NodeJS.ErrnoException).code !== undefined } diff --git a/test/unit/node/cli.test.ts b/test/unit/node/cli.test.ts index 97b648788440..c6f346d22691 100644 --- a/test/unit/node/cli.test.ts +++ b/test/unit/node/cli.test.ts @@ -8,14 +8,14 @@ import { bindAddrFromArgs, defaultConfigFile, parse, + readSocketPath, setDefaults, shouldOpenInExistingInstance, shouldRunVsCodeCli, splitOnFirstEquals, } from "../../../src/node/cli" -import { tmpdir } from "../../../src/node/constants" import { generatePassword, paths } from "../../../src/node/util" -import { useEnv } from "../../utils/helpers" +import { tmpdir, useEnv } from "../../utils/helpers" type Mutable = { -readonly [P in keyof T]: T[P] @@ -378,10 +378,11 @@ describe("parser", () => { describe("cli", () => { let args: Mutable = { _: [] } - const testDir = path.join(tmpdir, "tests/cli") const vscodeIpcPath = path.join(os.tmpdir(), "vscode-ipc") + let testDir: string beforeAll(async () => { + testDir = await tmpdir("cli") await fs.rmdir(testDir, { recursive: true }) await fs.mkdir(testDir, { recursive: true }) }) @@ -655,3 +656,40 @@ password: ${password} cert: false`) }) }) + +describe("readSocketPath", () => { + const fileContents = "readSocketPath file contents" + let tmpDirPath: string + let tmpFilePath: string + + beforeEach(async () => { + tmpDirPath = await tmpdir("readSocketPath") + tmpFilePath = path.join(tmpDirPath, "readSocketPath.txt") + await fs.writeFile(tmpFilePath, fileContents) + }) + + afterEach(async () => { + await fs.rmdir(tmpDirPath, { recursive: true }) + }) + + it("should throw an error if it can't read the file", async () => { + // TODO@jsjoeio - implement + // Test it on a directory.... ESDIR + // TODO@jsjoeio - implement + expect(() => readSocketPath(tmpDirPath)).rejects.toThrow("EISDIR") + }) + it("should return undefined if it can't read the file", async () => { + // TODO@jsjoeio - implement + const socketPath = await readSocketPath(path.join(tmpDirPath, "not-a-file")) + expect(socketPath).toBeUndefined() + }) + it("should return the file contents", async () => { + const contents = await readSocketPath(tmpFilePath) + expect(contents).toBe(fileContents) + }) + it("should return the same file contents for two different calls", async () => { + const contents1 = await readSocketPath(tmpFilePath) + const contents2 = await readSocketPath(tmpFilePath) + expect(contents2).toBe(contents1) + }) +})