fix(shellenv): only export env vars Devbox actually modifies#2902
Open
mikeland73 wants to merge 1 commit into
Open
fix(shellenv): only export env vars Devbox actually modifies#2902mikeland73 wants to merge 1 commit into
mikeland73 wants to merge 1 commit into
Conversation
`devbox shellenv` and `devbox global shellenv` emitted an `export` statement for every variable in the computed environment, including inherited variables that Devbox never changed (e.g. HOSTNAME, LANG, PROFILEREAD). Re-exporting an unchanged variable is redundant, and it breaks in shells that mark some inherited variables as read-only: on openSUSE, `eval "$(devbox global shellenv)"` failed on every prompt with `read-only variable: PROFILEREAD`. Filter the computed environment down to the variables whose value differs from the current environment (or that are new) before formatting the export statements. The output is meant to be eval'd in the caller's shell, so skipping variables that already have the same value is a no-op in the normal case and avoids the read-only error. The interactive `devbox shell` code path (which sources a generated shellrc in a child shell) is unchanged; only `EnvExports`, used by `devbox shellenv` and `devbox shell --print-env`, is affected. Fixes #2826 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WiTxPixVaWVbpBsZf1DLeG
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates devbox shellenv/devbox global shellenv to emit export statements only for environment variables that Devbox actually changes relative to the current environment, avoiding redundant re-exports that can fail on shells with read-only inherited variables (e.g. PROFILEREAD on openSUSE).
Changes:
- Added
onlyModifiedEnvs(envs, baseEnv)helper to filter env maps down to new/changed variables. - Updated
Devbox.EnvExportsto filter computed envs againstos.Environ()before formatting bash/nushell output. - Added unit tests for
onlyModifiedEnvscovering unchanged, changed/new, empty-value, and empty-base behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/devbox/envvars.go | Adds onlyModifiedEnvs helper to filter environment variables by diff vs. a base env map. |
| internal/devbox/envvars_test.go | Introduces focused unit tests validating the filtering behavior. |
| internal/devbox/devbox.go | Applies the filtering in EnvExports so shellenv output avoids re-exporting unchanged inherited vars. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #2826.
devbox shellenvanddevbox global shellenvemit anexportstatement for every variable in the computed environment — including inherited variables that Devbox never touched, such asHOSTNAME,LANG, andPROFILEREAD. Re-exporting an unchanged variable is redundant, and it breaks in shells that mark some inherited variables as read-only. On openSUSE,eval "$(devbox global shellenv)"failed on every prompt with:Root cause
Devbox.computeEnv(internal/devbox/devbox.go) builds the environment by copying the entire parent environment (os.Environ()) and then layering Nix- and Devbox-specific variables on top.EnvExportsthen runsexportifyover that whole map, so unchanged variables likePROFILEREADare re-emitted asexport PROFILEREAD="true";. When that output iseval'd in a shell that has markedPROFILEREADread-only, the assignment errors out.Fix
Filter the computed environment down to just the variables whose value differs from the current environment (or that are new) before formatting the export statements:
onlyModifiedEnvs(envs, baseEnv)ininternal/devbox/envvars.goreturns only entries that are absent from, or changed relative to,baseEnv.EnvExportsnow callsonlyModifiedEnvs(envs, envir.PairsToMap(os.Environ()))before choosing the bash/nushell export format.The output of
EnvExportsis meant to beeval'd in the caller's shell (devbox shellenv, anddevbox shell --print-envused by direnv), so dropping variables that already hold the same value is a no-op in the normal case and avoids the read-only error. Variables Devbox actually changes —PATH,XDG_DATA_DIRS,DEVBOX_*, Nix and plugin/config variables, the shellenv hash — all differ from the parent value and are still exported.The interactive
devbox shellpath (which sources a generated shellrc in a child shell viashell.go) is intentionally unchanged; onlyEnvExportsis affected.How was it tested?
internal/devbox/envvars_test.go::TestOnlyModifiedEnvs, covering unchanged variables being dropped, changed/new/emptied variables being kept, and empty-base-env behavior.go test ./internal/devbox/ -run TestOnlyModifiedEnvs— passes.go build ./internal/devbox/,go vet ./internal/devbox/, andgofmt— all clean.testscripts/shell/shellenv.test.txtflow is unaffected: it sources theexport PATH=line fromdevbox shellenv, andPATHis always modified by Devbox, so it is still emitted.cc @gasuketsu (issue reporter) — thanks for the clear write-up and pinpointing the read-only variable failure.
Community Contribution License
All community contributions in this pull request are licensed to the project
maintainers under the terms of the
Apache 2 License.
By creating this pull request, I represent that I have the right to license the
contributions to the project maintainers under the Apache 2 License as stated in
the
Community Contribution License.
🤖 Generated with Claude Code
https://claude.ai/code/session_01WiTxPixVaWVbpBsZf1DLeG
Generated by Claude Code