Skip to content

Commit 8a93d0c

Browse files
Change default directory to a clean /workspace (#47)
* Change default directory to a clean /workspace - Move container server code from /app to /container-server - Default user commands to run in clean /workspace directory - Update documentation examples to use /workspace paths - Update UI placeholders to suggest /workspace paths - Create /workspace directory in container for user operations This gives developers a clean workspace without server implementation files cluttering their view. * Fix streaming exec to support cwd and env options The streaming execution handler was missing support for cwd and env options that the regular exec handler supports, causing inconsistent behavior. Now both handlers support the same options and default to /workspace when no cwd is specified. * Add changeset * Simplify comments
1 parent 1ab3f9d commit 8a93d0c

File tree

6 files changed

+23
-14
lines changed

6 files changed

+23
-14
lines changed

.changeset/weak-otters-bake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/sandbox": minor
3+
---
4+
5+
Change default directory to a clean /workspace

examples/basic/app/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,7 @@ function FilesTab({
14261426
<div className="input-group">
14271427
<input
14281428
type="text"
1429-
placeholder="File path (e.g., /app/package.json)"
1429+
placeholder="File path (e.g., /workspace/package.json)"
14301430
value={selectedFile || ""}
14311431
onChange={(e) => setSelectedFile(e.target.value)}
14321432
className="file-input"
@@ -1455,7 +1455,7 @@ function FilesTab({
14551455
<div className="input-group">
14561456
<input
14571457
type="text"
1458-
placeholder="File path (e.g., /app/hello.txt)"
1458+
placeholder="File path (e.g., /workspace/hello.txt)"
14591459
value={newFileName}
14601460
onChange={(e) => setNewFileName(e.target.value)}
14611461
className="file-input"
@@ -1485,7 +1485,7 @@ function FilesTab({
14851485
<div className="input-group">
14861486
<input
14871487
type="text"
1488-
placeholder="Directory path (e.g., /app/src)"
1488+
placeholder="Directory path (e.g., /workspace/src)"
14891489
value={newDirName}
14901490
onChange={(e) => setNewDirName(e.target.value)}
14911491
className="file-input"

packages/sandbox/Dockerfile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,23 @@ COPY --from=bun-source /usr/local/bin/bun /usr/local/bin/bun
5656
COPY --from=bun-source /usr/local/bin/bunx /usr/local/bin/bunx
5757

5858

59-
# Set up working directory
60-
WORKDIR /app
59+
# Set up container server directory
60+
WORKDIR /container-server
6161

6262
# Verify installations
6363
RUN python3 --version && \
6464
node --version && \
6565
npm --version && \
6666
bun --version
67-
6867

69-
# Copy container source files
68+
# Copy container source files to server directory
7069
COPY container_src/ ./
7170

71+
# Create clean workspace directory for users
72+
RUN mkdir -p /workspace
73+
7274
# Expose the application port
7375
EXPOSE 3000
7476

75-
# Run the application
77+
# Run the application from container server directory
7678
CMD ["bun", "index.ts"]

packages/sandbox/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ for await (const log of parseSSEStream<LogEvent>(logStream)) {
176176
Write content to a file.
177177

178178
```typescript
179-
await sandbox.writeFile("/app.js", "console.log('Hello!');");
179+
await sandbox.writeFile("/workspace/app.js", "console.log('Hello!');");
180180
```
181181

182182
#### `readFile(path, options?)`
@@ -314,7 +314,7 @@ const sandbox = getSandbox(env.Sandbox, "node-app");
314314

315315
// Write a simple Express server
316316
await sandbox.writeFile(
317-
"/app.js",
317+
"/workspace/app.js",
318318
`
319319
const express = require('express');
320320
const app = express();
@@ -498,7 +498,7 @@ Maintain context across commands:
498498
const sessionId = crypto.randomUUID();
499499

500500
// Commands in the same session share working directory
501-
await sandbox.exec("cd /app", { sessionId });
501+
await sandbox.exec("cd /workspace", { sessionId });
502502
await sandbox.exec("npm install", { sessionId });
503503
const app = await sandbox.startProcess("npm start", { sessionId });
504504
```

packages/sandbox/container_src/handler/exec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function executeCommand(
1616
shell: true,
1717
stdio: ["pipe", "pipe", "pipe"] as const,
1818
detached: options.background || false,
19-
cwd: options.cwd,
19+
cwd: options.cwd || "/workspace", // Default to clean /workspace directory
2020
env: options.env ? { ...process.env, ...options.env } : process.env
2121
};
2222

@@ -159,7 +159,7 @@ export async function handleStreamingExecuteRequest(
159159
): Promise<Response> {
160160
try {
161161
const body = (await req.json()) as ExecuteRequest;
162-
const { command, sessionId, background } = body;
162+
const { command, sessionId, background, cwd, env } = body;
163163

164164
if (!command || typeof command !== "string") {
165165
return new Response(
@@ -186,6 +186,8 @@ export async function handleStreamingExecuteRequest(
186186
shell: true,
187187
stdio: ["pipe", "pipe", "pipe"] as const,
188188
detached: background || false,
189+
cwd: cwd || "/workspace", // Default to clean /workspace directory
190+
env: env ? { ...process.env, ...env } : process.env
189191
};
190192

191193
const child = spawn(command, spawnOptions);

packages/sandbox/container_src/handler/process.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export async function handleStartProcessRequest(
7272
// Start the actual process
7373
try {
7474
const spawnOptions: SpawnOptions = {
75-
cwd: options.cwd || process.cwd(),
75+
cwd: options.cwd || "/workspace", // Default to /workspace for consistency with exec commands
7676
env: { ...process.env, ...options.env },
7777
detached: false,
7878
shell: true,

0 commit comments

Comments
 (0)