From b3f233c64c7bfec0cc2e87b56be1bcbbc1e6a045 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Sun, 22 Nov 2026 21:25:32 -0600 Subject: [PATCH] fix(app): open ~ file links in the server's home folder (#65689) --- packages/app/src/workspaces/files/model.tsx | 5 +++-- .../app/src/workspaces/files/path.test.ts | 20 +++++++++++++++++++ packages/app/src/workspaces/files/path.ts | 5 ++++- packages/gui-plugins/src/file/renderer.tsx | 3 ++- .../gui-plugins/src/file/resolve-link.test.ts | 16 +++++++++++++++ packages/gui-plugins/src/file/resolve-link.ts | 14 ++++++++----- 6 files changed, 54 insertions(+), 9 deletions(-) diff --git a/packages/app/src/workspaces/files/model.tsx b/packages/app/src/workspaces/files/model.tsx index 22994384cad..3820fca3ab0 100644 --- a/packages/app/src/workspaces/files/model.tsx +++ b/packages/app/src/workspaces/files/model.tsx @@ -28,7 +28,7 @@ import { } from "./content-machine" import { createFileViewCache } from "./view-cache" import { useServerSDK } from "@/runtime/server/client" -import { useData } from "@/runtime/server/current" +import { useData, useServer } from "@/runtime/server/current" import { formatServerError } from "@/runtime/server/errors" import { createFileTreeStore } from "./tree-store" import { invalidateFromWatcher } from "./watcher" @@ -46,12 +46,13 @@ export const { use: useFile, provider: FileProvider } = createSimpleContext({ const params = useParams() const serverSDK = useServerSDK() const data = useData() + const server = useServer() const platform = usePlatform() const language = useLanguage() const attachment = usePluginAttachment() const scope = createMemo(() => sdk().directory) - const path = createPathHelpers(scope) + const path = createPathHelpers(scope, () => server.ctx.sync.data.path.home) const cache = createFileContentCache() const [store, setStore] = createStore({ machine: createFileContentState(untrack(scope)) }) diff --git a/packages/app/src/workspaces/files/path.test.ts b/packages/app/src/workspaces/files/path.test.ts index 50bc59f04e6..b813e548d1b 100644 --- a/packages/app/src/workspaces/files/path.test.ts +++ b/packages/app/src/workspaces/files/path.test.ts @@ -30,6 +30,26 @@ describe("file path helpers", () => { expect(windows.normalize("file:///C:/repo/src/app.ts")).toBe("src/app.ts") }) + test("expands the server's home folder", () => { + const posix = createPathHelpers( + () => "/Users/me/repo", + () => "/Users/me", + ) + + expect(posix.normalize("~/Downloads/clip.mp4")).toBe("/Users/me/Downloads/clip.mp4") + expect(posix.normalize("~")).toBe("/Users/me") + expect(posix.normalize("~/repo/src/app.ts")).toBe("src/app.ts") + expect(posix.normalize("~other/file")).toBe("~other/file") + expect(createPathHelpers(() => "/repo").normalize("~/Downloads/clip.mp4")).toBe("~/Downloads/clip.mp4") + + const windows = createPathHelpers( + () => "C:\\repo", + () => "C:\\Users\\me\\", + ) + + expect(windows.normalize("~\\Downloads\\clip.mp4")).toBe("C:\\Users\\me\\Downloads\\clip.mp4") + }) + test.each([ ["/repo", "src/components///", "src/components"], ["C:\\repo", "frontend\\", "frontend"], diff --git a/packages/app/src/workspaces/files/path.ts b/packages/app/src/workspaces/files/path.ts index 25742622dd4..c79d8b45160 100644 --- a/packages/app/src/workspaces/files/path.ts +++ b/packages/app/src/workspaces/files/path.ts @@ -88,7 +88,7 @@ export function decodeFilePath(input: string) { } } -export function createPathHelpers(scope: () => string) { +export function createPathHelpers(scope: () => string, home: () => string = () => "") { const normalize = (input: string) => { const root = scope() @@ -98,6 +98,9 @@ export function createPathHelpers(scope: () => string) { "$1", ) + // `~` names the server's home folder, as it does in the paths agents write. + if (home() && /^~(?:[/\\]|$)/.test(path)) path = home().replace(/[/\\]+$/, "") + path.slice(1) + // Separator-agnostic prefix stripping for Cygwin/native Windows compatibility // Only case-insensitive on Windows (drive letter or UNC paths) const windows = /^[A-Za-z]:/.test(root) || root.startsWith("\\\\") diff --git a/packages/gui-plugins/src/file/renderer.tsx b/packages/gui-plugins/src/file/renderer.tsx index 7e2a49f91d9..61f30ead903 100644 --- a/packages/gui-plugins/src/file/renderer.tsx +++ b/packages/gui-plugins/src/file/renderer.tsx @@ -702,7 +702,8 @@ const setup: Plugin.Setup = (ctx) => { const resolve = (files: Files, value: string, base?: string) => { const root = files.root.replaceAll("\\", "/").replace(/\/+$/, "") - if (/^skill:\/\//i.test(value) || /^[a-z]:\//i.test(value) || value.startsWith("/")) return files.resolve(value) + if (/^skill:\/\//i.test(value) || /^[a-z]:\//i.test(value) || /^(?:\/|~(?:\/|$))/.test(value)) + return files.resolve(value) const relative = resolveArtifactPath(base ?? "", value) diff --git a/packages/gui-plugins/src/file/resolve-link.test.ts b/packages/gui-plugins/src/file/resolve-link.test.ts index 2ac5e12ba39..8c695dd2fda 100644 --- a/packages/gui-plugins/src/file/resolve-link.test.ts +++ b/packages/gui-plugins/src/file/resolve-link.test.ts @@ -111,4 +111,20 @@ describe("checkFileLinkExists", () => { expect(await check("\\\\host\\share\\x.ts")).toBe(false) expect(await check("console.log")).toBe(false) }) + + test("checks a home path once the files API expands it", async () => { + const check = (href: string) => + checkFileLinkExists({ + files: { + ...files, + exists: async (path: string) => path === "/Users/me/Downloads/clip.mp4", + resolve: (path: string) => path.replace(/^~/, "/Users/me"), + }, + href, + signal: new AbortController().signal, + }) + + expect(await check("~/Downloads/clip.mp4")).toBe(true) + expect(await check("~/Downloads/missing.mp4")).toBe(false) + }) }) diff --git a/packages/gui-plugins/src/file/resolve-link.ts b/packages/gui-plugins/src/file/resolve-link.ts index 465a764fb94..a00d46d6236 100644 --- a/packages/gui-plugins/src/file/resolve-link.ts +++ b/packages/gui-plugins/src/file/resolve-link.ts @@ -25,7 +25,7 @@ export function parseFileLink(href: string): ParsedFileLink { } export function isAbsoluteLink(path: string) { - return /^skill:\/\//i.test(path) || /^[a-z]:\//i.test(path) || path.startsWith("/") + return /^skill:\/\//i.test(path) || /^[a-z]:\//i.test(path) || path.startsWith("/") || /^~(?:\/|$)/.test(path) } /** @@ -63,11 +63,15 @@ export async function checkFileLinkExists(input: { }): Promise { const parsed = parseFileLink(input.href) - // `~` is the server's home folder, which the app cannot expand. A network share (`//host/share`) would make the - // server connect to the host just to answer. - if (!parsed.path || parsed.path.startsWith("~") || parsed.path.startsWith("//")) return false + // A network share (`//host/share`) would make the server connect to the host just to answer. + if (!parsed.path || parsed.path.startsWith("//")) return false - if (isAbsoluteLink(parsed.path)) return input.files.exists(input.files.resolve(parsed.path)) + if (isAbsoluteLink(parsed.path)) { + const resolved = input.files.resolve(parsed.path) + + // `resolve` expands `~` once the server has reported its home folder; until then the path is unknown. + return !resolved.startsWith("~") && input.files.exists(resolved) + } return !!(await findFileLink({ files: input.files, path: parsed.path, signal: input.signal })) }