fix(shellenv): preserve newlines in exported env values (ambiguous redirect)#2905
Open
mikeland73 wants to merge 2 commits into
Open
fix(shellenv): preserve newlines in exported env values (ambiguous redirect)#2905mikeland73 wants to merge 2 commits into
mikeland73 wants to merge 2 commits into
Conversation
exportify escaped literal newlines in environment variable values as `\<newline>`. Inside a double-quoted shell string that sequence is a line continuation, which the shell deletes — joining the value's adjacent lines. For a multi-command PROMPT_COMMAND (e.g. the bash-preexec value set by some terminals), this collapsed a line ending in `2>&1` into the next command `__bp_interactive_mode`, producing `2>&1__bp_interactive_mode` and the `bash: 1__bp_interactive_mode: ambiguous redirect` error on every prompt. A bare newline inside double quotes is already preserved verbatim, so simply drop the backslash escape for `\n`. Add a regression test. Fixes #2814 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XMi8bm4fhxGAdxSCt5YJY3
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes an issue in devbox global shellenv where multi-line environment variable values (notably PROMPT_COMMAND) were being corrupted due to incorrectly escaping literal newlines, which could trigger bash: ... ambiguous redirect errors when evaluated.
Changes:
- Stop escaping
\ninexportifyso literal newlines inside double quotes are preserved verbatim (avoiding POSIX line-continuation semantics). - Add a regression test asserting the export output contains bare newlines and does not emit backslash-newline sequences.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| internal/devbox/envvars.go | Adjusts exportify escaping rules to preserve literal newlines in exported env values. |
| internal/devbox/envvars_test.go | Adds a regression test ensuring multi-line values are emitted without \\\n escaping and remain multi-line. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
The added comment expanded the loop's scope enough for golangci-lint's varnamelen to flag the single-letter `r`. Rename it to `char`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XMi8bm4fhxGAdxSCt5YJY3
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: 1__bp_interactive_mode: ambiguous redirectat every prompt when the user's environment carried a multi-linePROMPT_COMMAND(e.g. the bash-preexec value some terminals set).Root cause:
exportifyininternal/devbox/envvars.goescaped literal newlines in a value as\followed by a newline. Inside a double-quoted shell string that sequence is a line continuation, which the shell deletes — joining the value's adjacent lines. A line ending in2>&1collapsed into the following command__bp_interactive_mode, producing2>&1__bp_interactive_mode, which bash parses as a redirect to the file1__bp_interactive_mode→ "ambiguous redirect".A bare newline inside double quotes is already preserved verbatim, so the escape was both unnecessary and the source of the corruption. The fix drops the backslash escape for
\n; the other special characters ($,`,",\) are still escaped.How was it tested?
TestExportifyPreservesNewlinesasserting the emitted export contains a bare (not backslash-escaped) newline and preserves the multi-line value. Existingexportifytests still pass.... 2>&1__bp_interactive_mode); sourcing the fixed output keeps them as separate commands.go build/go vetoninternal/devboxare 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_01XMi8bm4fhxGAdxSCt5YJY3
Generated by Claude Code