Skip to content

Per-project environment variables for runs, dev servers and terminals (#188) - #1578

Merged
currentbits merged 1 commit into
mainfrom
coder/project-env-188
Oct 10, 2026
Merged

currentbits merged 1 commit into
mainfrom
coder/project-env-188

Conversation

@currentbits

Copy link
Copy Markdown
Owner

Closes #188.

What

Projects get an env map, edited in Edit Project → Environment variables (one KEY=value per line, # comments and blank lines skipped). It is applied to:

  • agent runs, every provider (claude/grok, codex, cursor, kimi, muse, opencode, generic), local and across ssh/WSL
  • workflow phase agents
  • the thread terminal
  • verify, setup and quick-action commands (all go through runVerifyCommand)
  • dev servers (already read project.env via spawnEnvForDevServer; now there's a way to set it)

How

  • worktreeEnv.withProjectEnv(project, overlay) layers project env under a spawn's own overlay. Remote: injected once in resolveSpawn / resolveWorkflowSpawn, forwarded as quoted env K=V. Local: one wrap per runner-provider-*.js / workflow-agents-*.js.
  • Full-env sites (generic provider, terminal, verify) spread project env over the inherited env.
  • Precedence: inherited env < project env < provider overlay (CODEX_HOME, KIMI_CODE_HOME, …) < thread secrets.
  • Persisted via services-projects.updateProject (null/{} clears) and normalized in store-migrate (PATH and invalid names dropped, existing normalizeProjectEnv).
  • Claude's warm-session reuse key already includes spawnEnv, so an edit respawns the kept-alive CLI next turn.

Known limits

  • Only PATH is blocked; other keys pass through.
  • Values are stored in plain text in the store (the field note says so).
  • Verify/terminal on WSL don't forward env to the distro (no WSLENV); verify doesn't wrap ssh at all today.

Tests

  • electron/test/project-env.test.js: merge precedence, ssh forwarding, persist/reload/clear, a real generic run seeing the env, verify with an overlapping inherited key.
  • test/editProject.test.tsx: prefill, bad-line rejection, saved payload.
  • Local: test:electron 4481 pass / 0 fail (CI-like env), typecheck, vite build, edit-project renderer tests green.

🤖 Generated with Claude Code

…#188)

Projects get an `env` map (Edit Project → Environment variables, one
KEY=value per line). It is persisted and normalized on load (PATH and
invalid names dropped), and layered into:

- agent runs for every provider, locally and across ssh/WSL (forwarded
  as quoted `env K=V` through resolveSpawn / resolveWorkflowSpawn)
- workflow phase agents
- the thread terminal
- verify, setup and quick-action commands
- dev servers (already read project.env)

Precedence: inherited env < project env < provider overlay
(CODEX_HOME, KIMI_CODE_HOME, ...) < thread secrets.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-10T08:22:03.055192Z 380fefa PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@currentbits
currentbits merged commit 6d28a02 into main Oct 10, 2026
6 checks passed

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 380fefaff5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +117 to +120
envExtra: withProjectEnv(
opts.project,
entry && entry.id === "grok" ? mergeGrokSpawnEnv(wrapEnv) : undefined,
),

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Route Claude workflow phases through the boundary wrapper

For an SSH or WSL project whose workflow phase uses Claude, the project variables added here only enter the locally spawned process: unlike every other workflow provider, the spawn immediately above calls resolveWorkflowSpawn only for Grok. Consequently the remote Claude process never receives these variables and the phase may run against the local checkout instead; route Claude through the boundary wrapper as well.

Useful? React with 👍 / 👎.

if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) {
return `Invalid environment line: ${line}`;
}
if (key === "PATH") return "PATH cannot be set per project.";

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Reject PATH case-insensitively on Windows

On Windows, environment variable names are case-insensitive, so entering Path=... bypasses this exact-case check and is also accepted by normalizeProjectEnv. On systems where the inherited key is Path, the project value then overwrites the executable search path for agents, terminals, and commands, despite the UI claiming PATH is prohibited; compare the key case-insensitively.

Useful? React with 👍 / 👎.

export function parseEnvText(text: string): Record<string, string> | string {
const env: Record<string, string> = {};
for (const raw of text.split("\n")) {
const line = raw.trim();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve whitespace in environment values

When a value intentionally begins or ends with whitespace, trimming the entire line before splitting silently changes it—for example, TOKEN=abc is saved as TOKEN=abc. Because every project edit submits the parsed environment map, an existing whitespace-bearing value is also corrupted when the user saves an unrelated setting; trim only the key/formatting rather than the value.

Useful? React with 👍 / 👎.

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.

Per-project environment variables injected into agent runs and dev servers

1 participant