mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-27 07:38:51 +00:00
fix(app): open ~ file links in the server's home folder (#65689)
This commit is contained in:
parent
c6df1612c3
commit
b3f233c64c
6 changed files with 54 additions and 9 deletions
|
|
@ -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)) })
|
||||
|
|
|
|||
|
|
@ -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"],
|
||||
|
|
|
|||
|
|
@ -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("\\\\")
|
||||
|
|
|
|||
|
|
@ -702,7 +702,8 @@ const setup: Plugin.Setup<typeof File> = (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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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<boolean> {
|
||||
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 }))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue