Skip to content

Commit 8b93a3e

Browse files
Fix goenv alias regression - respect explicit version folder names (#495)
* Initial plan * Fix variable reference bug in goenv-version-name and goenv-which Co-authored-by: ChronosMasterOfAllTime <28963807+ChronosMasterOfAllTime@users.noreply.github.com> * Fix goenv-installed to respect exact version folders before resolving Co-authored-by: ChronosMasterOfAllTime <28963807+ChronosMasterOfAllTime@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ChronosMasterOfAllTime <28963807+ChronosMasterOfAllTime@users.noreply.github.com>
1 parent 142ab18 commit 8b93a3e

5 files changed

Lines changed: 50 additions & 2 deletions

File tree

libexec/goenv-installed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ if [ "$version" = "latest" ]; then
8787
fi
8888
fi
8989

90+
# Check if exact version folder exists first (e.g., user has an alias folder like "1.21")
91+
if [ -d "${GOENV_ROOT}/versions/${version}" ]; then
92+
echo "$version"
93+
exit 0
94+
fi
95+
9096
# Check version=1 (major without minor and patch) => 1.23.4 (latest major version)
9197
# Or version=23 (minor without major and patch) => 1.23.4 (latest patch version)
9298
# Or version=23rc1 (minor with release candidate) => 1.23rc2 (latest release candidate version)

libexec/goenv-version-name

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ version_exists() {
2626
local input_version="$1"
2727
local use_go_mod="$2"
2828

29-
if [[ -n use_go_mod ]] && [[ -d ${GOENV_ROOT}/versions ]] && grep -q -E "^[0-9]+\.[0-9]+\.?[0-9]*(\s*)$" <<<${input_version}; then
29+
if [[ -n "$use_go_mod" ]] && [[ -d ${GOENV_ROOT}/versions ]] && grep -q -E "^[0-9]+\.[0-9]+\.?[0-9]*(\s*)$" <<<${input_version}; then
3030
OLDIFS="$IFS"
3131
IFS=$'\n'
3232
local version_regex=$(echo ${input_version} | sed s/\\./\\\\./)

libexec/goenv-which

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ version_exists() {
6363
local input_version="$1"
6464
local use_go_mod="$2"
6565

66-
if [[ -n use_go_mod ]] && [[ -d ${GOENV_ROOT}/versions ]] && grep -q -E "^[0-9]+\.[0-9]+(\s*)$" <<<${input_version}; then
66+
if [[ -n "$use_go_mod" ]] && [[ -d ${GOENV_ROOT}/versions ]] && grep -q -E "^[0-9]+\.[0-9]+(\s*)$" <<<${input_version}; then
6767
OLDIFS="$IFS"
6868
IFS=$'\n'
6969
local version_regex=$(echo ${input_version} | sed s/\\./\\\\./)

test/goenv-installed.bats

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,26 @@ OUT
101101
run goenv-installed 1.2.4
102102
assert_failure "goenv: version '1.2.4' not installed"
103103
}
104+
105+
@test "respects exact version folder when alias folder exists" {
106+
# User has an alias folder like "1.21" that contains go 1.21.13
107+
mkdir -p "${GOENV_ROOT}/versions/1.21"
108+
run goenv-installed 1.21
109+
assert_success "1.21"
110+
}
111+
112+
@test "uses exact alias folder even when full version folder exists" {
113+
# User has both an alias folder and the full version folder
114+
mkdir -p "${GOENV_ROOT}/versions/1.21"
115+
mkdir -p "${GOENV_ROOT}/versions/1.21.13"
116+
run goenv-installed 1.21
117+
assert_success "1.21"
118+
}
119+
120+
@test "resolves to latest patch when no exact alias folder exists" {
121+
# Only full version folders exist, no alias
122+
mkdir -p "${GOENV_ROOT}/versions/1.20.5"
123+
mkdir -p "${GOENV_ROOT}/versions/1.20.8"
124+
run goenv-installed 1.20
125+
assert_success "1.20.8"
126+
}

test/goenv-version-name.bats

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,22 @@ OUT
121121

122122
assert_success "1.10.3"
123123
}
124+
125+
@test "respects version set by alias folder name" {
126+
# Simulate installing go 1.21.13 aliased as 1.21
127+
create_version "1.21"
128+
echo "1.21" > '.go-version'
129+
run goenv-version-name
130+
131+
assert_success "1.21"
132+
}
133+
134+
@test "respects version set by alias folder name even when full version folder exists" {
135+
# User has both an alias folder and the full version folder
136+
create_version "1.21"
137+
create_version "1.21.13"
138+
echo "1.21" > '.go-version'
139+
run goenv-version-name
140+
141+
assert_success "1.21"
142+
}

0 commit comments

Comments
 (0)