Skip to content

Commit db5fbf4

Browse files
committed
fix(tui): resolve relative file links against the session directory
Address review feedback on #54838: resolve project-relative Markdown paths before opening, skip any modified click so the terminal can handle Cmd-click, and avoid toasting when a link cannot be opened.
1 parent bc0ddde commit db5fbf4

5 files changed

Lines changed: 29 additions & 8 deletions

File tree

‎packages/tui/src/app.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
TuiStartupProvider,
3838
TuiTerminalEnvironmentProvider,
3939
useTuiApp,
40+
useTuiPaths,
4041
useTuiStartup,
4142
useTuiTerminalEnvironment,
4243
type TuiApp,
@@ -84,7 +85,7 @@ import { Toast, ToastProvider, useToast } from "./ui/toast"
8485
import { isFallbackTitle } from "@opencode/util/session-title-fallback"
8586
import * as Model from "./util/model"
8687
import { ArgsProvider, useArgs, type Args } from "./context/args"
87-
import { openHref, openUrl } from "@opencode/util/open"
88+
import { openHref, openUrl, resolveOpenTarget } from "@opencode/util/open"
8889
import { PromptRefProvider, usePromptRef } from "./context/prompt"
8990
import { Config, ConfigProvider, useConfig } from "./config"
9091
import { newSessionLocation } from "./config/new-session-location"
@@ -453,6 +454,7 @@ export const run = Effect.fn("Tui.run")(function* (input: TuiInput) {
453454

454455
function App() {
455456
const app = useTuiApp()
457+
const paths = useTuiPaths()
456458
const startup = useTuiStartup()
457459
const config = useConfig()
458460
const devtools = createMemo(() => config.data.debug?.devtools ?? app.channel === "local")
@@ -477,6 +479,7 @@ function App() {
477479
const plugins = usePlugin()
478480
const clipboard = useClipboard()
479481
const terminalEnvironment = useTuiTerminalEnvironment()
482+
const linkBase = () => location.current?.directory || paths.cwd
480483
let paletteTimer: ReturnType<typeof setTimeout> | undefined
481484
const afterFrame = () => {
482485
// The native writer can still be flushing the frame when FRAME fires. Keep OSC probes behind visible app output.
@@ -1334,7 +1337,11 @@ function App() {
13341337
}}
13351338
onMouseUp={(event) => {
13361339
const url = clickedLink(renderer, event)
1337-
if (url) return void openHref(url).catch(toast.error)
1340+
if (url) {
1341+
const base = linkBase()
1342+
// Skip links we cannot open so the terminal/IDE can handle them (e.g. Cmd-click).
1343+
if (resolveOpenTarget(url, base)) return void openHref(url, base).catch(toast.error)
1344+
}
13381345
if (copyOnSelectEnabled()) Selection.copyOnSelectRelease(event, renderer, toast, clipboard)
13391346
}}
13401347
>

‎packages/tui/src/ui/link.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ export function Link(props: LinkProps) {
3737
* Returns the hyperlink under a plain left click. opentui marks the release that ends a selection gesture with
3838
* `isDragging`, and a fresh "cell" selection means the pointer never moved; other releases, such as the end of a drag
3939
* that started outside text, are not clicks. Each rendered cell stores its full URL, so a click on any row of a wrapped
40-
* link resolves the whole link. Modified clicks are left to the terminal's own hyperlink handling.
40+
* link resolves the whole link. Modified clicks (including Cmd/meta when the terminal reports it) are left to the
41+
* terminal's own hyperlink handling.
4142
*/
4243
export function clickedLink(renderer: CliRenderer, event: MouseEvent) {
43-
if (!event.isDragging || event.modifiers.ctrl || event.modifiers.alt || event.modifiers.shift) return
44+
if (!event.isDragging || Object.values(event.modifiers).some(Boolean)) return
4445
const selection = renderer.getSelection()
4546
if (!selection?.isStart || selection.behavior !== "cell") return
4647
return renderer.getLinkAt(event.x, event.y) ?? undefined

‎packages/tui/test/ui/link.test.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ test("plain clicks on any row of a wrapped Markdown link resolve the full URL",
4444
await app.mockMouse.click(0, head)
4545
await app.mockMouse.click(x, head, 0, { modifiers: { ctrl: true } })
4646
await app.mockMouse.click(x, head, 0, { modifiers: { shift: true } })
47+
await app.mockMouse.click(x, head, 0, { modifiers: { alt: true } })
48+
await app.mockMouse.click(x, head, 0, { modifiers: { meta: true } as never })
4749
await app.mockMouse.click(x, head, 2)
4850
await app.mockMouse.drag(0, head, x + 2, head)
4951
await app.mockMouse.drag(0, 0, x + 2, head)

‎packages/util/src/open.test.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,17 @@ describe("resolveOpenTarget", () => {
2929
expect(resolveOpenTarget(file)).toBe(file)
3030
})
3131

32-
test("rejects remote file URLs and unsafe schemes", () => {
32+
test("resolves relative paths against a session directory base", () => {
33+
const base = path.resolve("/repo")
34+
expect(resolveOpenTarget("plan/example.md", base)).toBe(path.join(base, "plan/example.md"))
35+
expect(resolveOpenTarget("./src/foo.ts", base)).toBe(path.join(base, "src/foo.ts"))
36+
expect(resolveOpenTarget("../sibling.md", base)).toBe(path.resolve(base, "../sibling.md"))
37+
})
38+
39+
test("rejects remote file URLs, anchors, and unsafe schemes", () => {
3340
expect(resolveOpenTarget("file://example.com/share/index.html")).toBeUndefined()
3441
expect(resolveOpenTarget("javascript:alert(1)")).toBeUndefined()
42+
expect(resolveOpenTarget("#section", "/repo")).toBeUndefined()
3543
expect(resolveOpenTarget("packages/tui/src/ui/link.tsx")).toBeUndefined()
3644
expect(resolveOpenTarget("not a url")).toBeUndefined()
3745
})

‎packages/util/src/open.ts‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ export function openPath(target: string) {
1414
}
1515

1616
/** Open an http(s) URL in the browser, or a local file path / file:// link with the OS handler. */
17-
export function openHref(input: string) {
18-
const target = resolveOpenTarget(input)
17+
export function openHref(input: string, base?: string) {
18+
const target = resolveOpenTarget(input, base)
1919
if (!target) return Promise.reject(new Error(`Only http, https, and local file links can be opened: ${input}`))
2020
return open(target)
2121
}
2222

2323
/** @internal */
24-
export function resolveOpenTarget(input: string) {
24+
export function resolveOpenTarget(input: string, base?: string) {
2525
if (URL.canParse(input)) {
2626
const url = new URL(input)
2727
if (url.protocol === "http:" || url.protocol === "https:") return url.href
@@ -33,6 +33,9 @@ export function resolveOpenTarget(input: string) {
3333
return undefined
3434
}
3535
}
36+
return undefined
3637
}
3738
if (path.isAbsolute(input)) return input
39+
// Resolve project-relative Markdown links such as plan/example.md against the session directory.
40+
if (base !== undefined && input.length > 0 && !input.startsWith("#")) return path.resolve(base, input)
3841
}

0 commit comments

Comments
 (0)