Skip to content

Current install-golang.sh script expects go installation in specific directories and doesn't validate go version from GO_VERSIONS file #4517

@ShelbyZ

Description

@ShelbyZ

if [ $go_bin_path == "/usr/bin/go" ]; then
# rpm installs golang to the go_bin_path
echo "golang exists on /usr/bin/go, using system golang version"
elif [ ${go_bin_path:0:10} == "/usr/local" ]; then
# using existing installed golang; re-export path to be sure it's there
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
echo "$GOROOT exists. Using installed golang version"
else
# install golang defined in GO_VERSION file
echo "$GOROOT doesn't exist, installing $(cat ./GO_VERSION)"
GO_VERSION=$(cat ./GO_VERSION)
tmpdir=$(mktemp -d)
GOLANG_TAR="go${GO_VERSION}.linux-${GOARCH}.tar.gz"
wget -O ${tmpdir}/${GOLANG_TAR} https://storage.googleapis.com/golang/${GOLANG_TAR}
# only use sudo if it's available
if ! sudo true; then
tar -C /usr/local -xzf ${tmpdir}/${GOLANG_TAR}
else
sudo tar -C /usr/local -xzf ${tmpdir}/${GOLANG_TAR}
fi
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
# confirm installation
which go
go version
fi

This approach is a bit brittle when go may be installed via 3rd party tools (such as mise). It may be possible that go is not located under:

  1. /usr/bin/go
  2. /usr/local

In the case of mise I can see that the GOROOT environment variable is set and resolves to a path:

++ which go
/Users/shelbyzh/.local/share/mise/installs/go/1.22.7/bin/go
+++ which go
++ export go_bin_path=/Users/shelbyzh/.local/share/mise/installs/go/1.22.7/bin/go
++ go_bin_path=/Users/shelbyzh/.local/share/mise/installs/go/1.22.7/bin/go
++ '[' /Users/shelbyzh/.local/share/mise/installs/go/1.22.7/bin/go == /usr/bin/go ']'
++ '[' /Users/she == /usr/local ']'
+++ cat ./GO_VERSION
++ echo '/Users/shelbyzh/.local/share/mise/installs/go/1.22.7 doesn'\''t exist, installing 1.22.7'
/Users/shelbyzh/.local/share/mise/installs/go/1.22.7 doesn't exist, installing 1.22.7
+++ cat ./GO_VERSION
++ GO_VERSION=1.22.7
+++ mktemp -d
++ tmpdir=/var/folders/fh/t610t8ld6rl9fhd3r7wnr4540000gq/T/tmp.rsfAhGXuU9
++ GOLANG_TAR=go1.22.7.linux-arm64.tar.gz
++ wget -O /var/folders/fh/t610t8ld6rl9fhd3r7wnr4540000gq/T/tmp.rsfAhGXuU9/go1.22.7.linux-arm64.tar.gz https://storage.googleapis.com/golang/go1.22.7.linux-arm64.tar.gz
--2025-02-26 11:27:22--  https://storage.googleapis.com/golang/go1.22.7.linux-arm64.tar.gz
Resolving storage.googleapis.com (storage.googleapis.com)... 142.251.163.207, 142.251.179.207, 142.251.111.207, ...
Connecting to storage.googleapis.com (storage.googleapis.com)|142.251.163.207|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 65919842 (63M) [application/x-gzip]
Saving to: ‘/var/folders/fh/t610t8ld6rl9fhd3r7wnr4540000gq/T/tmp.rsfAhGXuU9/go1.22.7.linux-arm64.tar.gz’

In the above case it is a a valid go install, but not under expected paths (1/2).

We could change simplify the check as follows:

  1. Check GOROOT environment variable is set
  2. Check go version and validate the output against GO_VERSION file
  3. If 1 or 2 fail, then install the expected GO version

Sample:

# if $GOROOT is not set
if [[ -z $GOROOT ]]; then
  # install go
else
  # validate go version against GO_VERSIONS file
fi

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions