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: Quote commands correctly in plugin-test #1078

Merged
merged 7 commits into from Nov 2, 2021

Conversation

raxod502
Copy link
Contributor

Summary

Quote variables correctly in command-plugin-test.bash, eliminating bizarre results when trying to run commands with whitespace in them.

Other Information

Before:

% asdf plugin-test bundler https://github.com/jonathanmorley/asdf-bundler.git 'bundler --version | grep 2.2.29'
Updating bundler...
Already on 'master'
Your branch is up to date with 'origin/master'.
ERROR: "bundler version" was called with arguments ["|", "grep", "2.2.29"]
Usage: "bundler version"
FAILED: bundler --version | grep 2.2.29 failed with exit code 0

After:

% ./bin/asdf plugin-test bundler https://github.com/jonathanmorley/asdf-bundler.git 'bundler --version | grep 2.2.29'
Updating bundler to master
Already on 'master'
Your branch is up to date with 'origin/master'.
Bundler version 2.2.29

The intended behavior was somewhat unclear, but in the new code, if one argument is provided, it is assumed to be a shell command, while if multiple arguments are provided, it is assumed that the first is an executable and the remainder are arguments (so no shell is involved).

The use case that generated this pull request was specifying the following configuration to https://github.com/asdf-vm/actions for jonathanmorley/asdf-bundler#4:

      - name: asdf_plugin_test
        uses: asdf-vm/actions/plugin-test@v1
        with:
          command: bundle --version | grep -F "${{ matrix.version }}"

Where we see that the command is passed as a single argument to asdf plugin-test, and therefore ought to be interpreted as a shell command, rather than being split on whitespace and then executed without shell interpretation, which is the current behavior (see the bizarre error message above).

@raxod502 raxod502 requested a review from a team as a code owner October 24, 2021 23:50
@raxod502 raxod502 changed the title Quote commands correctly in plugin-test fix: Quote commands correctly in plugin-test Oct 24, 2021
@@ -129,10 +129,10 @@ plugin_test_command() {
fi

if [ -n "$plugin_command" ]; then
$plugin_command
"${plugin_command[@]}"
Copy link
Contributor

Choose a reason for hiding this comment

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

While we are currently using Bash, our goal is to take the POSIX compat soln where possible to support more Shells in future. Are shift and [@] POSIX compatible?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

shift is POSIX per this ref. Unfortunately, POSIX sh does not have arrays at all, so you'll have to do a more thorough rearchitecting in order to make this code portable. Applying this PR won't decrease the portability since all shells with arrays should have [@] indexing (otherwise they're not much use, since [*] doesn't handle arguments with spaces correctly).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking at the code, I think it would not be too hard in this case to refactor so it does not use arrays at all, would you like me to do that?

Copy link
Member

Choose a reason for hiding this comment

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

@raxod502 which do you think would be better? I only see one line that actually uses this variable as an array (line 25 pushes an element to the array).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, if you want to make asdf POSIX compliant in the long run, we might as well get rid of the array usage, since you'll have to do that at some point. I pushed a new commit doing so, and fixing some other quoting errors.

;;
esac
done

plugin_command="${plugin_command_array[*]}"
if [ "${#plugin_command[@]}" -eq 1 ]; then
plugin_command=(sh -c "${plugin_command[@]}")
Copy link
Member

Choose a reason for hiding this comment

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

Should this be sh or bash?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Pushed a commit making it default to $SHELL with fallback to POSIX-safe sh.

Copy link
Contributor

@jthegedus jthegedus left a comment

Choose a reason for hiding this comment

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

thanks @raxod502 , lgtm. merge if you're happy @Stratus3D

@jthegedus
Copy link
Contributor

@raxod502 can you run the scripts/shfmt.bash script?

@raxod502
Copy link
Contributor Author

raxod502 commented Nov 2, 2021

Done, sorry for not doing it the first time, shfmt wasn't packaged for Ubuntu so I had to write a packaging script to install it:

#!/usr/bin/env bash

set -euxo pipefail

ver="$(curl -fsSL https://api.github.com/repos/mvdan/sh/releases | jq 'map(.name) | .[]' -r | sed 's/^v//' | sort -V | tail -n1)"

pkg="${PWD}/shfmt-${ver}"

rm -rf "${pkg}"
install -d "${pkg}/usr/bin"
wget "https://github.com/mvdan/sh/releases/download/v${ver}/shfmt_v${ver}_linux_amd64" -O "${pkg}/usr/bin/shfmt"
chmod +x "${pkg}/usr/bin/shfmt"

install -d "${pkg}/DEBIAN"
tee "${pkg}/DEBIAN/control" <<EOF >/dev/null
Package: shfmt
Version: ${ver}
Section: shells
Priority: optional
Architecture: amd64
Maintainer: Radon Rosborough <radon.neon@gmail.com>
Description: Formats shell programs
EOF

fakeroot dpkg-deb --build "${pkg}" "shfmt-${ver}.deb"
if dpkg -s shfmt &>/dev/null; then
    sudo apt remove -y shfmt
fi
sudo apt install -y "./shfmt-${ver}.deb"

@jthegedus
Copy link
Contributor

Done, sorry for not doing it the first time, shfmt wasn't packaged for Ubuntu so I had to write a packaging script to install it:

We've got a .tool-versions in this repo with the deps for managing via asdf 😉

@Stratus3D Stratus3D merged commit 69ff2d0 into asdf-vm:master Nov 2, 2021
@raxod502 raxod502 deleted the rr-shift-correctly branch November 2, 2021 15:40
@raxod502
Copy link
Contributor Author

raxod502 commented Nov 2, 2021

We've got a .tool-versions in this repo with the deps

Well, I can't say I shouldn't have seen that coming.

@tsuyoshicho
Copy link

tsuyoshicho commented Nov 3, 2021

I observed This PR make asdf-vm/actions/plugin-test@v1 to failed.

These test ran after this PR merged.

I think test time ASDF_INSTALL_VERSION set as --asdf-plugin-gitref

@Stratus3D
Copy link
Member

@tsuyoshicho yeah it does look like we may have broken something here. @raxod502 would you be willing to take a look at @tsuyoshicho builds? Output shows things like:

asdf-awscli: Could not download https://github.com/aws/aws-cli/archive/--asdf-plugin-gitref.tar.gz

clearly --asdf-plugin-gitref was a flag for the tag, not the tag itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants