Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: keep unc file links per platform

ogsamrat61f5f5b6fd88
diff --git a/apps/app/src/react-app/domains/session/sync/attachment-file-part.ts b/apps/app/src/react-app/domains/session/sync/attachment-file-part.ts
index fd286679..6a4713ca 100644
--- a/apps/app/src/react-app/domains/session/sync/attachment-file-part.ts
+++ b/apps/app/src/react-app/domains/session/sync/attachment-file-part.ts
@@ -2,7 +2,7 @@ import type { FilePartInput, TextPartInput } from "@opencode-ai/sdk/v2/client";
import type { ComposerAttachment } from "../../../../app/types";
import { compressImageFile } from "./image-compression";
-import { joinWorkspaceRelativePath, toFileUrl } from "./prompt-file-parts";
+import { isWindowsWorkspaceRoot, joinWorkspaceRelativePath, toFileUrl } from "./prompt-file-parts";
type AttachmentKind = "image" | "file";
@@ -413,7 +413,7 @@ export async function composerAttachmentsToWorkspaceFileParts(input: {
mime: metadata.mime,
bytes: result.bytes,
workspacePath,
- url: toFileUrl(absolutePath),
+ url: toFileUrl(absolutePath, isWindowsWorkspaceRoot(workspaceRoot)),
file,
});
}
diff --git a/apps/app/src/react-app/domains/session/sync/prompt-file-parts.ts b/apps/app/src/react-app/domains/session/sync/prompt-file-parts.ts
index 150f2d98..4bd251ea 100644
--- a/apps/app/src/react-app/domains/session/sync/prompt-file-parts.ts
+++ b/apps/app/src/react-app/domains/session/sync/prompt-file-parts.ts
@@ -67,16 +67,22 @@ function encodeFilePath(path: string) {
return path.replace(/\\/g, "/").split("/").map(encodeURIComponent).join("/");
}
-export function toFileUrl(path: string) {
+export function toFileUrl(path: string, windows = false) {
const normalized = path.replace(/\\/g, "/");
if (/^[A-Za-z]:\//.test(normalized)) return `file:///${encodeFilePath(normalized).replace(/^([A-Za-z])%3A/, "$1:")}`;
- // UNC share (`\\server\share\...`): the server is the URL host. `file:////server`
- // has an empty host, which Windows cannot turn back into a path.
- const unc = normalized.match(/^\/\/([^/]+)(\/.*)$/);
- if (unc) return `file://${encodeURIComponent(unc[1] ?? "")}${encodeFilePath(unc[2] ?? "")}`;
+ // UNC share on Windows (`\\server\share\...`): the server is the URL host.
+ // `file:////server` has an empty host, which Windows cannot turn back into a
+ // path. Elsewhere a host is invalid, so `//x` keeps the empty-host form.
+ const unc = windows ? normalized.match(/^\/\/([^/]+)(\/.*)$/) : null;
+ if (unc && isUncHost(unc[1] ?? "")) return `file://${unc[1]}${encodeFilePath(unc[2] ?? "")}`;
return `file://${encodeFilePath(normalized)}`;
}
+/** A plain UNC server name; device paths (`\\?\`, `\\.\`) and odd hosts are not. */
+function isUncHost(host: string) {
+ return /^[A-Za-z0-9][A-Za-z0-9._-]*$/.test(host);
+}
+
/** A workspace on Windows: a drive (`C:\\`) or UNC (`\\\\server\\share`) root. */
export function isWindowsWorkspaceRoot(workspaceRoot: string) {
const root = workspaceRoot.trim();
@@ -95,12 +101,12 @@ export function promptFileUrl(path: string, workspaceRoot: string) {
const normalized = path.trim().replace(/\\/g, "/");
if (!normalized) return "";
const drive = /^[A-Za-z]:\//.test(normalized);
- const unc = /^\/\/[^/]+\/[^/]/.test(normalized);
if (isWindowsWorkspaceRoot(workspaceRoot)) {
- if (!drive && !unc) return "";
- } else if (!drive && !normalized.startsWith("/")) {
- return "";
+ const unc = normalized.match(/^\/\/([^/]+)\/[^/]/);
+ if (!drive && !(unc && isUncHost(unc[1] ?? ""))) return "";
+ return toFileUrl(normalized, true);
}
+ if (!drive && !normalized.startsWith("/")) return "";
return toFileUrl(normalized);
}
diff --git a/apps/app/tests/prompt-file-url.test.ts b/apps/app/tests/prompt-file-url.test.ts
index f9a1b868..3b9d9994 100644
--- a/apps/app/tests/prompt-file-url.test.ts
+++ b/apps/app/tests/prompt-file-url.test.ts
@@ -16,6 +16,10 @@ function windowsPath(url: string) {
return fileURLToPath(url, { windows: true });
}
+function posixPath(url: string) {
+ return fileURLToPath(url, { windows: false });
+}
+
function fileUrls(parts: Array<{ type: string; url?: string }>) {
return parts.filter((part) => part.type === "file").map((part) => part.url ?? "");
}
@@ -54,8 +58,23 @@ describe("prompt file URLs", () => {
expect(promptFileUrl("/home/omar/code/a.ts", "/home/omar/code")).toBe("file:///home/omar/code/a.ts");
});
- test("UNC paths get the server as the URL host", () => {
- expect(toFileUrl("//server/share/x.ts")).toBe("file://server/share/x.ts");
+ test("UNC paths get the server as the URL host on Windows only", () => {
+ expect(toFileUrl("//server/share/x.ts", true)).toBe("file://server/share/x.ts");
+ expect(toFileUrl("//server/share/x.ts")).toBe("file:////server/share/x.ts");
+ });
+
+ test("double-slash paths on macOS and Linux stay valid POSIX URLs", () => {
+ const url = promptFileUrl("//server/share/x.ts", "/home/omar/code");
+ expect(posixPath(url)).toBe("//server/share/x.ts");
+ expect(posixPath(toFileUrl("//net/a.ts"))).toBe("//net/a.ts");
+ });
+
+ test("Windows device paths and odd hosts are not sent", () => {
+ const root = "C:\\Users\\omar\\code";
+ expect(promptFileUrl("\\\\?\\C:\\x\\a.ts", root)).toBe("");
+ expect(promptFileUrl("\\\\.\\pipe\\x", root)).toBe("");
+ expect(promptFileUrl("\\\\my host\\share\\a.ts", root)).toBe("");
+ expect(toFileUrl("//?/C:/x", true)).not.toStartWith("file://?");
});
});
@@ -76,6 +95,13 @@ describe("Windows first-line paths", () => {
});
});
+describe("POSIX first-line paths", () => {
+ test("a file URL with a host never becomes a URL the engine rejects", () => {
+ const parts = firstLineLocalFileParts("open file://server/share/a.ts and /home/omar/b.ts", "/home/omar/code");
+ for (const url of fileUrls(parts)) expect(() => posixPath(url)).not.toThrow();
+ });
+});
+
describe("Windows draft mentions", () => {
test("mentions resolve inside the workspace and never make an unreadable URL", async () => {
const parts = await draftToParts(
@@ -113,4 +139,19 @@ describe("Windows draft mentions", () => {
);
expect(fileUrls(parts)).toEqual(["file:///etc/hosts"]);
});
+
+ test("POSIX double-slash mentions stay valid POSIX URLs", async () => {
+ const parts = await draftToParts(
+ {
+ mode: "prompt",
+ text: "look at @//srv/share/a.ts",
+ parts: [{ type: "file", path: "//srv/share/a.ts" }],
+ attachments: [],
+ },
+ "/home/omar/code",
+ "ses_test",
+ null,
+ );
+ for (const url of fileUrls(parts)) expect(() => posixPath(url)).not.toThrow();
+ });
});