Skip to content

Commit fb70363

Browse files
committed
fix: add workaround for asdf symlink bug in plugin (fixed asdf-vm/asdf#1961)
1 parent 3d336a3 commit fb70363

File tree

5 files changed

+113
-40
lines changed

5 files changed

+113
-40
lines changed

bin/exec-env

-12
This file was deleted.

bin/post-plugin-update

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
current_script_path=${BASH_SOURCE[0]}
6+
plugin_dir=$(dirname "$(dirname "$current_script_path")")
7+
8+
# shellcheck source=../lib/utils.bash
9+
source "${plugin_dir}/lib/utils.bash"
10+
11+
sync_installed_go_sdk

contrib/Dockerfile

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1-
FROM ubuntu:20.04
1+
FROM ubuntu:24.04
22

33
RUN apt-get update -y && \
4-
DEBIAN_FRONTEND=noninteractive apt-get install -y bash git curl && \
5-
git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.8.1 && \
4+
DEBIAN_FRONTEND=noninteractive \
5+
apt-get install -y bash git curl golang
6+
7+
RUN git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.15.0 && \
68
echo '. $HOME/.asdf/asdf.sh' >> $HOME/.bashrc && \
79
echo '. $HOME/.asdf/completions/asdf.bash' >> $HOME/.bashrc
810

9-
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y golang
11+
RUN mkdir -p ~/.asdf/plugins/go-sdk
12+
13+
RUN curl https://mise.run | sh && \
14+
echo 'eval "$(~/.local/bin/mise activate bash)"' >> ~/.bashrc && \
15+
mkdir -p ~/.local/share/mise/plugins && \
16+
ln -sf ~/.asdf/plugins/go-sdk ~/.local/share/mise/plugins
17+
18+
RUN go install github.com/asdf-vm/asdf/cmd/asdf@latest
1019

1120
WORKDIR /root

lib/run

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
GO_SDK_LINK_DIR=sdk
3+
4+
bin_dir=${0%/*}
5+
cmd_name="${0##*/}"
6+
GOROOT=$(realpath "${bin_dir}/../${GO_SDK_LINK_DIR}")
7+
export GOROOT
8+
9+
exec "$GOROOT/bin/$cmd_name" "$@"

lib/utils.bash

+80-24
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ TOOL_NAME="go-sdk"
99
TOOL_TEST="go version"
1010
GO_SDK_PATH=
1111
GO_SDK_LOW_LIMIT_VERSION="${GO_SDK_LOW_LIMIT_VERSION:-1.12.0}"
12+
GO_SDK_LINK_DIR=sdk
13+
GO_SDK_SHIM="${current_script_dir}/run"
1214

1315
fail() {
1416
echo -e "asdf-$TOOL_NAME: $*" >/dev/stderr
@@ -171,9 +173,7 @@ install_version() {
171173

172174
${go_bin} download
173175

174-
# Remove exists dir for VERSION before create symlink
175-
[[ -d "${install_path}" ]] && rm -r "${install_path}"
176-
ln -s "$(${go_bin} env GOROOT)" "${install_path}"
176+
sync_installed_go_sdk_for_version "${install_path}"
177177

178178
test -x "$install_path/bin/$tool_cmd" || fail "Expected $install_path/bin/$tool_cmd to be executable."
179179

@@ -188,54 +188,110 @@ uninstall_version() {
188188
local install_type="$1"
189189
local version="$2"
190190
local install_path="$3"
191+
local sdk_link="${install_path}/${GO_SDK_LINK_DIR}"
191192

192193
if [ "$install_type" != "version" ]; then
193194
fail "asdf-$TOOL_NAME supports release uninstalls only"
194195
fi
195196

196197
local go_bin
197-
go_bin=$(find_go_installed_bin "go${version}")
198+
go_bin=$(find_go_installed_bin "go${version}" || true)
198199

199200
if [[ -f "${go_bin}" ]]; then
200201
rm "${go_bin}"
201202
fi
202203

203-
if [[ -L "${install_path}" ]]; then
204+
if [[ -e "${sdk_link}" ]]; then
204205
# Remove GOROOT for version
205-
rm -r "$(readlink "${install_path}")"
206-
# Remove link from asdf install dir
207-
rm "${install_path}"
208-
elif [[ -d "${install_path}" ]]; then
209-
rm -r "${install_path}"
206+
rm -r "$(realpath "${sdk_link}")"
210207
fi
208+
209+
sync_installed_go_sdk_for_version "${install_path}"
210+
}
211+
212+
sync_installed_go_sdk_for_version() {
213+
local plugin_install_path
214+
local version=
215+
local sdk_link
216+
plugin_install_path="$1"
217+
version="${plugin_install_path##*/}"
218+
sdk_link="${plugin_install_path}/${GO_SDK_LINK_DIR}"
219+
220+
if [[ -e "${plugin_install_path}" && -L "${sdk_link}" && ! -e ${sdk_link} ]]; then
221+
# uninstall
222+
echo "Unlink not installed SDK of ${version}"
223+
rm -r "${plugin_install_path}"
224+
return
225+
fi
226+
227+
# install
228+
local sdk
229+
if [[ -e ${sdk_link} ]]; then
230+
sdk=$(realpath "${sdk_link}")
231+
else
232+
local go_bin
233+
go_bin=$(find_go_installed_bin "go${version}" || true)
234+
if [[ -z "${go_bin}" ]]; then
235+
echo "Go version ${version} not installed"
236+
return
237+
fi
238+
sdk=$(${go_bin} env GOROOT)
239+
fi
240+
241+
if [[ -e "${plugin_install_path}" && ! -e "${sdk_link}" ]]; then
242+
rm -r "${plugin_install_path}"
243+
fi
244+
245+
if [[ ! -d "${plugin_install_path}" ]]; then
246+
mkdir -p "${plugin_install_path}"
247+
fi
248+
249+
if [[ ! -e "${sdk_link}" ]]; then
250+
echo "Link installed SDK of ${version} to ${sdk}"
251+
ln -s "${sdk}" "${sdk_link}"
252+
fi
253+
254+
create_bin "${sdk}" "${plugin_install_path}"
255+
}
256+
257+
create_bin() {
258+
local goroot="$1"
259+
local install_path="$2"
260+
local sdk_bin
261+
262+
if [[ ! -d "${install_path}/bin" ]]; then
263+
mkdir -p "${install_path}/bin"
264+
fi
265+
266+
for sdk_bin in "${goroot}/bin/"*; do
267+
local bin_path
268+
local cmd_name=${sdk_bin##*/}
269+
bin_path="${install_path}/bin/${cmd_name}"
270+
ln -sf "${GO_SDK_SHIM}" "${bin_path}"
271+
done
211272
}
212273

213274
sync_installed_go_sdk() {
214275
local plugin_install_path
215-
plugin_install_path=${ASDF_INSTALL_PATH}
276+
local installed
277+
plugin_install_path=${ASDF_INSTALL_PATH:-"$ASDF_DIR/installs/$TOOL_NAME"}
278+
279+
if [[ $plugin_install_path == */$TOOL_NAME/* ]]; then
280+
sync_installed_go_sdk_for_version "${plugin_install_path}"
281+
return
282+
fi
216283

217284
if [[ ! -d "${plugin_install_path}" ]]; then
218285
mkdir -p "${plugin_install_path}"
219286
fi
220287

221288
for installed in "${plugin_install_path}"/*; do
222-
if [[ -L "${installed}" && ! -e "${installed}" ]]; then
223-
echo "Unlink not installed SDK of $(basename "${installed}")"
224-
rm "${installed}"
225-
fi
289+
sync_installed_go_sdk_for_version "${installed}"
226290
done
227291

228292
for sdk in $(list_installed_sdks); do
229293
local version="${sdk#*/go}"
230294
local installed="${plugin_install_path}/${version}"
231-
232-
if [[ -e "${installed}" && ! -L "${installed}" ]]; then
233-
rm -r "${installed}"
234-
fi
235-
236-
if [[ ! -e "${installed}" ]]; then
237-
echo "Link installed SDK of ${version}"
238-
ln -s "${sdk}" "${installed}"
239-
fi
295+
sync_installed_go_sdk_for_version "${installed}"
240296
done
241297
}

0 commit comments

Comments
 (0)