Skip to content

Commit eb498b0

Browse files
authored
Remove humanPath (#6404)
The tilde is ambiguous and it can be helpful to know exactly what paths code-server is trying to use, especially if it is running as a different user than you expected.
1 parent eb8099f commit eb498b0

File tree

5 files changed

+10
-43
lines changed

5 files changed

+10
-43
lines changed

src/node/cli.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { field, Level, logger } from "@coder/logger"
22
import { promises as fs } from "fs"
33
import { load } from "js-yaml"
4-
import * as os from "os"
54
import * as path from "path"
6-
import { generateCertificate, generatePassword, humanPath, paths, splitOnFirstEquals } from "./util"
5+
import { generateCertificate, generatePassword, paths, splitOnFirstEquals } from "./util"
76
import { EditorSessionManagerClient } from "./vscodeSocket"
87

98
export enum Feature {
@@ -663,7 +662,7 @@ export async function readConfigFile(configPath?: string): Promise<ConfigArgs> {
663662
await fs.writeFile(configPath, defaultConfigFile(generatedPassword), {
664663
flag: "wx", // wx means to fail if the path exists.
665664
})
666-
logger.info(`Wrote default config file to ${humanPath(os.homedir(), configPath)}`)
665+
logger.info(`Wrote default config file to ${configPath}`)
667666
} catch (error: any) {
668667
// EEXIST is fine; we don't want to overwrite existing configurations.
669668
if (error.code !== "EEXIST") {

src/node/main.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { field, logger } from "@coder/logger"
22
import http from "http"
3-
import * as os from "os"
43
import { Disposable } from "../common/emitter"
54
import { plural } from "../common/util"
65
import { createApp, ensureAddress } from "./app"
76
import { AuthType, DefaultedArgs, Feature, SpawnCodeCli, toCodeArgs, UserProvidedArgs } from "./cli"
87
import { commit, version } from "./constants"
98
import { register } from "./routes"
10-
import { humanPath, isDirectory, loadAMDModule, open } from "./util"
9+
import { isDirectory, loadAMDModule, open } from "./util"
1110

1211
/**
1312
* Return true if the user passed an extension-related VS Code flag.
@@ -109,8 +108,8 @@ export const runCodeServer = async (
109108
): Promise<{ dispose: Disposable["dispose"]; server: http.Server }> => {
110109
logger.info(`code-server ${version} ${commit}`)
111110

112-
logger.info(`Using user-data-dir ${humanPath(os.homedir(), args["user-data-dir"])}`)
113-
logger.trace(`Using extensions-dir ${humanPath(os.homedir(), args["extensions-dir"])}`)
111+
logger.info(`Using user-data-dir ${args["user-data-dir"]}`)
112+
logger.trace(`Using extensions-dir ${args["extensions-dir"]}`)
114113

115114
if (args.auth === AuthType.Password && !args.password && !args["hashed-password"]) {
116115
throw new Error(
@@ -123,7 +122,7 @@ export const runCodeServer = async (
123122
const serverAddress = ensureAddress(app.server, protocol)
124123
const disposeRoutes = await register(app, args)
125124

126-
logger.info(`Using config file ${humanPath(os.homedir(), args.config)}`)
125+
logger.info(`Using config file ${args.config}`)
127126
logger.info(`${protocol.toUpperCase()} server listening on ${serverAddress.toString()}`)
128127
if (args.auth === AuthType.Password) {
129128
logger.info(" - Authentication is enabled")
@@ -132,14 +131,14 @@ export const runCodeServer = async (
132131
} else if (args.usingEnvHashedPassword) {
133132
logger.info(" - Using password from $HASHED_PASSWORD")
134133
} else {
135-
logger.info(` - Using password from ${humanPath(os.homedir(), args.config)}`)
134+
logger.info(` - Using password from ${args.config}`)
136135
}
137136
} else {
138137
logger.info(" - Authentication is disabled")
139138
}
140139

141140
if (args.cert) {
142-
logger.info(` - Using certificate for HTTPS: ${humanPath(os.homedir(), args.cert.value)}`)
141+
logger.info(` - Using certificate for HTTPS: ${args.cert.value}`)
143142
} else {
144143
logger.info(" - Not serving HTTPS")
145144
}

src/node/routes/login.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { Router, Request } from "express"
22
import { promises as fs } from "fs"
33
import { RateLimiter as Limiter } from "limiter"
4-
import * as os from "os"
54
import * as path from "path"
65
import { CookieKeys } from "../../common/http"
76
import { rootPath } from "../constants"
87
import { authenticated, getCookieOptions, redirect, replaceTemplates } from "../http"
9-
import { getPasswordMethod, handlePasswordValidation, humanPath, sanitizeString, escapeHtml } from "../util"
8+
import { getPasswordMethod, handlePasswordValidation, sanitizeString, escapeHtml } from "../util"
109
import i18n from "../i18n"
1110

1211
// RateLimiter wraps around the limiter library for logins.
@@ -33,7 +32,7 @@ const getRoot = async (req: Request, error?: Error): Promise<string> => {
3332
i18n.changeLanguage(locale)
3433
const appName = req.args["app-name"] || "code-server"
3534
const welcomeText = req.args["welcome-text"] || (i18n.t("WELCOME", { app: appName }) as string)
36-
let passwordMsg = i18n.t("LOGIN_PASSWORD", { configFile: humanPath(os.homedir(), req.args.config) })
35+
let passwordMsg = i18n.t("LOGIN_PASSWORD", { configFile: req.args.config })
3736
if (req.args.usingEnvPassword) {
3837
passwordMsg = i18n.t("LOGIN_USING_ENV_PASSWORD")
3938
} else if (req.args.usingEnvHashedPassword) {

src/node/util.ts

-14
Original file line numberDiff line numberDiff line change
@@ -87,20 +87,6 @@ export function getEnvPaths(platform = process.platform): Paths {
8787
}
8888
}
8989

90-
/**
91-
* humanPath replaces the home directory in path with ~.
92-
* Makes it more readable.
93-
*
94-
* @param homedir - the home directory(i.e. `os.homedir()`)
95-
* @param path - a file path
96-
*/
97-
export function humanPath(homedir: string, path?: string): string {
98-
if (!path) {
99-
return ""
100-
}
101-
return path.replace(homedir, "~")
102-
}
103-
10490
export const generateCertificate = async (hostname: string): Promise<{ cert: string; certKey: string }> => {
10591
const certPath = path.join(paths.data, `${hostname.replace(/\./g, "_")}.crt`)
10692
const certKeyPath = path.join(paths.data, `${hostname.replace(/\./g, "_")}.key`)

test/unit/node/util.test.ts

-16
Original file line numberDiff line numberDiff line change
@@ -491,22 +491,6 @@ describe("isDirectory", () => {
491491
})
492492
})
493493

494-
describe("humanPath", () => {
495-
it("should return an empty string if no path provided", () => {
496-
const mockHomedir = "/home/coder"
497-
const actual = util.humanPath(mockHomedir)
498-
const expected = ""
499-
expect(actual).toBe(expected)
500-
})
501-
it("should replace the homedir with ~", () => {
502-
const mockHomedir = "/home/coder"
503-
const path = `${mockHomedir}/code-server`
504-
const actual = util.humanPath(mockHomedir, path)
505-
const expected = "~/code-server"
506-
expect(actual).toBe(expected)
507-
})
508-
})
509-
510494
describe("isWsl", () => {
511495
const testName = "wsl"
512496

0 commit comments

Comments
 (0)