diff --git a/libexec/goenv-version-name b/libexec/goenv-version-name index d516d532..388b89e3 100755 --- a/libexec/goenv-version-name +++ b/libexec/goenv-version-name @@ -31,7 +31,7 @@ version_exists() { IFS=$'\n' local version_regex=$(echo ${input_version} | sed s/\\./\\\\./) - # check if input_version is major/minor/patch instead of just major/minor + # check if input_version is major.minor only (not major.minor.patch) to add optional patch pattern if ! echo "${input_version}" | grep -q -E "^[0-9]+\.[0-9]+\.[0-9]+(\s*)$"; then version_regex="${version_regex}(\\.[0-9]+)?" fi @@ -61,6 +61,9 @@ OLDIFS="$IFS" if [[ -z $GOENV_GOMOD_VERSION_ENABLE ]] || ! version_exists "$version" "$GOENV_GOMOD_VERSION_ENABLE"; then echo "goenv: version '$version' is not installed (set by $(goenv-version-origin))" >&2 any_not_installed=1 + else + # version_exists with GOENV_GOMOD_VERSION_ENABLE succeeded and resolved the version + versions=("${versions[@]}" "${version}") fi fi done diff --git a/libexec/goenv-which b/libexec/goenv-which index c17c6403..7ddc0712 100755 --- a/libexec/goenv-which +++ b/libexec/goenv-which @@ -26,6 +26,49 @@ OLDIFS="$IFS" IFS=: versions=(${GOENV_VERSION:-$(goenv-version-name)}) IFS="$OLDIFS" +version_exists() { + local input_version="$1" + local use_go_mod="$2" + + if [[ -n $use_go_mod ]] && [[ -d ${GOENV_ROOT}/versions ]] && grep -q -E "^[0-9]+\.[0-9]+\.?[0-9]*(\s*)$" <<<${input_version}; then + OLDIFS="$IFS" + IFS=$'\n' + local version_regex=$(echo ${input_version} | sed s/\\./\\\\./) + + # check if input_version is major.minor only (not major.minor.patch) to add optional patch pattern + if ! echo "${input_version}" | grep -q -E "^[0-9]+\.[0-9]+\.[0-9]+(\s*)$"; then + version_regex="${version_regex}(\\.[0-9]+)?" + fi + + local versions=($(ls ${GOENV_ROOT}/versions | grep -E "^$version_regex$" | sort -V)) + IFS="$OLDIFS" + if [[ ${#versions[@]} -eq 0 ]]; then + return 1 + fi + version=${versions[${#versions[@]} - 1]} + fi + + [ -d "${GOENV_ROOT}/versions/${version}" ] +} + +# Resolve major.minor versions to full versions when GOENV_GOMOD_VERSION_ENABLE is set +if [[ -n "$GOENV_GOMOD_VERSION_ENABLE" ]]; then + resolved_versions=() + for ver in "${versions[@]}"; do + if [ "$ver" != "system" ]; then + version="$ver" + if version_exists "$version" "$GOENV_GOMOD_VERSION_ENABLE"; then + resolved_versions=("${resolved_versions[@]}" "${version}") + else + resolved_versions=("${resolved_versions[@]}" "$ver") + fi + else + resolved_versions=("${resolved_versions[@]}" "$ver") + fi + done + versions=("${resolved_versions[@]}") +fi + remove_from_path() { local path_to_remove="$1" local path_before @@ -59,25 +102,6 @@ for version in "${versions[@]}"; do fi done -version_exists() { - local input_version="$1" - local use_go_mod="$2" - - if [[ -n $use_go_mod ]] && [[ -d ${GOENV_ROOT}/versions ]] && grep -q -E "^[0-9]+\.[0-9]+(\s*)$" <<<${input_version}; then - OLDIFS="$IFS" - IFS=$'\n' - local version_regex=$(echo ${input_version} | sed s/\\./\\\\./) - local versions=($(ls ${GOENV_ROOT}/versions | grep -E "^$version_regex(\\.[0-9]+)?$" | sort -V)) - IFS="$OLDIFS" - if [[ ${#versions[@]} -eq 0 ]]; then - return 1 - fi - version=${versions[${#versions[@]} - 1]} - fi - - [ -d "${GOENV_ROOT}/versions/${version}" ] -} - OLDIFS="$IFS" IFS=$'\n' scripts=($(goenv-hooks which)) @@ -96,7 +120,7 @@ for version in "${versions[@]}"; do if [ "$version" = "system" ]; then continue fi - if ! version_exists "$version" "$GOENV_GO_MOD_ENABLE"; then + if ! version_exists "$version" "$GOENV_GOMOD_VERSION_ENABLE"; then echo "goenv: version '$version' is not installed (set by $(goenv-version-origin))" >&2 any_not_installed=1 fi diff --git a/test/goenv-exec.bats b/test/goenv-exec.bats index 7c34a159..d0116ff9 100644 --- a/test/goenv-exec.bats +++ b/test/goenv-exec.bats @@ -383,3 +383,36 @@ SH LC_ALL is unset OUT } + +@test "resolves major.minor version from go.mod and executes command when GOENV_GOMOD_VERSION_ENABLE=1" { + create_executable "1.20.14" "go" < go.mod < go.mod < "${GOENV_TEST_DIR}/go.mod" <