Skip to content

Commit a5cf018

Browse files
authored
Merge pull request #3751 from cdr/jsjoeio-fix-type-confusion
fix: check uri.path is string in pathToFsPath
2 parents c145008 + 7ce9ee0 commit a5cf018

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/node/util.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as path from "path"
1010
import safeCompare from "safe-compare"
1111
import * as util from "util"
1212
import xdgBasedir from "xdg-basedir"
13+
import { getFirstString } from "../common/util"
1314

1415
export interface Paths {
1516
data: string
@@ -457,10 +458,17 @@ enum CharCode {
457458
* Taken from vs/base/common/uri.ts. It's not imported to avoid also importing
458459
* everything that file imports.
459460
*/
460-
export function pathToFsPath(path: string, keepDriveLetterCasing = false): string {
461+
export function pathToFsPath(path: string | string[], keepDriveLetterCasing = false): string {
461462
const isWindows = process.platform === "win32"
462-
const uri = { authority: undefined, path, scheme: "file" }
463+
const uri = { authority: undefined, path: getFirstString(path), scheme: "file" }
463464
let value: string
465+
466+
if (typeof uri.path !== "string") {
467+
throw new Error(
468+
`Could not compute fsPath from given uri. Expected path to be of type string, but was of type ${typeof uri.path}.`,
469+
)
470+
}
471+
464472
if (uri.authority && uri.path.length > 1 && uri.scheme === "file") {
465473
// unc path: file://shares/c$/far/boo
466474
value = `//${uri.authority}${uri.path}`

test/unit/node/util.test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -453,3 +453,44 @@ describe("escapeHtml", () => {
453453
)
454454
})
455455
})
456+
457+
describe("pathToFsPath", () => {
458+
it("should convert a path to a file system path", () => {
459+
expect(util.pathToFsPath("/foo/bar/baz")).toBe("/foo/bar/baz")
460+
})
461+
it("should lowercase drive letter casing by default", () => {
462+
expect(util.pathToFsPath("/C:/far/boo")).toBe("c:/far/boo")
463+
})
464+
it("should keep drive letter casing when set to true", () => {
465+
expect(util.pathToFsPath("/C:/far/bo", true)).toBe("C:/far/bo")
466+
})
467+
it("should throw an error if a non-string is passed in for path", () => {
468+
expect(() =>
469+
util
470+
// @ts-expect-error We need to check other types
471+
.pathToFsPath({}),
472+
).toThrow(
473+
`Could not compute fsPath from given uri. Expected path to be of type string, but was of type undefined.`,
474+
)
475+
})
476+
it("should not throw an error for a string array", () => {
477+
// @ts-expect-error We need to check other types
478+
expect(() => util.pathToFsPath(["/hello/foo", "/hello/bar"]).not.toThrow())
479+
})
480+
it("should use the first string in a string array", () => {
481+
expect(util.pathToFsPath(["/hello/foo", "/hello/bar"])).toBe("/hello/foo")
482+
})
483+
it("should replace / with \\ on Windows", () => {
484+
let ORIGINAL_PLATFORM = process.platform
485+
486+
Object.defineProperty(process, "platform", {
487+
value: "win32",
488+
})
489+
490+
expect(util.pathToFsPath("/C:/far/boo")).toBe("c:\\far\\boo")
491+
492+
Object.defineProperty(process, "platform", {
493+
value: ORIGINAL_PLATFORM,
494+
})
495+
})
496+
})

0 commit comments

Comments
 (0)