fix(shellenv): preserve newlines in exported env values (ambiguous redirect)#2932
Open
mikeland73 wants to merge 2 commits into
Open
fix(shellenv): preserve newlines in exported env values (ambiguous redirect)#2932mikeland73 wants to merge 2 commits into
mikeland73 wants to merge 2 commits into
Conversation
exportify escaped newlines in env values as a backslash-newline. Inside a double-quoted bash string that is a line continuation, which the shell removes, silently joining the two lines together. Any multi-line value — most notably a PROMPT_COMMAND captured from bash-preexec — was corrupted: adjacent lines fused into strings like `... 2>&1__bp_interactive_mode`, producing a "bash: ...: ambiguous redirect" error at every prompt. A literal newline inside double quotes is already preserved as-is, so stop escaping it. Add a regression test covering the multi-line PROMPT_COMMAND case from the issue. Fixes #2814 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014nhuYqHdGxzteXxH78b4sE
The added explanatory comment widened the scope over which the single-letter loop variable is used, tripping golangci-lint's varnamelen. Rename it to a descriptive name. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_014nhuYqHdGxzteXxH78b4sE
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 #2814.
eval "$(devbox global shellenv)"printedbash: ...__bp_interactive_mode: ambiguous redirectat every prompt when the user'sPROMPT_COMMANDwas multi-line (as set up by bash-preexec / the elementary terminal integration).Root cause:
exportify(internal/devbox/envvars.go) emits each variable asexport KEY="value";, escaping special characters so the value is treated literally. It escaped newlines as a backslash followed by the newline. Inside a bash double-quoted string, a backslash-newline is a line continuation that the shell removes, silently joining the two lines. So aPROMPT_COMMANDlike:collapsed into
... 2>&1__bp_interactive_mode, and bash parsed2>&1__bp_interactive_modeas a redirect to the ambiguous target1__bp_interactive_mode→ "ambiguous redirect".Fix: stop escaping the newline. A literal newline inside double quotes is already preserved verbatim, so the value round-trips correctly.
$,`,", and\are still escaped as before.Demonstration of the two behaviors:
How was it tested?
TestExportifyPreservesNewlinesreproducing the issue's multi-linePROMPT_COMMANDand asserting the newline is emitted literally (never as a backslash-newline line continuation).go test ./internal/devbox/ -run 'TestExportify|TestIsValidEnvName'— all pass.go build ./internal/devbox/,go vet ./internal/devbox/, andgofmt -l— clean.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.
cc @proedie (issue reporter)
🤖 Generated with Claude Code
https://claude.ai/code/session_014nhuYqHdGxzteXxH78b4sE
Generated by Claude Code