Skip to content

[envvars] Fix error when env contains Bash function #2612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions internal/devbox/envvars.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,33 @@ func exportify(vars map[string]string) string {
slices.Sort(keys) // for reproducibility

strb := strings.Builder{}
for _, k := range keys {
strb.WriteString("export ")
strb.WriteString(k)
strb.WriteString(`="`)
for _, r := range vars[k] {
switch r {
// Special characters inside double quotes:
// https://pubs.opengroup.org/onlinepubs/009604499/utilities/xcu_chap02.html#tag_02_02_03
case '$', '`', '"', '\\', '\n':
strb.WriteRune('\\')
for _, key := range keys {
if strings.HasPrefix(key, "BASH_FUNC_") && strings.HasSuffix(key, "%%") {
// Bash function
funcName := strings.TrimSuffix(key, "%%")
funcName = strings.TrimPrefix(funcName, "BASH_FUNC_")
strb.WriteString(funcName)
strb.WriteString(" ")
strb.WriteString(vars[key])
strb.WriteString("\nexport -f ")
strb.WriteString(funcName)
strb.WriteString("\n")
} else {
// Regular variable
strb.WriteString("export ")
strb.WriteString(key)
strb.WriteString(`="`)
for _, r := range vars[key] {
switch r {
// Special characters inside double quotes:
// https://pubs.opengroup.org/onlinepubs/009604499/utilities/xcu_chap02.html#tag_02_02_03
case '$', '`', '"', '\\', '\n':
strb.WriteRune('\\')
}
strb.WriteRune(r)
}
strb.WriteRune(r)
strb.WriteString("\";\n")
}
strb.WriteString("\";\n")
}
return strings.TrimSpace(strb.String())
}
Expand Down
1 change: 1 addition & 0 deletions internal/devbox/testdata/shellrc/basic/env
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ simple=value
space=quote me
quote=they said, "lasers"
special=$`"\
BASH_FUNC_echo_simple%%=() { echo "${simple}"; }
2 changes: 2 additions & 0 deletions internal/devbox/testdata/shellrc/basic/shellrc.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ if [ -f testdata/shellrc/basic/shellrc ]; then
fi
# Begin Devbox Post-init Hook

echo_simple () { echo "${simple}"; }
export -f echo_simple
export quote="they said, \"lasers\"";
export simple="value";
export space="quote me";
Expand Down