Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/web/src/components/files/filePath.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ describe("fileBreadcrumbs", () => {
{ label: ".claude", path: "~/.claude", kind: "directory" },
{ label: "CLAUDE.md", path: "~/.claude/CLAUDE.md", kind: "file" },
]);
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" },
]);
expect(isListableHostFolder("~/")).toBe(true);
expect(isListableHostFolder("~/.claude")).toBe(true);
});
Expand Down
4 changes: 3 additions & 1 deletion apps/web/src/components/files/filePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export function fileBreadcrumbs(projectName: string, relativePath: string): File
const windowsPath = isWindowsAbsolutePath(relativePath);
const separator = windowsPath ? "\\" : "/";
// `\` is a legal filename character outside Windows paths.
const parts = relativePath.split(windowsPath ? /[\\/]/ : "/").filter(Boolean);
// 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 [
...(hostPath ? [] : [{ label: projectName, path: "", kind: "project" as const }]),
Expand Down
11 changes: 8 additions & 3 deletions packages/client-runtime/src/operations/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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}`);
}

Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +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"],
["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"],
Expand Down
13 changes: 9 additions & 4 deletions packages/shared/src/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,20 @@ 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"`.
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.
Expand All @@ -157,13 +162,13 @@ 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;
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");
}
Expand Down
Loading