From 8a1ad4e763f47c6b40af0c2afb72f61ed613ba1c Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Tue, 10 Feb 2026 01:12:50 +0000 Subject: [PATCH] Reuse shared directory validation in runtime methods Co-authored-by: Theo Browne --- apps/t3/src/runtimeApiServer.ts | 43 ++++++++++++++------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/apps/t3/src/runtimeApiServer.ts b/apps/t3/src/runtimeApiServer.ts index 8f18763d5b..828a0cb7f0 100644 --- a/apps/t3/src/runtimeApiServer.ts +++ b/apps/t3/src/runtimeApiServer.ts @@ -255,21 +255,7 @@ async function runTerminalCommand( defaultCwd: string, ) { const providedCwd = parsed.cwd ?? defaultCwd; - const resolvedCwd = (() => { - const candidate = path.resolve(providedCwd); - let stats: fs.Stats; - try { - stats = fs.statSync(candidate); - } catch { - throw new Error(`Working directory does not exist: ${candidate}`); - } - - if (!stats.isDirectory()) { - throw new Error(`Working directory is not a directory: ${candidate}`); - } - - return candidate; - })(); + const resolvedCwd = resolveExistingDirectory(providedCwd, "Working directory"); const shellPath = process.platform === "win32" @@ -329,6 +315,22 @@ async function runTerminalCommand( }); } +function resolveExistingDirectory(targetPath: string, label: string): string { + const candidate = path.resolve(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 { @@ -580,16 +582,7 @@ export async function startRuntimeApiServer( if (method === "shell.openInEditor") { const parsed = shellOpenInEditorInputSchema.parse(params); - const targetPath = path.resolve(parsed.cwd); - let targetStats: fs.Stats; - try { - targetStats = fs.statSync(targetPath); - } catch { - throw new Error(`Editor target does not exist: ${targetPath}`); - } - if (!targetStats.isDirectory()) { - throw new Error(`Editor target is not a directory: ${targetPath}`); - } + const targetPath = resolveExistingDirectory(parsed.cwd, "Editor target"); const editor = EDITORS.find((entry) => entry.id === parsed.editor); if (!editor) {