From 0d84c1298a8cd3e5a3ebd40157fae73d35439720 Mon Sep 17 00:00:00 2001 From: Fuksss Date: Tue, 17 Nov 2026 15:39:36 +0100 Subject: [PATCH 1/4] fix(web): treat backslash home paths as read-only host files Match the server's home-path expansion so edits cannot save home-file content into a different file under the project. Build breadcrumbs using the workspace path style, preserving literal backslashes on POSIX. --- .../src/components/files/FileBreadcrumbs.tsx | 4 +- .../src/components/files/FilePreviewPanel.tsx | 12 ++++-- .../web/src/components/files/filePath.test.ts | 11 ++++- apps/web/src/components/files/filePath.ts | 41 +++++++++++++------ packages/shared/src/path.test.ts | 5 +++ packages/shared/src/path.ts | 10 +++-- 6 files changed, 59 insertions(+), 24 deletions(-) diff --git a/apps/web/src/components/files/FileBreadcrumbs.tsx b/apps/web/src/components/files/FileBreadcrumbs.tsx index a2f2120a2db3c..475b68b36f40f 100644 --- a/apps/web/src/components/files/FileBreadcrumbs.tsx +++ b/apps/web/src/components/files/FileBreadcrumbs.tsx @@ -294,8 +294,8 @@ function FolderBreadcrumb(props: { /** Host paths outside the workspace omit contents menus; the browser already lists their destination. */ export function FileBreadcrumbs(props: FileBreadcrumbsProps) { const hostPath = isHostPath(props.relativePath); - const breadcrumbs = fileBreadcrumbs(props.projectName, props.trail); - const currentPath = breadcrumbPathOf(props.relativePath); + const breadcrumbs = fileBreadcrumbs(props.projectName, props.trail, props.cwd); + const currentPath = breadcrumbPathOf(props.relativePath, props.cwd); const currentIndex = breadcrumbs.findIndex((crumb) => crumb.path === currentPath); return ( diff --git a/apps/web/src/components/files/FilePreviewPanel.tsx b/apps/web/src/components/files/FilePreviewPanel.tsx index 57b93a76353d2..29deebf72bb68 100644 --- a/apps/web/src/components/files/FilePreviewPanel.tsx +++ b/apps/web/src/components/files/FilePreviewPanel.tsx @@ -1225,7 +1225,7 @@ export default function FilePreviewPanel({ attachment !== undefined ? null : (relativePath ?? (folderTrail === undefined ? null : "")); // Reset crumbs when a host trail reaches the workspace root and no longer passes through the current path. const breadcrumbTrail = - breadcrumbPath === null ? null : retainedBreadcrumbTrail(folderTrail, breadcrumbPath); + breadcrumbPath === null ? null : retainedBreadcrumbTrail(folderTrail, breadcrumbPath, cwd); // A trail implies a folder before reads settle, preventing file-preview flashes while navigating up. const isFolder = attachment === undefined && @@ -1373,10 +1373,14 @@ export default function FilePreviewPanel({ // Navigating up preserves the file tab; retain the trail's path spelling to find its folder tab. const navigateToFolder = (crumbPath: string) => { const path = - breadcrumbTrail !== null && breadcrumbPathOf(breadcrumbTrail) === crumbPath + breadcrumbTrail !== null && breadcrumbPathOf(breadcrumbTrail, cwd) === crumbPath ? breadcrumbTrail : crumbPath; - onNavigateFolder(path, retainedBreadcrumbTrail(breadcrumbTrail ?? undefined, path), !isFolder); + onNavigateFolder( + path, + retainedBreadcrumbTrail(breadcrumbTrail ?? undefined, path, cwd), + !isFolder, + ); }; const absolutePath = breadcrumbPath === "" @@ -1777,7 +1781,7 @@ export default function FilePreviewPanel({ selectedPath={ hostFolderPath === null ? relativePath - : trailChildOf(hostFolderPath, breadcrumbTrail ?? hostFolderPath) + : trailChildOf(hostFolderPath, breadcrumbTrail ?? hostFolderPath, cwd) } selectedPathRevealId={revealRequestId} onOpenFile={onOpenFile} diff --git a/apps/web/src/components/files/filePath.test.ts b/apps/web/src/components/files/filePath.test.ts index fbe6a67ece922..036c087bf5119 100644 --- a/apps/web/src/components/files/filePath.test.ts +++ b/apps/web/src/components/files/filePath.test.ts @@ -33,16 +33,25 @@ describe("fileBreadcrumbs", () => { { label: ".claude", path: "~/.claude", kind: "directory" }, { label: "CLAUDE.md", path: "~/.claude/CLAUDE.md", kind: "file" }, ]); + expect(fileBreadcrumbs("t3code", "~\\.claude\\CLAUDE.md", "C:\\repo")).toEqual([ + { label: "~", path: "~/", kind: "directory" }, + { label: ".claude", path: "~/.claude", kind: "directory" }, + { label: "CLAUDE.md", path: "~/.claude/CLAUDE.md", kind: "file" }, + ]); expect(isListableHostFolder("~/")).toBe(true); expect(isListableHostFolder("~/.claude")).toBe(true); }); - it("keeps a literal backslash inside a workspace path segment", () => { + it("keeps literal backslashes inside POSIX path segments", () => { expect(fileBreadcrumbs("t3code", "dir\\one/report.txt")).toEqual([ { label: "t3code", path: "", kind: "project" }, { label: "dir\\one", path: "dir\\one", kind: "directory" }, { label: "report.txt", path: "dir\\one/report.txt", kind: "file" }, ]); + expect(fileBreadcrumbs("t3code", "~\\.claude\\CLAUDE.md", "/repo")).toEqual([ + { label: "~", path: "~/", kind: "directory" }, + { label: ".claude\\CLAUDE.md", path: "~/.claude\\CLAUDE.md", kind: "file" }, + ]); }); it("keeps a project folder named ~ inside the project", () => { diff --git a/apps/web/src/components/files/filePath.ts b/apps/web/src/components/files/filePath.ts index facefbb173016..cf4c01f67abb6 100644 --- a/apps/web/src/components/files/filePath.ts +++ b/apps/web/src/components/files/filePath.ts @@ -11,12 +11,23 @@ export interface FileBreadcrumbChild extends ProjectEntry { label: string; } -export function fileBreadcrumbs(projectName: string, relativePath: string): FileBreadcrumb[] { +export function fileBreadcrumbs( + projectName: string, + relativePath: string, + cwd?: string, +): FileBreadcrumb[] { const hostPath = isHostPath(relativePath); const windowsPath = isWindowsAbsolutePath(relativePath); + // Home expansion consumes the prefix; only Windows treats its remaining backslashes as separators. + const homePath = relativePath.startsWith("~\\") + ? "~/" + + (isWindowsAbsolutePath(cwd ?? "") + ? relativePath.slice(2).replaceAll("\\", "/") + : relativePath.slice(2)) + : relativePath; const separator = windowsPath ? "\\" : "/"; // `\` is a legal filename character outside Windows paths. - const parts = relativePath.split(windowsPath ? /[\\/]/ : "/").filter(Boolean); + const parts = homePath.split(windowsPath ? /[\\/]/ : "/").filter(Boolean); const root = relativePath.startsWith("\\\\") ? "\\\\" : relativePath.startsWith("/") ? "/" : ""; return [ ...(hostPath ? [] : [{ label: projectName, path: "", kind: "project" as const }]), @@ -33,9 +44,9 @@ export function fileBreadcrumbs(projectName: string, relativePath: string): File ]; } -/** Use the crumb's path spelling so trailing or repeated separators still match. */ -export function breadcrumbPathOf(relativePath: string): string { - return fileBreadcrumbs("", relativePath).at(-1)?.path ?? relativePath; +/** Use the destination's crumb spelling so trailing or repeated separators still match. */ +export function breadcrumbPathOf(relativePath: string, cwd?: string): string { + return fileBreadcrumbs("", relativePath, cwd).at(-1)?.path ?? relativePath; } /** UNC servers are not listable directories; their shares are. */ @@ -43,19 +54,23 @@ export function isListableHostFolder(path: string): boolean { return isHostPath(path) && !/^\\\\[^\\/]+$/.test(path); } -function trailCrumbIndex(trail: string, path: string): number { - const crumbPath = breadcrumbPathOf(path); - return fileBreadcrumbs("", trail).findIndex((crumb) => crumb.path === crumbPath); +function trailCrumbIndex(trail: string, path: string, cwd?: string): number { + const crumbPath = breadcrumbPathOf(path, cwd); + return fileBreadcrumbs("", trail, cwd).findIndex((crumb) => crumb.path === crumbPath); } /** Retain descendant crumbs when navigating up; other navigation resets the trail to path. */ -export function retainedBreadcrumbTrail(trail: string | undefined, path: string): string { - return trail !== undefined && trailCrumbIndex(trail, path) >= 0 ? trail : path; +export function retainedBreadcrumbTrail( + trail: string | undefined, + path: string, + cwd?: string, +): string { + return trail !== undefined && trailCrumbIndex(trail, path, cwd) >= 0 ? trail : path; } -export function trailChildOf(folder: string, trail: string): string | null { - const index = trailCrumbIndex(trail, folder); - return index < 0 ? null : (fileBreadcrumbs("", trail)[index + 1]?.label ?? null); +export function trailChildOf(folder: string, trail: string, cwd?: string): string | null { + const index = trailCrumbIndex(trail, folder, cwd); + return index < 0 ? null : (fileBreadcrumbs("", trail, cwd)[index + 1]?.label ?? null); } export function joinHostPath(folder: string, relativePath: string): string { diff --git a/packages/shared/src/path.test.ts b/packages/shared/src/path.test.ts index 514f92b8f54e0..e0023c33b8b92 100644 --- a/packages/shared/src/path.test.ts +++ b/packages/shared/src/path.test.ts @@ -6,9 +6,13 @@ import { normalizeProjectPathForDispatch, resolvePastedPath, resolveWorkspaceFilePath, + isHostPath, } from "./path.ts"; describe("path helpers", () => { + it("recognizes the server's backslash home prefix", () => { + expect(isHostPath("~\\.claude/CLAUDE.md")).toBe(true); + }); // regression: #17923 literal colons in names are kept, not parsed as positions it.each([ ["report:123", "/workspace", "/workspace/report:123"], @@ -59,6 +63,7 @@ describe("resolvePastedPath", () => { ["over a selection", "/Users/me/old", "'/Users/me/My Code'", "/Users/me/My Code"], ["after a prefilled folder", "~/", "~/'/Users/me/My Code'", "/Users/me/My Code"], ["plain, after a prefilled folder", "~/", "~//Users/me/code", "/Users/me/code"], + ["backslash home, after a folder", "~/", "~/~\\folder\\ child", "~\\folder\\ child"], ["after a Windows folder", "C:\\Users\\", 'C:\\Users\\"D:\\My Code"', "D:\\My Code"], ["a shell's quoted apostrophe", "", "'/Users/me/Bob'\\''s Work'", "/Users/me/Bob's Work"], ["after a clone destination", "~/code/repo", "~/code/repo'/a b'", "/a b"], diff --git a/packages/shared/src/path.ts b/packages/shared/src/path.ts index 45b49b200091c..df56890b363f6 100644 --- a/packages/shared/src/path.ts +++ b/packages/shared/src/path.ts @@ -134,9 +134,9 @@ export function isAbsolutePath(value: string): boolean { return value.startsWith("/") || isWindowsAbsolutePath(value); } -/** Home-relative host paths are expanded by the destination server. */ +/** Both ~/ and ~\ home prefixes are expanded by the destination server. */ export function isHostPath(value: string): boolean { - return isAbsolutePath(value) || value.startsWith("~/"); + return isAbsolutePath(value) || value.startsWith("~/") || value.startsWith("~\\"); } // Finder and Explorer copy a path ready for a shell: `'/a b'`, `/a\'s\ b`, or `"C:\a b"`. @@ -164,8 +164,10 @@ export function resolvePastedPath(previous: string, next: string): string { const afterFolder = /[/\\]$/.test(previous) && next.startsWith(previous); const path = afterFolder ? next.slice(previous.length) : next; if (!isHostPath(path)) return next; - // `\` separates folders in Windows paths, so it only escapes in `/` and `~/` paths. - return isWindowsAbsolutePath(path) ? path : path.replace(SHELL_ESCAPE, "$1"); + // Keep backslashes in Windows and `~\` paths; only `/` and `~/` paths use shell escapes. + return isWindowsAbsolutePath(path) || path.startsWith("~\\") + ? path + : path.replace(SHELL_ESCAPE, "$1"); } export function resolveWorkspaceFilePath(path: string, workspaceRoot: string): string { From 1fbd6b76cb5a9e1aa5ed3449f4818729f59b03f1 Mon Sep 17 00:00:00 2001 From: Fuksss Date: Tue, 17 Nov 2026 17:48:59 +0100 Subject: [PATCH 2/4] refactor(web): keep home breadcrumbs independent of workspace roots Use the smaller home-prefix fix selected in review. Keep relative-path backslashes literal and use the same spelling in all breadcrumb helpers. --- .../src/components/files/FileBreadcrumbs.tsx | 4 +- .../src/components/files/FilePreviewPanel.tsx | 12 ++---- .../web/src/components/files/filePath.test.ts | 8 +--- apps/web/src/components/files/filePath.ts | 41 +++++++------------ 4 files changed, 22 insertions(+), 43 deletions(-) diff --git a/apps/web/src/components/files/FileBreadcrumbs.tsx b/apps/web/src/components/files/FileBreadcrumbs.tsx index 475b68b36f40f..a2f2120a2db3c 100644 --- a/apps/web/src/components/files/FileBreadcrumbs.tsx +++ b/apps/web/src/components/files/FileBreadcrumbs.tsx @@ -294,8 +294,8 @@ function FolderBreadcrumb(props: { /** Host paths outside the workspace omit contents menus; the browser already lists their destination. */ export function FileBreadcrumbs(props: FileBreadcrumbsProps) { const hostPath = isHostPath(props.relativePath); - const breadcrumbs = fileBreadcrumbs(props.projectName, props.trail, props.cwd); - const currentPath = breadcrumbPathOf(props.relativePath, props.cwd); + const breadcrumbs = fileBreadcrumbs(props.projectName, props.trail); + const currentPath = breadcrumbPathOf(props.relativePath); const currentIndex = breadcrumbs.findIndex((crumb) => crumb.path === currentPath); return ( diff --git a/apps/web/src/components/files/FilePreviewPanel.tsx b/apps/web/src/components/files/FilePreviewPanel.tsx index 29deebf72bb68..57b93a76353d2 100644 --- a/apps/web/src/components/files/FilePreviewPanel.tsx +++ b/apps/web/src/components/files/FilePreviewPanel.tsx @@ -1225,7 +1225,7 @@ export default function FilePreviewPanel({ attachment !== undefined ? null : (relativePath ?? (folderTrail === undefined ? null : "")); // Reset crumbs when a host trail reaches the workspace root and no longer passes through the current path. const breadcrumbTrail = - breadcrumbPath === null ? null : retainedBreadcrumbTrail(folderTrail, breadcrumbPath, cwd); + breadcrumbPath === null ? null : retainedBreadcrumbTrail(folderTrail, breadcrumbPath); // A trail implies a folder before reads settle, preventing file-preview flashes while navigating up. const isFolder = attachment === undefined && @@ -1373,14 +1373,10 @@ export default function FilePreviewPanel({ // Navigating up preserves the file tab; retain the trail's path spelling to find its folder tab. const navigateToFolder = (crumbPath: string) => { const path = - breadcrumbTrail !== null && breadcrumbPathOf(breadcrumbTrail, cwd) === crumbPath + breadcrumbTrail !== null && breadcrumbPathOf(breadcrumbTrail) === crumbPath ? breadcrumbTrail : crumbPath; - onNavigateFolder( - path, - retainedBreadcrumbTrail(breadcrumbTrail ?? undefined, path, cwd), - !isFolder, - ); + onNavigateFolder(path, retainedBreadcrumbTrail(breadcrumbTrail ?? undefined, path), !isFolder); }; const absolutePath = breadcrumbPath === "" @@ -1781,7 +1777,7 @@ export default function FilePreviewPanel({ selectedPath={ hostFolderPath === null ? relativePath - : trailChildOf(hostFolderPath, breadcrumbTrail ?? hostFolderPath, cwd) + : trailChildOf(hostFolderPath, breadcrumbTrail ?? hostFolderPath) } selectedPathRevealId={revealRequestId} onOpenFile={onOpenFile} diff --git a/apps/web/src/components/files/filePath.test.ts b/apps/web/src/components/files/filePath.test.ts index 036c087bf5119..00a3247dc6508 100644 --- a/apps/web/src/components/files/filePath.test.ts +++ b/apps/web/src/components/files/filePath.test.ts @@ -33,7 +33,7 @@ describe("fileBreadcrumbs", () => { { label: ".claude", path: "~/.claude", kind: "directory" }, { label: "CLAUDE.md", path: "~/.claude/CLAUDE.md", kind: "file" }, ]); - expect(fileBreadcrumbs("t3code", "~\\.claude\\CLAUDE.md", "C:\\repo")).toEqual([ + expect(fileBreadcrumbs("t3code", "~\\.claude/CLAUDE.md")).toEqual([ { label: "~", path: "~/", kind: "directory" }, { label: ".claude", path: "~/.claude", kind: "directory" }, { label: "CLAUDE.md", path: "~/.claude/CLAUDE.md", kind: "file" }, @@ -42,16 +42,12 @@ describe("fileBreadcrumbs", () => { expect(isListableHostFolder("~/.claude")).toBe(true); }); - it("keeps literal backslashes inside POSIX path segments", () => { + it("keeps a literal backslash inside a workspace path segment", () => { expect(fileBreadcrumbs("t3code", "dir\\one/report.txt")).toEqual([ { label: "t3code", path: "", kind: "project" }, { label: "dir\\one", path: "dir\\one", kind: "directory" }, { label: "report.txt", path: "dir\\one/report.txt", kind: "file" }, ]); - expect(fileBreadcrumbs("t3code", "~\\.claude\\CLAUDE.md", "/repo")).toEqual([ - { label: "~", path: "~/", kind: "directory" }, - { label: ".claude\\CLAUDE.md", path: "~/.claude\\CLAUDE.md", kind: "file" }, - ]); }); it("keeps a project folder named ~ inside the project", () => { diff --git a/apps/web/src/components/files/filePath.ts b/apps/web/src/components/files/filePath.ts index cf4c01f67abb6..b7a9d2784d973 100644 --- a/apps/web/src/components/files/filePath.ts +++ b/apps/web/src/components/files/filePath.ts @@ -11,22 +11,13 @@ export interface FileBreadcrumbChild extends ProjectEntry { label: string; } -export function fileBreadcrumbs( - projectName: string, - relativePath: string, - cwd?: string, -): FileBreadcrumb[] { +export function fileBreadcrumbs(projectName: string, relativePath: string): FileBreadcrumb[] { const hostPath = isHostPath(relativePath); const windowsPath = isWindowsAbsolutePath(relativePath); - // Home expansion consumes the prefix; only Windows treats its remaining backslashes as separators. - const homePath = relativePath.startsWith("~\\") - ? "~/" + - (isWindowsAbsolutePath(cwd ?? "") - ? relativePath.slice(2).replaceAll("\\", "/") - : relativePath.slice(2)) - : relativePath; const separator = windowsPath ? "\\" : "/"; // `\` is a legal filename character outside Windows paths. + // The server reads `~\` as home; the rest keeps the usual relative-path spelling. + const homePath = relativePath.startsWith("~\\") ? `~/${relativePath.slice(2)}` : relativePath; const parts = homePath.split(windowsPath ? /[\\/]/ : "/").filter(Boolean); const root = relativePath.startsWith("\\\\") ? "\\\\" : relativePath.startsWith("/") ? "/" : ""; return [ @@ -44,9 +35,9 @@ export function fileBreadcrumbs( ]; } -/** Use the destination's crumb spelling so trailing or repeated separators still match. */ -export function breadcrumbPathOf(relativePath: string, cwd?: string): string { - return fileBreadcrumbs("", relativePath, cwd).at(-1)?.path ?? relativePath; +/** Use the crumb's path spelling so trailing or repeated separators still match. */ +export function breadcrumbPathOf(relativePath: string): string { + return fileBreadcrumbs("", relativePath).at(-1)?.path ?? relativePath; } /** UNC servers are not listable directories; their shares are. */ @@ -54,23 +45,19 @@ export function isListableHostFolder(path: string): boolean { return isHostPath(path) && !/^\\\\[^\\/]+$/.test(path); } -function trailCrumbIndex(trail: string, path: string, cwd?: string): number { - const crumbPath = breadcrumbPathOf(path, cwd); - return fileBreadcrumbs("", trail, cwd).findIndex((crumb) => crumb.path === crumbPath); +function trailCrumbIndex(trail: string, path: string): number { + const crumbPath = breadcrumbPathOf(path); + return fileBreadcrumbs("", trail).findIndex((crumb) => crumb.path === crumbPath); } /** Retain descendant crumbs when navigating up; other navigation resets the trail to path. */ -export function retainedBreadcrumbTrail( - trail: string | undefined, - path: string, - cwd?: string, -): string { - return trail !== undefined && trailCrumbIndex(trail, path, cwd) >= 0 ? trail : path; +export function retainedBreadcrumbTrail(trail: string | undefined, path: string): string { + return trail !== undefined && trailCrumbIndex(trail, path) >= 0 ? trail : path; } -export function trailChildOf(folder: string, trail: string, cwd?: string): string | null { - const index = trailCrumbIndex(trail, folder, cwd); - return index < 0 ? null : (fileBreadcrumbs("", trail, cwd)[index + 1]?.label ?? null); +export function trailChildOf(folder: string, trail: string): string | null { + const index = trailCrumbIndex(trail, folder); + return index < 0 ? null : (fileBreadcrumbs("", trail)[index + 1]?.label ?? null); } export function joinHostPath(folder: string, relativePath: string): string { From fd997e9d854a931732ea78d4fd620b786814ecab Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 09:10:49 +0000 Subject: [PATCH 3/4] test(shared): drop isHostPath case the home breadcrumb test already covers Co-Authored-By: Claude Opus 5.5 --- packages/shared/src/path.test.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/shared/src/path.test.ts b/packages/shared/src/path.test.ts index e0023c33b8b92..0170bb4a1aa85 100644 --- a/packages/shared/src/path.test.ts +++ b/packages/shared/src/path.test.ts @@ -6,13 +6,9 @@ import { normalizeProjectPathForDispatch, resolvePastedPath, resolveWorkspaceFilePath, - isHostPath, } from "./path.ts"; describe("path helpers", () => { - it("recognizes the server's backslash home prefix", () => { - expect(isHostPath("~\\.claude/CLAUDE.md")).toBe(true); - }); // regression: #17923 literal colons in names are kept, not parsed as positions it.each([ ["report:123", "/workspace", "/workspace/report:123"], From 090eebe91741038906399b23b791d3aa1191f4eb Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 18 Nov 2026 09:45:49 +0000 Subject: [PATCH 4/4] fix(shared): pasting a ~\ path into a folder box keeps folder browsing The folder browser navigates ~/ but not ~\ home paths, so treating a pasted ~\ path (or a ~\ base-directory setting) as a replacement left the Add project palette out of browse mode. Folder boxes keep main's handling; only the file panel reads ~\ as a home path. Co-Authored-By: Claude Opus 5.5 --- .../client-runtime/src/operations/projects.ts | 11 ++++++++--- packages/shared/src/path.test.ts | 2 +- packages/shared/src/path.ts | 15 +++++++++------ 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/client-runtime/src/operations/projects.ts b/packages/client-runtime/src/operations/projects.ts index 92532a6a90579..c56fa973c4501 100644 --- a/packages/client-runtime/src/operations/projects.ts +++ b/packages/client-runtime/src/operations/projects.ts @@ -11,7 +11,7 @@ import type { } from "@t3tools/contracts"; import { SourceControlProviderKind } from "@t3tools/contracts"; import { cloneUrlRepositoryPath } from "@t3tools/shared/cloneUrl"; -import { isExplicitRelativePath, isHostPath, newProjectFolderName } from "@t3tools/shared/path"; +import { isAbsolutePath, isExplicitRelativePath, newProjectFolderName } from "@t3tools/shared/path"; import * as Arr from "effect/Array"; import * as Option from "effect/Option"; import * as Order from "effect/Order"; @@ -222,8 +222,13 @@ export function buildAddProjectRemoteSourceReadiness( export function getAddProjectInitialQuery(baseDirectory: string | null | undefined): string { const trimmed = baseDirectory?.trim() ?? ""; if (trimmed.length === 0) return "~/"; - // A bare relative setting such as `Desktop/dev` means a folder under home. - const isAnchored = trimmed === "~" || isHostPath(trimmed) || isExplicitRelativePath(trimmed); + // A bare relative setting such as `Desktop/dev` means a folder under home. The browser + // navigates `~/` but not `~\` home paths. + const isAnchored = + trimmed === "~" || + isAbsolutePath(trimmed) || + trimmed.startsWith("~/") || + isExplicitRelativePath(trimmed); return ensureBrowseDirectoryPath(isAnchored ? trimmed : `~/${trimmed}`); } diff --git a/packages/shared/src/path.test.ts b/packages/shared/src/path.test.ts index 0170bb4a1aa85..9a9647c1bcd4b 100644 --- a/packages/shared/src/path.test.ts +++ b/packages/shared/src/path.test.ts @@ -59,7 +59,7 @@ describe("resolvePastedPath", () => { ["over a selection", "/Users/me/old", "'/Users/me/My Code'", "/Users/me/My Code"], ["after a prefilled folder", "~/", "~/'/Users/me/My Code'", "/Users/me/My Code"], ["plain, after a prefilled folder", "~/", "~//Users/me/code", "/Users/me/code"], - ["backslash home, after a folder", "~/", "~/~\\folder\\ child", "~\\folder\\ child"], + ["backslash home, after a folder", "~/", "~/~\\folder\\ child", "~/~\\folder\\ child"], ["after a Windows folder", "C:\\Users\\", 'C:\\Users\\"D:\\My Code"', "D:\\My Code"], ["a shell's quoted apostrophe", "", "'/Users/me/Bob'\\''s Work'", "/Users/me/Bob's Work"], ["after a clone destination", "~/code/repo", "~/code/repo'/a b'", "/a b"], diff --git a/packages/shared/src/path.ts b/packages/shared/src/path.ts index df56890b363f6..e4351b97b2626 100644 --- a/packages/shared/src/path.ts +++ b/packages/shared/src/path.ts @@ -143,6 +143,11 @@ export function isHostPath(value: string): boolean { const QUOTED_PATH_AT_END = /(?:'((?:[^']|'\\'')+)'|"([^"]+)")\s*$/; const SHELL_ESCAPE = /\\([\s'"\\()[\]{}<>|&;$`!*?#~^=])/g; +// Folder browsers only navigate `~/` home paths, so a pasted `~\` path is not a replacement. +function isPastedFolderPath(value: string): boolean { + return isAbsolutePath(value) || value.startsWith("~/"); +} + /** * What a folder box holds after a change. A full path pasted into it replaces the box, even after * the text already there, because `~/'/a b'` would create that nested folder. @@ -157,17 +162,15 @@ export function resolvePastedPath(previous: string, next: string): string { // Text before the quote that wasn't in the box is a search, not a folder to paste after. if (match && previous.startsWith(next.slice(0, match.index).trimEnd())) { const quoted = match[1]?.replaceAll("'\\''", "'") ?? match[2] ?? ""; - if (isHostPath(quoted)) return quoted; + if (isPastedFolderPath(quoted)) return quoted; } // Without a trailing separator, `/a` then `/a/b` is a longer path, not one pasted after a folder. const afterFolder = /[/\\]$/.test(previous) && next.startsWith(previous); const path = afterFolder ? next.slice(previous.length) : next; - if (!isHostPath(path)) return next; - // Keep backslashes in Windows and `~\` paths; only `/` and `~/` paths use shell escapes. - return isWindowsAbsolutePath(path) || path.startsWith("~\\") - ? path - : path.replace(SHELL_ESCAPE, "$1"); + if (!isPastedFolderPath(path)) return next; + // `\` separates folders in Windows paths, so it only escapes in `/` and `~/` paths. + return isWindowsAbsolutePath(path) ? path : path.replace(SHELL_ESCAPE, "$1"); } export function resolveWorkspaceFilePath(path: string, workspaceRoot: string): string {