diff --git a/apps/web/src/components/ChatMarkdown.browser.test.tsx b/apps/web/src/components/ChatMarkdown.browser.test.tsx index faca71b4a101f..b0aa293812d6d 100644 --- a/apps/web/src/components/ChatMarkdown.browser.test.tsx +++ b/apps/web/src/components/ChatMarkdown.browser.test.tsx @@ -90,6 +90,17 @@ vi.mock(import("../state/filesystem"), async (original) => { : actual.useFilesystemReadAccess(environmentId)) as never, }; }); +const threadWorktrees = vi.hoisted(() => new Map()); +vi.mock(import("../state/entities"), async (original) => { + const actual = await original(); + return { + ...actual, + readThreadShell: ((ref: { threadId: string }) => { + const worktreePath = threadWorktrees.get(ref.threadId); + return worktreePath === undefined ? actual.readThreadShell(ref as never) : { worktreePath }; + }) as never, + }; +}); vi.mock(import("../remoteOpen"), async (original) => ({ ...(await original()), useRemoteOpenResolution: () => ({ state: { mode: "local-exec" }, isResolved: true }) as never, @@ -170,6 +181,7 @@ function setup() { parsedSources.length = 0; settingsOverrides.mathRenderingEnabled = false; renderDiagram.mockReset(); + threadWorktrees.clear(); workspaceSearch.mockReset().mockResolvedValue(AsyncResult.success({ entries: [] })); filesystemMetadata.mockReset().mockResolvedValue(AsyncResult.success({ entries: [null] })); } @@ -559,6 +571,27 @@ describe("ChatMarkdown workspace file opens", () => { expect(workspaceSearch).toHaveBeenCalledOnce(); }); + it("opens a path from markdown rendered in another checkout under that checkout", async () => { + setup(); + const scratchThread = { ...threadRef, threadId: ThreadId.make("scratch-thread") }; + threadWorktrees.set(scratchThread.threadId, "/scratch/thread"); + filesystemMetadata.mockResolvedValue(AsyncResult.success({ entries: [{ kind: "file" }] })); + using openFile = vi.spyOn(useRightPanelStore.getState(), "openFile"); + await using screen = await renderText("[State](packages/state/filesystem.ts)", { + cwd: "/repo-a", + threadRef: scratchThread, + }); + await act(async () => screen.container.querySelector("a")!.click()); + + await vi.waitFor(() => + expect(openFile).toHaveBeenCalledExactlyOnceWith( + scratchThread, + "/repo-a/packages/state/filesystem.ts", + undefined, + ), + ); + }); + it.each([ ["its own panel", "thread-own-panel", false], ["its parent's panel from a side chat", "thread-side-chat", true], diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index 2edc4be1febcd..f5f69e063a967 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -942,8 +942,24 @@ function useChatMarkdownState( }; const openFileInPanel = (fileLinkMeta: MarkdownFileLinkMeta, panelPath: string) => { if (!threadRef) return; + // The panel reads relative paths from the thread's folder. A pull request body's `cwd` is the + // checkout of its repository, which can be another folder. + const shell = readThreadShell(threadRef); + const panelRoot = + shell?.worktreePath ?? + projects.find( + (project) => project.environmentId === environmentId && project.id === shell?.projectId, + )?.workspaceRoot; const openAt = (path: string) => - useRightPanelStore.getState().openFile(threadRef, path, fileLinkMeta.line); + useRightPanelStore + .getState() + .openFile( + threadRef, + cwd && panelRoot !== undefined && cwd !== panelRoot + ? resolveWorkspaceFilePath(path, cwd) + : path, + fileLinkMeta.line, + ); if ( fileLinkMeta.workspaceLookupPath === null || !cwd ||