Skip to content
Merged
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
21 changes: 20 additions & 1 deletion libexec/goenv
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/usr/bin/env bash
set -e
export -n CDPATH
export LC_ALL=C # boost grep performance by disabling unicode

# Save original LC_ALL to restore later
GOENV_ORIGINAL_LC_ALL="${LC_ALL:-}"

# Temporarily set LC_ALL=C to boost grep performance by disabling unicode
export LC_ALL=C

if [ "$1" = "--debug" ]; then
export GOENV_DEBUG=1
Expand All @@ -24,6 +29,15 @@ abort() {
exit 1
}

# Restore original LC_ALL before executing commands
restore_lc_all() {
if [ -n "$GOENV_ORIGINAL_LC_ALL" ]; then
export LC_ALL="$GOENV_ORIGINAL_LC_ALL"
else
unset LC_ALL
fi
}

if enable -f "${BASH_SOURCE%/*}"/../libexec/goenv-realpath.dylib realpath 2>/dev/null; then
abs_dirname() {
local path="$(realpath "$1")"
Expand Down Expand Up @@ -121,13 +135,16 @@ case "$command" in
} | abort
;;
-v | --version)
restore_lc_all
exec goenv---version
;;
-h | --help)
restore_lc_all
exec goenv-help
;;
# NOTE: Provide goenv completions
--complete)
restore_lc_all
exec goenv-commands
;;
*)
Expand All @@ -149,8 +166,10 @@ case "$command" in
fi

if [ "$1" = --help ]; then
restore_lc_all
exec goenv-help "$command"
else
restore_lc_all
exec "$command_path" "$@"
fi
;;
Expand Down
33 changes: 33 additions & 0 deletions test/goenv-exec.bats
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,36 @@ $GOENV_ROOT/versions/1.12.0
/tmp/goenv/example/1.12.0
OUT
}

@test "preserves LC_ALL environment variable when executing commands" {
create_version "1.12.0"
create_executable "1.12.0" "go-lc" <<SH
#!$BASH
echo "LC_ALL=\$LC_ALL"
SH

LC_ALL=en_US.UTF-8 GOENV_VERSION=1.12.0 PATH=${GOENV_TEST_DIR}:${PATH} run goenv-exec go-lc

assert_success_out <<OUT
LC_ALL=en_US.UTF-8
OUT
}

@test "LC_ALL remains unset if it was not set before" {
create_version "1.12.0"
create_executable "1.12.0" "go-lc" <<SH
#!$BASH
if [ -z "\$LC_ALL" ]; then
echo "LC_ALL is unset"
else
echo "LC_ALL=\$LC_ALL"
fi
SH

unset LC_ALL
GOENV_VERSION=1.12.0 PATH=${GOENV_TEST_DIR}:${PATH} run goenv-exec go-lc

assert_success_out <<OUT
LC_ALL is unset
OUT
}
Loading