Skip to content

Commit f07405c

Browse files
authored
Expand ~ in path options from the config file (#8055)
The shell expands ~ in command-line arguments, but values read from config.yaml were passed to path.resolve as-is, so `user-data-dir: ~/.config/code-server-data` resolved to `<cwd>/~/.config/code-server-data`.
1 parent c5fee2d commit f07405c

4 files changed

Lines changed: 45 additions & 2 deletions

File tree

‎src/node/cli.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as crypto from "crypto"
33
import { promises as fs } from "fs"
44
import { load } from "js-yaml"
55
import * as path from "path"
6-
import { generateCertificate, generatePassword, paths, splitOnFirstEquals } from "./util"
6+
import { expandTilde, generateCertificate, generatePassword, paths, splitOnFirstEquals } from "./util"
77
import { EditorSessionManagerClient } from "./vscodeSocket"
88

99
export enum Feature {
@@ -466,7 +466,7 @@ export const parse = (
466466
}
467467

468468
if (option.path) {
469-
value = path.resolve(value)
469+
value = path.resolve(expandTilde(value))
470470
}
471471

472472
switch (option.type) {

‎src/node/util.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,18 @@ export function splitOnFirstEquals(str: string): [string, string | undefined] {
511511
const split = str.split(/=(.+)?/, 2)
512512
return [split[0], split[1]]
513513
}
514+
515+
/**
516+
* Expand a leading `~` to the home directory. The shell does this for
517+
* command-line arguments but values from the config file are not expanded.
518+
* `~user` forms are left alone.
519+
*/
520+
export function expandTilde(p: string): string {
521+
if (p === "~") {
522+
return os.homedir()
523+
}
524+
if (p.startsWith("~/") || p.startsWith(`~${path.sep}`)) {
525+
return path.join(os.homedir(), p.slice(2))
526+
}
527+
return p
528+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Level, logger } from "@coder/logger"
22
import { promises as fs } from "fs"
3+
import * as os from "os"
34
import * as path from "path"
45
import {
56
UserProvidedArgs,
@@ -575,6 +576,17 @@ describe("parser", () => {
575576
"install-extension": ["extension.number1", "extension.number2"],
576577
})
577578
})
579+
it("should expand ~ in path options from the config file", () => {
580+
const configContents = `
581+
user-data-dir: ~/.config/code-server-data
582+
extensions-dir: ~/extensions
583+
`
584+
expect(parseConfigFile(configContents, "/fake-config-path")).toEqual({
585+
config: "/fake-config-path",
586+
"user-data-dir": path.join(os.homedir(), ".config/code-server-data"),
587+
"extensions-dir": path.join(os.homedir(), "extensions"),
588+
})
589+
})
578590
it("should ignore optional strings set to false", async () => {
579591
expect(parse(["--cert=false"])).toEqual({})
580592
})

‎test/unit/node/util.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as cp from "child_process"
22
import { promises as fs } from "fs"
3+
import * as os from "os"
34
import * as path from "path"
45
import { generateUuid } from "../../../src/common/util"
56
import { tmpdir } from "../../../src/node/constants"
@@ -623,3 +624,18 @@ describe("splitOnFirstEquals", () => {
623624
})
624625
})
625626
})
627+
628+
describe("expandTilde", () => {
629+
it("should expand a bare ~", () => {
630+
expect(util.expandTilde("~")).toBe(os.homedir())
631+
})
632+
it("should expand a leading ~/", () => {
633+
expect(util.expandTilde("~/.config/code-server")).toBe(path.join(os.homedir(), ".config/code-server"))
634+
})
635+
it("should leave other paths alone", () => {
636+
expect(util.expandTilde("/tmp/~/data")).toBe("/tmp/~/data")
637+
expect(util.expandTilde("relative/~")).toBe("relative/~")
638+
expect(util.expandTilde("~user/data")).toBe("~user/data")
639+
expect(util.expandTilde("")).toBe("")
640+
})
641+
})

0 commit comments

Comments
 (0)