Skip to content

Commit 8eacc40

Browse files
Fix LC_ALL being reset by goenv - preserve user's locale settings
Co-authored-by: ChronosMasterOfAllTime <28963807+ChronosMasterOfAllTime@users.noreply.github.com>
1 parent 9847a07 commit 8eacc40

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

libexec/goenv

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
#!/usr/bin/env bash
22
set -e
33
export -n CDPATH
4-
export LC_ALL=C # boost grep performance by disabling unicode
4+
5+
# Save original LC_ALL to restore later
6+
GOENV_ORIGINAL_LC_ALL="${LC_ALL:-}"
7+
8+
# Temporarily set LC_ALL=C to boost grep performance by disabling unicode
9+
export LC_ALL=C
510

611
if [ "$1" = "--debug" ]; then
712
export GOENV_DEBUG=1
@@ -24,6 +29,15 @@ abort() {
2429
exit 1
2530
}
2631

32+
# Restore original LC_ALL before executing commands
33+
restore_lc_all() {
34+
if [ -n "$GOENV_ORIGINAL_LC_ALL" ]; then
35+
export LC_ALL="$GOENV_ORIGINAL_LC_ALL"
36+
else
37+
unset LC_ALL
38+
fi
39+
}
40+
2741
if enable -f "${BASH_SOURCE%/*}"/../libexec/goenv-realpath.dylib realpath 2>/dev/null; then
2842
abs_dirname() {
2943
local path="$(realpath "$1")"
@@ -121,13 +135,16 @@ case "$command" in
121135
} | abort
122136
;;
123137
-v | --version)
138+
restore_lc_all
124139
exec goenv---version
125140
;;
126141
-h | --help)
142+
restore_lc_all
127143
exec goenv-help
128144
;;
129145
# NOTE: Provide goenv completions
130146
--complete)
147+
restore_lc_all
131148
exec goenv-commands
132149
;;
133150
*)
@@ -149,8 +166,10 @@ case "$command" in
149166
fi
150167

151168
if [ "$1" = --help ]; then
169+
restore_lc_all
152170
exec goenv-help "$command"
153171
else
172+
restore_lc_all
154173
exec "$command_path" "$@"
155174
fi
156175
;;

test/goenv-exec.bats

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,36 @@ $GOENV_ROOT/versions/1.12.0
350350
/tmp/goenv/example/1.12.0
351351
OUT
352352
}
353+
354+
@test "preserves LC_ALL environment variable when executing commands" {
355+
create_version "1.12.0"
356+
create_executable "1.12.0" "go-lc" <<SH
357+
#!$BASH
358+
echo "LC_ALL=\$LC_ALL"
359+
SH
360+
361+
LC_ALL=en_US.UTF-8 GOENV_VERSION=1.12.0 PATH=${GOENV_TEST_DIR}:${PATH} run goenv-exec go-lc
362+
363+
assert_success_out <<OUT
364+
LC_ALL=en_US.UTF-8
365+
OUT
366+
}
367+
368+
@test "LC_ALL remains unset if it was not set before" {
369+
create_version "1.12.0"
370+
create_executable "1.12.0" "go-lc" <<SH
371+
#!$BASH
372+
if [ -z "\$LC_ALL" ]; then
373+
echo "LC_ALL is unset"
374+
else
375+
echo "LC_ALL=\$LC_ALL"
376+
fi
377+
SH
378+
379+
unset LC_ALL
380+
GOENV_VERSION=1.12.0 PATH=${GOENV_TEST_DIR}:${PATH} run goenv-exec go-lc
381+
382+
assert_success_out <<OUT
383+
LC_ALL is unset
384+
OUT
385+
}

0 commit comments

Comments
 (0)