Skip to content

Commit eed581e

Browse files
cliffhallclaude
andcommitted
fix(filesystem): expand ~\ to the home directory on Windows
expandHome recognized only `~` and `~/`, so a Windows user's `~\projects` (a command-line directory, a request path or a plain-path client Root) resolved to a literal `~` directory under the working directory. Expand `~\` as well, on win32 only: on POSIX a backslash is a filename character and `~\notes` names a file. parseRootUri carried its own copy of the `~/` check; it now calls expandHome, so a Root and a command-line directory agree. Closes #5180 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 22ce3e5 commit eed581e

5 files changed

Lines changed: 76 additions & 9 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-filesystem": patch
3+
---
4+
5+
Expand `~\` to the home directory on Windows, in command-line directories, request paths and client Roots alike, as `~/` already is. On POSIX a backslash is a filename character, so `~\name` there still names a literal file.

‎src/filesystem/__tests__/path-utils.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,19 @@ describe("Path Utilities", () => {
264264
it("leaves other paths unchanged", () => {
265265
expect(expandHome("C:/test")).toBe("C:/test");
266266
});
267+
268+
// On POSIX a backslash is a filename character, so ~\notes names a
269+
// file in the working directory, not one in the home directory (#5180).
270+
// win32.test.ts covers the Windows expansion.
271+
it("leaves ~\\ alone on POSIX", () => {
272+
const realPlatform = process.platform;
273+
Object.defineProperty(process, "platform", { value: "linux" });
274+
try {
275+
expect(expandHome("~\\notes")).toBe("~\\notes");
276+
} finally {
277+
Object.defineProperty(process, "platform", { value: realPlatform });
278+
}
279+
});
267280
});
268281

269282
describe("WSL path handling (issue #2795 fix)", () => {

‎src/filesystem/__tests__/win32.test.ts‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
// Windows-only branches of path-utils.ts and path-validation.ts run here:
44
// drive roots, bare drive letters, backslash conversion and UNC shares.
55

6+
import { promises as fsp, type Stats } from "fs";
7+
import os from "os";
68
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
79

810
vi.mock("path", async () => {
911
const actual = await vi.importActual<typeof import("path")>("path");
1012
return { ...actual.win32, default: actual.win32 };
1113
});
1214

13-
const { normalizePath } = await import("../path-utils.js");
15+
const { normalizePath, expandHome } = await import("../path-utils.js");
16+
const { getValidRootDirectories } = await import("../roots-utils.js");
1417
const { isPathWithinAllowedDirectories } =
1518
await import("../path-validation.js");
1619

@@ -141,3 +144,43 @@ describe("isPathWithinAllowedDirectories on win32", () => {
141144
).toBe(false);
142145
});
143146
});
147+
148+
// Windows users write a home-relative path with a backslash (#5180). The
149+
// home directory is stubbed to a Windows spelling, since the host's is POSIX.
150+
describe("~\\ on win32", () => {
151+
const home = "C:\\Users\\me";
152+
153+
it("expands ~\\ to the home directory, as ~/ is", () => {
154+
const spy = vi.spyOn(os, "homedir").mockReturnValue(home);
155+
try {
156+
expect(expandHome("~\\projects")).toBe("C:\\Users\\me\\projects");
157+
expect(expandHome("~\\projects")).toBe(expandHome("~/projects"));
158+
} finally {
159+
spy.mockRestore();
160+
}
161+
});
162+
163+
it("leaves a ~ that does not start a segment alone", () => {
164+
expect(expandHome("~user\\projects")).toBe("~user\\projects");
165+
expect(expandHome("C:\\~\\x")).toBe("C:\\~\\x");
166+
});
167+
168+
it("expands a ~\\ client Root through the same expandHome", async () => {
169+
const spies = [
170+
vi.spyOn(os, "homedir").mockReturnValue(home),
171+
// The directory does not exist on this host: realpath and stat answer
172+
// as Windows would for an existing directory.
173+
vi.spyOn(fsp, "realpath").mockImplementation(async (p) => String(p)),
174+
vi
175+
.spyOn(fsp, "stat")
176+
.mockResolvedValue({ isDirectory: () => true } as Stats),
177+
];
178+
try {
179+
expect(await getValidRootDirectories([{ uri: "~\\projects" }])).toEqual([
180+
"C:\\Users\\me\\projects",
181+
]);
182+
} finally {
183+
for (const spy of spies) spy.mockRestore();
184+
}
185+
});
186+
});

‎src/filesystem/path-utils.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,18 @@ export function normalizePathFormat(p: string): string {
124124
}
125125

126126
/**
127-
* Expands home directory tildes in paths
127+
* Expands home directory tildes in paths: a bare `~`, `~/…` and, on Windows
128+
* only, `~\…` (#5180). On POSIX a backslash is an ordinary filename
129+
* character, so `~\notes` there names a file and is left as it is.
128130
* @param filepath The path to expand
129131
* @returns Expanded path
130132
*/
131133
export function expandHome(filepath: string): string {
132-
if (filepath.startsWith("~/") || filepath === "~") {
134+
if (
135+
filepath === "~" ||
136+
filepath.startsWith("~/") ||
137+
(process.platform === "win32" && filepath.startsWith("~\\"))
138+
) {
133139
return path.join(os.homedir(), filepath.slice(1));
134140
}
135141
return filepath;

‎src/filesystem/roots-utils.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { promises as fs, type Stats } from "fs";
22
import path from "path";
3-
import os from "os";
4-
import { normalizePathFormat, realpathWithRetry } from "./path-utils.js";
3+
import {
4+
normalizePathFormat,
5+
realpathWithRetry,
6+
expandHome,
7+
} from "./path-utils.js";
58
import type { Root } from "@modelcontextprotocol/server";
69
import { fileURLToPath } from "url";
710

@@ -25,10 +28,7 @@ async function parseRootUri(rootUri: string): Promise<RootPaths | null> {
2528
const rawPath = rootUri.startsWith("file://")
2629
? fileURLToPath(rootUri)
2730
: rootUri;
28-
const expandedPath =
29-
rawPath.startsWith("~/") || rawPath === "~"
30-
? path.join(os.homedir(), rawPath.slice(1))
31-
: rawPath;
31+
const expandedPath = expandHome(rawPath);
3232
const absolutePath = path.resolve(expandedPath);
3333
const resolvedPath = await realpathWithRetry(absolutePath);
3434
return {

0 commit comments

Comments
 (0)