Skip to content

fix(shellenv): only export env vars Devbox adds or changes#2894

Open
mikeland73 wants to merge 1 commit into
mainfrom
claude/focused-goldberg-3l9m20
Open

fix(shellenv): only export env vars Devbox adds or changes#2894
mikeland73 wants to merge 1 commit into
mainfrom
claude/focused-goldberg-3l9m20

Conversation

@mikeland73

Copy link
Copy Markdown
Collaborator

Summary

Fixes #2826.

devbox shellenv and devbox global shellenv export the entire environment, including variables that Devbox never touches such as HOSTNAME and LANG. On distributions that mark some variables read-only (e.g. openSUSE marks PROFILEREAD read-only), eval "$(devbox global shellenv)" then fails with errors like:

(eval):55: read-only variable: PROFILEREAD

Re-exporting a variable to the value it already has is a no-op at best, and at worst breaks the user's prompt/shell on every command.

Fix

EnvExports now restricts the exported variables to those Devbox actually adds or changes relative to the current shell environment. Variables whose value already matches the ambient environment are dropped, since they are already present in the shell — so there is no reason to re-emit them (and no reason to trip over read-only ones).

  • internal/devbox/envvars.go: add onlyModifiedEnvVars(env, ambient) helper that returns only new/changed variables.
  • internal/devbox/devbox.go: in EnvExports, filter the computed env against os.Environ() when the new option is set. Skipped in pure mode, where the intent is to emit a complete, self-contained environment rather than a diff against the current shell.
  • internal/devbox/devopt/devboxopts.go: add OnlyModifiedEnv to EnvExportsOpts.
  • internal/boxcli/shellenv.go: enable OnlyModifiedEnv for the shellenv command (the reported path). The devbox shell --print-env / direnv path is intentionally left unchanged.

Variables Devbox relies on (PATH, the DEVBOX_* helpers, the shellenv hash, plugin/config env, etc.) are always new or changed, so they continue to be exported. Re-running shellenv inside an already-active environment now emits only what differs, which is idempotent.

Testing

  • Added TestOnlyModifiedEnvVars in internal/devbox/envvars_test.go covering unchanged (dropped), changed (kept), and new (kept) variables, including a read-only-style PROFILEREAD case.
  • go test ./internal/devbox/ -run 'TestOnlyModifiedEnvVars|TestExportify' passes.
  • go build ./internal/..., go vet, and gofmt are clean.

(The remaining failures in ./internal/devbox/ unit tests are pre-existing and environmental — they require the nix binary, which isn't available in CI sandbox; they are unrelated to this change.)

cc @gasuketsu (issue reporter)


🤖 Generated with Claude Code

https://claude.ai/code/session_01E6tBNHN6DFv5VabVTAosNq


Generated by Claude Code

`devbox shellenv` and `devbox global shellenv` exported the entire
environment, including unrelated variables such as HOSTNAME and LANG.
On distributions that mark some variables read-only (e.g. PROFILEREAD on
openSUSE), `eval "$(devbox global shellenv)"` then fails with errors like:

    (eval):55: read-only variable: PROFILEREAD

Restrict the exported variables to those Devbox actually adds or changes
relative to the current shell. Unchanged variables are already present in
the shell, so re-exporting them is redundant and only risks these errors.

The filtering is applied to the `devbox shellenv` command path and skipped
in pure mode (where a complete, self-contained environment is intended).

Fixes #2826

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E6tBNHN6DFv5VabVTAosNq
Copilot AI review requested due to automatic review settings June 26, 2026 14:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Why devbox shellenv and devbox global shellenv export all existing envvars ?

3 participants