GeezerVison is showing github.com — fix: properly encode file URLs with special characters (#12424) · cyrilmhansen/opencode@fde0b39
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/app/src/components/file-tree.tsx +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,6 +19,14 @@ import { | 19 | 19 | import { Dynamic } from "solid-js/web" | 20 | 20 | import type { FileNode } from "@opencode-ai/sdk/v2" | 21 | 21 |
| | 22 | +function pathToFileUrl(filepath: string): string { | | 23 | + const encodedPath = filepath | | 24 | + .split("/") | | 25 | + .map((segment) => encodeURIComponent(segment)) | | 26 | + .join("/") | | 27 | + return `file://${encodedPath}` | | 28 | +} | | 29 | + | 22 | 30 | type Kind = "add" | "del" | "mix" | 23 | 31 |
| 24 | 32 | type Filter = { | @@ -247,7 +255,7 @@ export default function FileTree(props: { | 247 | 255 | onDragStart={(e: DragEvent) => { | 248 | 256 | if (!draggable()) return | 249 | 257 | e.dataTransfer?.setData("text/plain", `file:${local.node.path}`) | 250 | | - e.dataTransfer?.setData("text/uri-list", `file://${local.node.path}`) | | 258 | + e.dataTransfer?.setData("text/uri-list", pathToFileUrl(local.node.path)) | 251 | 259 | if (e.dataTransfer) e.dataTransfer.effectAllowed = "copy" | 252 | 260 |
| 253 | 261 | const dragImage = document.createElement("div") | |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/components/prompt-input/build-request-parts.ts +8-2Lines changed: 8 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,6 +30,12 @@ type BuildRequestPartsInput = { | 30 | 30 | const absolute = (directory: string, path: string) => | 31 | 31 | path.startsWith("/") ? path : (directory + "/" + path).replace("//", "/") | 32 | 32 |
| | 33 | +const encodeFilePath = (filepath: string): string => | | 34 | + filepath | | 35 | + .split("/") | | 36 | + .map((segment) => encodeURIComponent(segment)) | | 37 | + .join("/") | | 38 | + | 33 | 39 | const fileQuery = (selection: FileSelection | undefined) => | 34 | 40 | selection ? `?start=${selection.startLine}&end=${selection.endLine}` : "" | 35 | 41 |
| @@ -99,7 +105,7 @@ export function buildRequestParts(input: BuildRequestPartsInput) { | 99 | 105 | id: Identifier.ascending("part"), | 100 | 106 | type: "file", | 101 | 107 | mime: "text/plain", | 102 | | - url: `file://${path}${fileQuery(attachment.selection)}`, | | 108 | + url: `file://${encodeFilePath(path)}${fileQuery(attachment.selection)}`, | 103 | 109 | filename: getFilename(attachment.path), | 104 | 110 | source: { | 105 | 111 | type: "file", | @@ -129,7 +135,7 @@ export function buildRequestParts(input: BuildRequestPartsInput) { | 129 | 135 | const used = new Set(files.map((part) => part.url)) | 130 | 136 | const context = input.context.flatMap((item) => { | 131 | 137 | const path = absolute(input.sessionDirectory, item.path) | 132 | | - const url = `file://${path}${fileQuery(item.selection)}` | | 138 | + const url = `file://${encodeFilePath(path)}${fileQuery(item.selection)}` | 133 | 139 | const comment = item.comment?.trim() | 134 | 140 | if (!comment && used.has(url)) return [] | 135 | 141 | used.add(url) | |
Collapse file Copy file name to clipboardExpand all lines: packages/app/src/context/file/path.ts +17-2Lines changed: 17 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -72,12 +72,27 @@ export function unquoteGitPath(input: string) { | 72 | 72 | return new TextDecoder().decode(new Uint8Array(bytes)) | 73 | 73 | } | 74 | 74 |
| | 75 | +export function decodeFilePath(input: string) { | | 76 | + try { | | 77 | + return decodeURIComponent(input) | | 78 | + } catch { | | 79 | + return input | | 80 | + } | | 81 | +} | | 82 | + | | 83 | +export function encodeFilePath(filepath: string): string { | | 84 | + return filepath | | 85 | + .split("/") | | 86 | + .map((segment) => encodeURIComponent(segment)) | | 87 | + .join("/") | | 88 | +} | | 89 | + | 75 | 90 | export function createPathHelpers(scope: () => string) { | 76 | 91 | const normalize = (input: string) => { | 77 | 92 | const root = scope() | 78 | 93 | const prefix = root.endsWith("/") ? root : root + "/" | 79 | 94 |
| 80 | | - let path = unquoteGitPath(stripQueryAndHash(stripFileProtocol(input))) | | 95 | + let path = unquoteGitPath(decodeFilePath(stripQueryAndHash(stripFileProtocol(input)))) | 81 | 96 |
| 82 | 97 | if (path.startsWith(prefix)) { | 83 | 98 | path = path.slice(prefix.length) | @@ -100,7 +115,7 @@ export function createPathHelpers(scope: () => string) { | 100 | 115 |
| 101 | 116 | const tab = (input: string) => { | 102 | 117 | const path = normalize(input) | 103 | | - return `file://${path}` | | 118 | + return `file://${encodeFilePath(path)}` | 104 | 119 | } | 105 | 120 |
| 106 | 121 | const pathFromTab = (tabValue: string) => { | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/acp/agent.ts +6-4Lines changed: 6 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,6 +29,7 @@ import { | 29 | 29 | } from "@agentclientprotocol/sdk" | 30 | 30 |
| 31 | 31 | import { Log } from "../util/log" | | 32 | +import { pathToFileURL } from "bun" | 32 | 33 | import { ACPSessionManager } from "./session" | 33 | 34 | import type { ACPConfig } from "./types" | 34 | 35 | import { Provider } from "../provider/provider" | @@ -986,7 +987,7 @@ export namespace ACP { | 986 | 987 | type: "image", | 987 | 988 | mimeType: effectiveMime, | 988 | 989 | data: base64Data, | 989 | | - uri: `file://${filename}`, | | 990 | + uri: pathToFileURL(filename).href, | 990 | 991 | }, | 991 | 992 | }, | 992 | 993 | }) | @@ -996,13 +997,14 @@ export namespace ACP { | 996 | 997 | } else { | 997 | 998 | // Non-image: text types get decoded, binary types stay as blob | 998 | 999 | const isText = effectiveMime.startsWith("text/") || effectiveMime === "application/json" | | 1000 | + const fileUri = pathToFileURL(filename).href | 999 | 1001 | const resource = isText | 1000 | 1002 | ? { | 1001 | | - uri: `file://${filename}`, | | 1003 | + uri: fileUri, | 1002 | 1004 | mimeType: effectiveMime, | 1003 | 1005 | text: Buffer.from(base64Data, "base64").toString("utf-8"), | 1004 | 1006 | } | 1005 | | - : { uri: `file://${filename}`, mimeType: effectiveMime, blob: base64Data } | | 1007 | + : { uri: fileUri, mimeType: effectiveMime, blob: base64Data } | 1006 | 1008 |
| 1007 | 1009 | await this.connection | 1008 | 1010 | .sessionUpdate({ | @@ -1544,7 +1546,7 @@ export namespace ACP { | 1544 | 1546 | const name = path.split("/").pop() || path | 1545 | 1547 | return { | 1546 | 1548 | type: "file", | 1547 | | - url: `file://${path}`, | | 1549 | + url: pathToFileURL(path).href, | 1548 | 1550 | filename: name, | 1549 | 1551 | mime: "text/plain", | 1550 | 1552 | } | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/run.ts +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { Argv } from "yargs" | 2 | 2 | import path from "path" | | 3 | +import { pathToFileURL } from "bun" | 3 | 4 | import { UI } from "../ui" | 4 | 5 | import { cmd } from "./cmd" | 5 | 6 | import { Flag } from "../../flag/flag" | @@ -314,7 +315,7 @@ export const RunCommand = cmd({ | 314 | 315 |
| 315 | 316 | files.push({ | 316 | 317 | type: "file", | 317 | | - url: `file://${resolvedPath}`, | | 318 | + url: pathToFileURL(resolvedPath).href, | 318 | 319 | filename: path.basename(resolvedPath), | 319 | 320 | mime, | 320 | 321 | }) | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/tui/component/dialog-status.tsx +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import { TextAttributes } from "@opentui/core" | | 2 | +import { fileURLToPath } from "bun" | 2 | 3 | import { useTheme } from "../context/theme" | 3 | 4 | import { useDialog } from "@tui/ui/dialog" | 4 | 5 | import { useSync } from "@tui/context/sync" | @@ -19,7 +20,7 @@ export function DialogStatus() { | 19 | 20 | const list = sync.data.config.plugin ?? [] | 20 | 21 | const result = list.map((value) => { | 21 | 22 | if (value.startsWith("file://")) { | 22 | | - const path = value.substring("file://".length) | | 23 | + const path = fileURLToPath(value) | 23 | 24 | const parts = path.split("/") | 24 | 25 | const filename = parts.pop() || path | 25 | 26 | if (!filename.includes(".")) return { name: filename } | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { BoxRenderable, TextareaRenderable, KeyEvent, ScrollBoxRenderable } from "@opentui/core" | | 2 | +import { pathToFileURL } from "bun" | 2 | 3 | import fuzzysort from "fuzzysort" | 3 | 4 | import { firstBy } from "remeda" | 4 | 5 | import { createMemo, createResource, createEffect, onMount, onCleanup, Index, Show, createSignal } from "solid-js" | @@ -246,17 +247,17 @@ export function Autocomplete(props: { | 246 | 247 | const width = props.anchor().width - 4 | 247 | 248 | options.push( | 248 | 249 | ...sortedFiles.map((item): AutocompleteOption => { | 249 | | - let url = `file://${process.cwd()}/${item}` | | 250 | + const fullPath = `${process.cwd()}/${item}` | | 251 | + const urlObj = pathToFileURL(fullPath) | 250 | 252 | let filename = item | 251 | 253 | if (lineRange && !item.endsWith("/")) { | 252 | 254 | filename = `${item}#${lineRange.startLine}${lineRange.endLine ? `-${lineRange.endLine}` : ""}` | 253 | | - const urlObj = new URL(url) | 254 | 255 | urlObj.searchParams.set("start", String(lineRange.startLine)) | 255 | 256 | if (lineRange.endLine !== undefined) { | 256 | 257 | urlObj.searchParams.set("end", String(lineRange.endLine)) | 257 | 258 | } | 258 | | - url = urlObj.toString() | 259 | 259 | } | | 260 | + const url = urlObj.href | 260 | 261 |
| 261 | 262 | const isDir = item.endsWith("/") | 262 | 263 | return { | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/lsp/index.ts +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,7 +3,7 @@ import { Bus } from "@/bus" | 3 | 3 | import { Log } from "../util/log" | 4 | 4 | import { LSPClient } from "./client" | 5 | 5 | import path from "path" | 6 | | -import { pathToFileURL } from "url" | | 6 | +import { pathToFileURL, fileURLToPath } from "url" | 7 | 7 | import { LSPServer } from "./server" | 8 | 8 | import z from "zod" | 9 | 9 | import { Config } from "../config/config" | @@ -369,7 +369,7 @@ export namespace LSP { | 369 | 369 | } | 370 | 370 |
| 371 | 371 | export async function documentSymbol(uri: string) { | 372 | | - const file = new URL(uri).pathname | | 372 | + const file = fileURLToPath(uri) | 373 | 373 | return run(file, (client) => | 374 | 374 | client.connection | 375 | 375 | .sendRequest("textDocument/documentSymbol", { | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/prompt.ts +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,7 +32,7 @@ import { Flag } from "../flag/flag" | 32 | 32 | import { ulid } from "ulid" | 33 | 33 | import { spawn } from "child_process" | 34 | 34 | import { Command } from "../command" | 35 | | -import { $, fileURLToPath } from "bun" | | 35 | +import { $, fileURLToPath, pathToFileURL } from "bun" | 36 | 36 | import { ConfigMarkdown } from "../config/markdown" | 37 | 37 | import { SessionSummary } from "./summary" | 38 | 38 | import { NamedError } from "@opencode-ai/util/error" | @@ -210,7 +210,7 @@ export namespace SessionPrompt { | 210 | 210 | if (stats.isDirectory()) { | 211 | 211 | parts.push({ | 212 | 212 | type: "file", | 213 | | - url: `file://${filepath}`, | | 213 | + url: pathToFileURL(filepath).href, | 214 | 214 | filename: name, | 215 | 215 | mime: "application/x-directory", | 216 | 216 | }) | @@ -219,7 +219,7 @@ export namespace SessionPrompt { | 219 | 219 |
| 220 | 220 | parts.push({ | 221 | 221 | type: "file", | 222 | | - url: `file://${filepath}`, | | 222 | + url: pathToFileURL(filepath).href, | 223 | 223 | filename: name, | 224 | 224 | mime: "text/plain", | 225 | 225 | }) | |
Collapse file Copy file name to clipboard +56Lines changed: 56 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import path from "path" | | 2 | +import { describe, expect, test } from "bun:test" | | 3 | +import { fileURLToPath } from "url" | | 4 | +import { Instance } from "../../src/project/instance" | | 5 | +import { Log } from "../../src/util/log" | | 6 | +import { Session } from "../../src/session" | | 7 | +import { SessionPrompt } from "../../src/session/prompt" | | 8 | +import { MessageV2 } from "../../src/session/message-v2" | | 9 | +import { tmpdir } from "../fixture/fixture" | | 10 | + | | 11 | +Log.init({ print: false }) | | 12 | + | | 13 | +describe("session.prompt special characters", () => { | | 14 | + test("handles filenames with # character", async () => { | | 15 | + await using tmp = await tmpdir({ | | 16 | + git: true, | | 17 | + init: async (dir) => { | | 18 | + await Bun.write(path.join(dir, "file#name.txt"), "special content\n") | | 19 | + }, | | 20 | + }) | | 21 | + | | 22 | + await Instance.provide({ | | 23 | + directory: tmp.path, | | 24 | + fn: async () => { | | 25 | + const session = await Session.create({}) | | 26 | + const template = "Read @file#name.txt" | | 27 | + const parts = await SessionPrompt.resolvePromptParts(template) | | 28 | + const fileParts = parts.filter((part) => part.type === "file") | | 29 | + | | 30 | + expect(fileParts.length).toBe(1) | | 31 | + expect(fileParts[0].filename).toBe("file#name.txt") | | 32 | + | | 33 | + // Verify the URL is properly encoded (# should be %23) | | 34 | + expect(fileParts[0].url).toContain("%23") | | 35 | + | | 36 | + // Verify the URL can be correctly converted back to a file path | | 37 | + const decodedPath = fileURLToPath(fileParts[0].url) | | 38 | + expect(decodedPath).toBe(path.join(tmp.path, "file#name.txt")) | | 39 | + | | 40 | + const message = await SessionPrompt.prompt({ | | 41 | + sessionID: session.id, | | 42 | + parts, | | 43 | + noReply: true, | | 44 | + }) | | 45 | + const stored = await MessageV2.get({ sessionID: session.id, messageID: message.info.id }) | | 46 | + | | 47 | + // Verify the file content was read correctly | | 48 | + const textParts = stored.parts.filter((part) => part.type === "text") | | 49 | + const hasContent = textParts.some((part) => part.text.includes("special content")) | | 50 | + expect(hasContent).toBe(true) | | 51 | + | | 52 | + await Session.remove(session.id) | | 53 | + }, | | 54 | + }) | | 55 | + }) | | 56 | +}) |
|