From 77ebbad521019ec220d5f0b196d44aa79feb622b Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 01:16:41 +0000 Subject: [PATCH] Resolve relative runtime cwd paths from launch directory Co-authored-by: Theo Browne --- apps/t3/src/runtimeApiServer.test.ts | 73 ++++++++++++++++++++++++++-- apps/t3/src/runtimeApiServer.ts | 41 +++++++++++----- 2 files changed, 99 insertions(+), 15 deletions(-) diff --git a/apps/t3/src/runtimeApiServer.test.ts b/apps/t3/src/runtimeApiServer.test.ts index ef737d3225..d9b08720f4 100644 --- a/apps/t3/src/runtimeApiServer.test.ts +++ b/apps/t3/src/runtimeApiServer.test.ts @@ -1,4 +1,4 @@ -import { mkdtempSync, writeFileSync } from "node:fs"; +import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { createServer as createNetServer } from "node:net"; @@ -215,7 +215,7 @@ describe("runtimeApiServer", () => { port: 0, launchCwd: missingPath, }), - ).rejects.toThrow("Invalid launchCwd: directory does not exist"); + ).rejects.toThrow("Invalid launchCwd does not exist"); }); it("rejects startup when launchCwd is not a directory", async () => { @@ -227,7 +227,7 @@ describe("runtimeApiServer", () => { port: 0, launchCwd: filePath, }), - ).rejects.toThrow("Invalid launchCwd: expected directory path"); + ).rejects.toThrow("Invalid launchCwd is not a directory"); }); it("rejects non-positive bootstrap timeout configuration", async () => { @@ -480,6 +480,36 @@ describe("runtimeApiServer", () => { client.socket.close(); }); + it("resolves relative shell.openInEditor cwd paths from launch cwd", async () => { + const launchCwd = mkdtempSync(path.join(os.tmpdir(), "t3-shell-launch-cwd-")); + const nestedDir = path.join(launchCwd, "nested"); + mkdirSync(nestedDir, { recursive: true }); + + const server = await startRuntimeApiServer({ + port: 0, + launchCwd, + }); + servers.push(server); + + const client = await connectClient(server.wsUrl); + await client.nextMessage(); + + const response = await sendRequest( + client.socket, + client.nextMessage, + "shell-open-relative-launch-cwd-1", + "shell.openInEditor", + { + cwd: "nested", + editor: "file-manager", + }, + ); + expect(response.ok).toBe(true); + expect(response.result).toBeNull(); + + client.socket.close(); + }); + it("responds to dialogs.pickFolder requests", async () => { const server = await startRuntimeApiServer({ port: 0, @@ -1472,6 +1502,43 @@ describe("runtimeApiServer", () => { client.socket.close(); }); + it("resolves relative terminal cwd paths from launch cwd", async () => { + const launchCwd = mkdtempSync(path.join(os.tmpdir(), "t3-terminal-launch-cwd-")); + const nestedDir = path.join(launchCwd, "nested"); + mkdirSync(nestedDir, { recursive: true }); + + const server = await startRuntimeApiServer({ + port: 0, + launchCwd, + }); + servers.push(server); + + const client = await connectClient(server.wsUrl); + await client.nextMessage(); + + const response = await sendRequest( + client.socket, + client.nextMessage, + "terminal-cwd-relative-launch-1", + "terminal.run", + { + command: "pwd", + cwd: "nested", + }, + ); + expect(response.ok).toBe(true); + if (!response.ok) { + throw new Error("Expected relative launch cwd terminal response to succeed."); + } + + const payload = response.result as { + stdout: string; + }; + expect(payload.stdout.trim()).toBe(nestedDir); + + client.socket.close(); + }); + it("returns structured errors when terminal cwd does not exist", async () => { const server = await startRuntimeApiServer({ port: 0, diff --git a/apps/t3/src/runtimeApiServer.ts b/apps/t3/src/runtimeApiServer.ts index 828a0cb7f0..b0087f491f 100644 --- a/apps/t3/src/runtimeApiServer.ts +++ b/apps/t3/src/runtimeApiServer.ts @@ -255,7 +255,11 @@ async function runTerminalCommand( defaultCwd: string, ) { const providedCwd = parsed.cwd ?? defaultCwd; - const resolvedCwd = resolveExistingDirectory(providedCwd, "Working directory"); + const resolvedCwd = resolveExistingDirectoryFromBase( + providedCwd, + "Working directory", + defaultCwd, + ); const shellPath = process.platform === "win32" @@ -331,22 +335,35 @@ function resolveExistingDirectory(targetPath: string, label: string): string { return candidate; } +function resolveExistingDirectoryFromBase( + targetPath: string, + label: string, + baseDirectory: string, +): string { + const candidate = path.isAbsolute(targetPath) + ? path.resolve(targetPath) + : path.resolve(baseDirectory, targetPath); + let stats: fs.Stats; + try { + stats = fs.statSync(candidate); + } catch { + throw new Error(`${label} does not exist: ${candidate}`); + } + + if (!stats.isDirectory()) { + throw new Error(`${label} is not a directory: ${candidate}`); + } + + return candidate; +} + export async function startRuntimeApiServer( options: RuntimeApiServerOptions, ): Promise { if (!Number.isInteger(options.port) || options.port < 0 || options.port > 65_535) { throw new Error("Invalid runtime port: expected integer between 0 and 65535."); } - const launchCwd = path.resolve(options.launchCwd); - let launchCwdStats: fs.Stats; - try { - launchCwdStats = fs.statSync(launchCwd); - } catch { - throw new Error(`Invalid launchCwd: directory does not exist: ${launchCwd}`); - } - if (!launchCwdStats.isDirectory()) { - throw new Error(`Invalid launchCwd: expected directory path: ${launchCwd}`); - } + const launchCwd = resolveExistingDirectory(options.launchCwd, "Invalid launchCwd"); const authToken = options.authToken?.trim(); if (options.authToken !== undefined && !authToken) { throw new Error("Invalid runtime auth token: expected non-empty string."); @@ -582,7 +599,7 @@ export async function startRuntimeApiServer( if (method === "shell.openInEditor") { const parsed = shellOpenInEditorInputSchema.parse(params); - const targetPath = resolveExistingDirectory(parsed.cwd, "Editor target"); + const targetPath = resolveExistingDirectoryFromBase(parsed.cwd, "Editor target", launchCwd); const editor = EDITORS.find((entry) => entry.id === parsed.editor); if (!editor) {