@@ -4,7 +4,11 @@ import {
44 EnvironmentVariablesSchema ,
55} from "@cheatcode/sandbox-contracts" ;
66import { z } from "zod" ;
7- import { resolveProjectWorkspacePath , WorkspacePathSchema } from "./workspace-paths" ;
7+ import {
8+ remapProjectWorkspaceReferences ,
9+ resolveProjectWorkspacePath ,
10+ WorkspacePathSchema ,
11+ } from "./workspace-paths" ;
812
913export const ShellExecInputSchema = z . strictObject ( {
1014 command : z
@@ -97,9 +101,13 @@ export async function executeShellExec(
97101 runtimeContext : CodeRuntimeContextFor < "exec" > ,
98102) : Promise < ShellExecOutput > {
99103 const parsedInput = ShellExecInputSchema . parse ( input ) ;
104+ const workspaceDir = runtimeContext . workspaceDir ;
100105 const result = await runtimeContext . sandbox . exec ( {
101- command : parsedInput . command ,
102- cwd : resolveProjectWorkspacePath ( parsedInput . cwd , runtimeContext . workspaceDir ) ,
106+ command : remapCommandWorkspaceReferences ( parsedInput . command , workspaceDir ) ,
107+ cwd :
108+ workspaceDir || parsedInput . cwd
109+ ? resolveProjectWorkspacePath ( parsedInput . cwd , workspaceDir )
110+ : "/tmp" ,
103111 ...( parsedInput . env ? { env : parsedInput . env } : { } ) ,
104112 ...( parsedInput . timeoutMs ? { timeoutMs : parsedInput . timeoutMs } : { } ) ,
105113 } ) ;
@@ -130,7 +138,7 @@ export async function executeShellStartProcess(
130138 : undefined ;
131139 return ShellProcessOutputSchema . parse (
132140 await runtimeContext . sandbox . startProcess ( {
133- command : parsedInput . command ,
141+ command : remapCommandWorkspaceReferences ( parsedInput . command , runtimeContext . workspaceDir ) ,
134142 cwd : resolveProjectWorkspacePath ( parsedInput . cwd , runtimeContext . workspaceDir ) ,
135143 ...( parsedInput . env ? { env : parsedInput . env } : { } ) ,
136144 keepAliveTimeoutMs : parsedInput . keepAliveTimeoutMs ,
@@ -162,9 +170,20 @@ export async function executeShellTerminal(
162170 const parsedInput = ShellTerminalInputSchema . parse ( input ) ;
163171 return ShellExecOutputSchema . parse (
164172 await runtimeContext . sandbox . exec ( {
165- command : [ "sh" , "-lc" , parsedInput . command ] ,
173+ command : [
174+ "sh" ,
175+ "-lc" ,
176+ remapProjectWorkspaceReferences ( parsedInput . command , runtimeContext . workspaceDir ) ,
177+ ] ,
166178 cwd : resolveProjectWorkspacePath ( parsedInput . cwd , runtimeContext . workspaceDir ) ,
167179 timeoutMs : parsedInput . timeoutMs ,
168180 } ) ,
169181 ) ;
170182}
183+
184+ function remapCommandWorkspaceReferences (
185+ command : readonly string [ ] ,
186+ workspaceDir : string | undefined ,
187+ ) : string [ ] {
188+ return command . map ( ( argument ) => remapProjectWorkspaceReferences ( argument , workspaceDir ) ) ;
189+ }
0 commit comments