Skip to content

Commit c4648ab

Browse files
go: fix bug - Go doesn't update version if go is already installed (devcontainers#303)
* go: fix bug - Go doesn't update version if go is already installed * Update tests with bash -c
1 parent 173c7ca commit c4648ab

6 files changed

Lines changed: 31 additions & 9 deletions

File tree

src/go/devcontainer-feature.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"id": "go",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"name": "Go",
55
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/go",
66
"description": "Installs Go and common Go utilities. Auto-detects latest version and installs needed dependencies.",

src/go/install.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ if ! cat /etc/group | grep -e "^golang:" > /dev/null 2>&1; then
142142
fi
143143
usermod -a -G golang "${USERNAME}"
144144
mkdir -p "${TARGET_GOROOT}" "${TARGET_GOPATH}"
145-
if [ "${TARGET_GO_VERSION}" != "none" ] && ! type go > /dev/null 2>&1; then
145+
146+
if [[ "${TARGET_GO_VERSION}" != "none" ]] && [[ "$(go version)" != *"${TARGET_GO_VERSION}"* ]]; then
146147
# Use a temporary locaiton for gpg keys to avoid polluting image
147148
export GNUPGHOME="/tmp/tmp-gnupg"
148149
mkdir -p ${GNUPGHOME}
@@ -186,7 +187,7 @@ if [ "${TARGET_GO_VERSION}" != "none" ] && ! type go > /dev/null 2>&1; then
186187
tar -xzf /tmp/go.tar.gz -C "${TARGET_GOROOT}" --strip-components=1
187188
rm -rf /tmp/go.tar.gz /tmp/go.tar.gz.asc /tmp/tmp-gnupg
188189
else
189-
echo "Go already installed. Skipping."
190+
echo "(!) Go is already installed with version ${TARGET_GO_VERSION}. Skipping."
190191
fi
191192

192193
# Install Go tools that are isImportant && !replacedByGopls based on
@@ -218,8 +219,10 @@ if [ "${INSTALL_GO_TOOLS}" = "true" ]; then
218219
(echo "${GO_TOOLS}" | xargs -n 1 go ${go_install_command} -v )2>&1 | tee -a /usr/local/etc/vscode-dev-containers/go.log
219220

220221
# Move Go tools into path and clean up
221-
mv /tmp/gotools/bin/* ${TARGET_GOPATH}/bin/
222-
rm -rf /tmp/gotools
222+
if [ -d /tmp/gotools/bin ]; then
223+
mv /tmp/gotools/bin/* ${TARGET_GOPATH}/bin/
224+
rm -rf /tmp/gotools
225+
fi
223226

224227
# Install golangci-lint from precompiled binares
225228
if [ "$GOLANGCILINT_VERSION" = "latest" ] || [ "$GOLANGCILINT_VERSION" = "" ]; then

test/go/install_go_tool_in_postCreate.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ set -e
55
# Optional: Import test library
66
source dev-container-features-test-lib
77

8-
check "mkcert version" mkcert --version | grep "v1.4.2"
9-
check "mkcert is installed at correct path" which mkcert | grep "/go/bin/mkcert"
10-
check "golangci-lint version" golangci-lint --version | grep "golangci-lint has version 1.50.0"
8+
check "mkcert version" bash -c "mkcert --version | grep v1.4.2"
9+
check "mkcert is installed at correct path" bash -c "which mkcert | grep /go/bin/mkcert"
10+
check "golangci-lint version" bash -c "golangci-lint --version | grep 'golangci-lint has version 1.50.0'"
1111

1212
# Report result
1313
reportResults

test/go/install_go_twice.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# Optional: Import test library
6+
source dev-container-features-test-lib
7+
8+
check "go-version" bash -c "go version | grep 1.19"
9+
10+
# Report result
11+
reportResults

test/go/scenarios.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,13 @@
88
}
99
},
1010
"postCreateCommand": "go install filippo.io/mkcert@v1.4.2"
11+
},
12+
"install_go_twice": {
13+
"image": "mcr.microsoft.com/devcontainers/go:1.18",
14+
"features": {
15+
"go": {
16+
"version": "1.19"
17+
}
18+
}
1119
}
1220
}

test/go/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ source dev-container-features-test-lib
77

88
check "version" go version
99
check "revive version" revive --version
10-
check "revive is installed at correct path" which revive | grep "/go/bin/revive"
10+
check "revive is installed at correct path" bash -c "which revive | grep /go/bin/revive"
1111

1212
# Report result
1313
reportResults

0 commit comments

Comments
 (0)