Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reshim did not rewrite executable path #1311

Merged
merged 13 commits into from
Dec 20, 2022
41 changes: 16 additions & 25 deletions lib/commands/reshim.bash
Original file line number Diff line number Diff line change
Expand Up @@ -88,37 +88,28 @@ write_shim_script() {
local shim_path
shim_path="$(asdf_data_dir)/shims/$executable_name"

if [ -f "$shim_path" ]; then
if ! grep -x "# asdf-plugin: ${plugin_name} ${version}" "$shim_path" >/dev/null; then
sed -i.bak -e "s/exec /# asdf-plugin: ${plugin_name} ${version}\\"$'\n''exec /' "$shim_path"
rm -f "$shim_path".bak
fi
else
cat <<EOF >"$shim_path"
#!/usr/bin/env bash
local temp_dir
temp_dir=${TMPDIR:-/tmp}

local temp_versions_path
temp_versions_path=$(mktemp "$temp_dir/asdf-command-reshim-write-shims.XXXXXX")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure line 91-92 are necessary as mktemp should fallback to TMPDIR and /tmp automatically.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jthegedus, do you have any thoughts since you added this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seemed fine to me, but after looking at existing use of mktemp this was the outlier, so I changed it to align with the other usages. We can back this out later once we clean up and decide to put this behind a function or something.

cat <<EOF >"$temp_versions_path"
# asdf-plugin: ${plugin_name} ${version}
exec $(asdf_dir)/bin/asdf exec "${executable_name}" "\$@" # asdf_allow: ' asdf '
EOF
fi

chmod +x "$shim_path"
}

generate_shim_for_executable() {
local plugin_name=$1
local executable=$2
if [ -f "$shim_path" ]; then
grep '^#\sasdf-plugin:\s' <"$shim_path" >>"$temp_versions_path"
fi

check_if_plugin_exists "$plugin_name"
cat <<EOF >"$shim_path"
#!/usr/bin/env bash
$(sort -u <"$temp_versions_path")
exec $(asdf_dir)/bin/asdf exec "${executable_name}" "\$@" # asdf_allow: ' asdf '
EOF

local version
IFS=':' read -r -a version_info <<<"$full_version"
if [ "${version_info[0]}" = "ref" ]; then
version="${version_info[1]}"
else
version="${version_info[0]}"
fi
rm "$temp_versions_path"

write_shim_script "$plugin_name" "$version" "$executable"
chmod +x "$shim_path"
}

generate_shims_for_version() {
Expand Down
16 changes: 16 additions & 0 deletions test/reshim_command.bats
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,19 @@ EOM
run asdf reshim dummy 1.0
[ "$output" == "RESHIM" ]
}

# Fixes https://github.com/asdf-vm/asdf/issues/1115
# (Issue with executable_name changing after homebre updates)
@test "reshim should rewrite the shim file except the version list" {
run asdf install dummy 1.0
local dummy_shim
dummy_shim="$ASDF_DIR/shims/dummy"

sed -i.bak -e 's/exec /exec \/borked_path_due_to_homebrew_update/' "$dummy_shim"
run grep 'borked_path_due_to_homebrew_update' "$dummy_shim" # Sanity check
[ "$status" -eq 0 ]

run asdf reshim dummy "path:$ASDF_DIR/installs/dummy/path"
run grep -v 'borked_path_due_to_homebrew_update' "$dummy_shim"
[ "$status" -eq 0 ]
}