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
9 changes: 8 additions & 1 deletion packages/opencode/src/tool/external-directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function resolveWindowsForPermission(target: string, base: string, fs: Permissio
if (!stat) throw Object.assign(new Error("missing"), { code: "ENOENT" })
current = stat.isSymbolicLink() ? fs.realpath(candidate) : candidate
} catch (error: any) {
if (error?.code !== "ENOENT" && error?.code !== "ENOTDIR") throw error
if (!isMissingPermissionPath(error, candidate)) throw error
return AppFileSystem.normalizePath(path.win32.join(current, part, ...parts.slice(i + 1)), { base })
}
}
Expand Down Expand Up @@ -85,6 +85,13 @@ function windowsPermissionPath(target: string, base: string): string {
return uppercaseDriveRoot(`${base.replace(/[\\/]+$/, "")}\\${input}`)
}

function isMissingPermissionPath(error: any, candidate: string): boolean {
if (error?.code === "ENOENT" || error?.code === "ENOTDIR") return true
// Bun on Windows reports unreachable UNC components as EUNKNOWN; for permission metadata
// resolution they are equivalent to a not-yet-existing path under the UNC share.
return error?.code === "EUNKNOWN" && error?.syscall === "lstat" && /^\\\\/.test(candidate)
Comment thread
Astro-Han marked this conversation as resolved.
}

function stripWindowsExtendedPrefix(target: string): string {
if (/^\\\\\?\\UNC\\/i.test(target)) return target.replace(/^\\\\\?\\UNC\\/i, "\\\\")
if (/^\\\\\?\\[A-Za-z]:\\/i.test(target)) return target.slice(4)
Expand Down
23 changes: 23 additions & 0 deletions packages/opencode/test/tool/external-directory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,29 @@ describe("tool.assertExternalDirectory", () => {
})
})

test("treats unreachable UNC components as missing paths for permission metadata", async () => {
await withWin32Platform(async () => {
const unreachable = "\\\\server\\share\\outside"
const resolved = resolveExternalPathForPermission("\\\\?\\UNC\\server\\share\\outside\\file.txt", "D:\\project", {
lstat: (candidate) => {
if (candidate.toLowerCase() === unreachable.toLowerCase()) {
throw Object.assign(new Error("unknown"), {
code: "EUNKNOWN",
path: candidate,
syscall: "lstat",
})
}
return {
isSymbolicLink: () => false,
} as ReturnType<typeof import("fs").lstatSync>
},
realpath: (candidate) => candidate,
})

expect(resolved).toBe("\\\\server\\share\\outside\\file.txt")
})
})

if (process.platform === "win32") {
test("normalizes Windows path variants to one glob", async () => {
const { requests, ctx } = makeCtx()
Expand Down
Loading