Skip to content

Conversation

@whoiskatrin
Copy link
Collaborator

@whoiskatrin whoiskatrin commented Nov 11, 2025

No description provided.

@changeset-bot
Copy link

changeset-bot bot commented Nov 11, 2025

⚠️ No Changeset found

Latest commit: 9ef41fe

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@pkg-pr-new
Copy link

pkg-pr-new bot commented Nov 11, 2025

Open in StackBlitz

npm i https://pkg.pr.new/cloudflare/sandbox-sdk/@cloudflare/sandbox@204

commit: 9ef41fe

@github-actions
Copy link
Contributor

github-actions bot commented Nov 11, 2025

🐳 Docker Image Published

FROM cloudflare/sandbox:0.0.0-pr-204-6608c47

Version: 0.0.0-pr-204-6608c47

You can use this Docker image with the preview package from this PR.

@ghostwriternr
Copy link
Member

(Sharing some thoughts here about env and cwd as I go, before adding more specific feedback. Literally me "think out loud"-ing haha)

Related issues: #16, #144

Overall, I quite like the features this PR is adding and think this is an important one to clean up and get right. It's been fragile for a long time and I'm happy we're fixing this!

So with env, I generally want us to make sure there are 4 layers of env setting and all of them work cohesively:

  1. ENV variable when extending our published Dockerfile
  2. Using setEnvVars on the sandbox object returned by getSandbox
  3. Using setEnvVars on the session object returned by createSession
  4. Passing the env parameter on methods like exec, execStream, startProcess, etc.

And env is mainly critical for all the flows that involve the default session and the ones created explicitly using createSession, where we maintain persistent bash sessions. We inherit whatever is set using ENV on the Dockerfile via process.env when we create a shell session. And "intuitively", I think setEnvVars on the sandbox object (global) should possibly set the variables on all the sessions created explicitly, whereas setEnvVars on the session object (local) should only affect that particular bash session.

cwd is much easier to reason with as an option, and is definitely quite useful when working in the scope of either the default session or the manually created session, but sometimes developers just want to run a command elsewhere and don't want to do cd x, command_to_run then cd back_to_where_I_was, which can get annoying.

Copy link
Member

@ghostwriternr ghostwriternr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides these (commandWithEnv is the only blocking one though), I think we could do a couple of things more:

  1. Add setEnvVars to the ISandbox type. I just realised this is not there currently, but should be.
  2. In createSession, IF you think my earlier comment about setEnvVars behaviour on the global sandbox object vs the session object makes sense, we can merge this.envVars with the session-specific ones before creating a session from the session object.

Comment on lines +656 to +660
return `export ${key}='${escapedValue}'`;
})
.join('; ');
// Wrap in subshell to isolate env vars (they don't persist in session)
commandWithEnv = `(${exports}; ${command})`;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's probably a silent bug here, because if the command is a cd, it won't get persisted because we're running in a sub-shell. Probably a cleaner option is to remove the export bit, and change commandWithEnv to ${envPrefix} ${command};? This is a standard shell pattern to keep variables scoped to just that command.

} else {
// Regular execution with session
const response = await this.client.commands.execute(command, sessionId);
const response = await this.client.commands.execute(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick, okay to skip - since this is an internal method, we can probably make options an object instead of extending the number of params.

Comment on lines +393 to +397
processId: body.processId,
env: body.env,
cwd: body.cwd,
timeout: body.timeout,
autoCleanup: body.autoCleanup
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the rest of these fields actually being used properly? I would say we should not add them to the scope of this PR, but if not addressed, we should create a separate issue to track. #81 was created a while ago for the timeout.

@@ -0,0 +1,195 @@
import {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this test intended to be checked-in? Seems to be a mix of testing pre-change behaviour vs new

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants