Skip to content

Commit 65c3c1c

Browse files
authored
[envvars] Fix error when env contains Bash function (#2612)
## Summary Fixes #995 - Error running `devbox shell` in environment with exported Bash function ## How was it tested? Tested that when running the below commands: - no errors are printed - the `foo` function runs successfully and prints `foo` Case 1 (simple): ``` foo() { echo foo; } export -f foo devbox shell foo ``` Case 2 (function that contains special characters `"` and `$`): ``` foo() { echo "${bar}"; } export -f foo export bar=foo devbox shell foo ``` Case 3 (`shellenv`): ``` foo() { echo foo; } export -f foo devbox shellenv > shellrc # open a new shell source shellrc foo ``` Also added a Bash function to the test data used by `TestWriteDevboxShellrc`. ## Community Contribution License All community contributions in this pull request are licensed to the project maintainers under the terms of the [Apache 2 License](https://www.apache.org/licenses/LICENSE-2.0). 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](https://github.com/jetify-com/opensource/blob/main/CONTRIBUTING.md#community-contribution-license).
1 parent 70964d8 commit 65c3c1c

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

internal/devbox/envvars.go

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,33 @@ func exportify(vars map[string]string) string {
2929
slices.Sort(keys) // for reproducibility
3030

3131
strb := strings.Builder{}
32-
for _, k := range keys {
33-
strb.WriteString("export ")
34-
strb.WriteString(k)
35-
strb.WriteString(`="`)
36-
for _, r := range vars[k] {
37-
switch r {
38-
// Special characters inside double quotes:
39-
// https://pubs.opengroup.org/onlinepubs/009604499/utilities/xcu_chap02.html#tag_02_02_03
40-
case '$', '`', '"', '\\', '\n':
41-
strb.WriteRune('\\')
32+
for _, key := range keys {
33+
if strings.HasPrefix(key, "BASH_FUNC_") && strings.HasSuffix(key, "%%") {
34+
// Bash function
35+
funcName := strings.TrimSuffix(key, "%%")
36+
funcName = strings.TrimPrefix(funcName, "BASH_FUNC_")
37+
strb.WriteString(funcName)
38+
strb.WriteString(" ")
39+
strb.WriteString(vars[key])
40+
strb.WriteString("\nexport -f ")
41+
strb.WriteString(funcName)
42+
strb.WriteString("\n")
43+
} else {
44+
// Regular variable
45+
strb.WriteString("export ")
46+
strb.WriteString(key)
47+
strb.WriteString(`="`)
48+
for _, r := range vars[key] {
49+
switch r {
50+
// Special characters inside double quotes:
51+
// https://pubs.opengroup.org/onlinepubs/009604499/utilities/xcu_chap02.html#tag_02_02_03
52+
case '$', '`', '"', '\\', '\n':
53+
strb.WriteRune('\\')
54+
}
55+
strb.WriteRune(r)
4256
}
43-
strb.WriteRune(r)
57+
strb.WriteString("\";\n")
4458
}
45-
strb.WriteString("\";\n")
4659
}
4760
return strings.TrimSpace(strb.String())
4861
}

internal/devbox/testdata/shellrc/basic/env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ simple=value
22
space=quote me
33
quote=they said, "lasers"
44
special=$`"\
5+
BASH_FUNC_echo_simple%%=() { echo "${simple}"; }

internal/devbox/testdata/shellrc/basic/shellrc.golden

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ if [ -f testdata/shellrc/basic/shellrc ]; then
33
fi
44
# Begin Devbox Post-init Hook
55

6+
echo_simple () { echo "${simple}"; }
7+
export -f echo_simple
68
export quote="they said, \"lasers\"";
79
export simple="value";
810
export space="quote me";

0 commit comments

Comments
 (0)