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 Shellcheck violations #443

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

ixti
Copy link

@ixti ixti commented Feb 21, 2023

Resolves: #442

@ixti ixti changed the title Fix outstanding Shellcheck violations Fix Shellcheck violations Feb 21, 2023
Copy link
Owner

@postmodern postmodern left a comment

Choose a reason for hiding this comment

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

Spotted a few things. We may want to disable a few more of the ShellCheck rules, since they might not correctly be matching the code.

@@ -12,7 +12,7 @@ function fetch()

while IFS="" read -r line; do
if [[ "$line" == "$key:"* ]]; then
echo "${line##$key:*([[:space:]])}"
echo "${line##"$key":*([[:space:]])}"
Copy link
Owner

@postmodern postmodern Feb 22, 2023

Choose a reason for hiding this comment

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

Are quotes really needed within bash variable substitutions?

var1="foo bar"
var2="foo bar baz"
echo "${var2#$var1 }"

Copy link
Author

Choose a reason for hiding this comment

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

I don't think so, but shellcheck disagrees :D

Copy link
Owner

Choose a reason for hiding this comment

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

Wait, which version of ShellCheck are you using? I'm on 0.7.2, which might be older than your version, which might explain why I'm not seeing those warnings?

Copy link
Author

Choose a reason for hiding this comment

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

Mine is 0.8.0 :D

ruby_dependencies=()
while IFS='' read -r line; do
ruby_dependencies+=("$line")
done < <(fetch "$ruby/$file" "$package_manager" || return $?)
Copy link
Owner

Choose a reason for hiding this comment

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

We actually need the implicit shell-splitting here. fetch "$ruby/$file" "$package_manager" will read the dependencies.txt file and return the apt: pkg1 pkg2 ... line that matches the "$package_manager", but without the apt: prefix. It is then implicitly splatted into an Array of individual package names. Reading each line would cause ruby_dependencies to become a singleton Array that contains the whole line (ex: ("pkg1 pkg2 pkg3")).

Copy link
Author

Choose a reason for hiding this comment

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

Hmm, agree. I just followed (blindly) what Shellcheck WIKI was proposing :D

local missing_pkgs=($(pacman -T "$@"))
local missing_pkgs=()
while IFS='' read -r line; do
missing_pkgs+=("$line")
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not a Arch user so I can't verify this, can you double check if pacman -T outputs the missing packages one per-line or a single-line with the package names separated by spaces?
https://archlinux.org/pacman/pacman.8.html

Copy link
Author

Choose a reason for hiding this comment

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

Don't have Arch installed, either. I'm just maintaining Gentoo ebuild in ::guru overlay, and currently I simply disable make check :D

Copy link
Owner

Choose a reason for hiding this comment

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

Wait, I forgot I can check using the archlinux docker image.

@postmodern
Copy link
Owner

postmodern commented Feb 22, 2023

Looking at some of the ShellCheck errors, I think SC2155 might be wrong here:

In share/ruby-install/checksums.sh line 33:
	local output="$(grep "  $file" "$checksums")"
              ^----^ SC2155: Declare and assign separately to avoid masking return values.


In share/ruby-install/checksums.sh line 58:
	local output="$($program "$file")"
              ^----^ SC2155: Declare and assign separately to avoid masking return values.


In share/ruby-install/checksums.sh line 74:
	local actual_checksum="$(compute_checksum "$algorithm" "$file")"
              ^-------------^ SC2155: Declare and assign separately to avoid masking return values.


In share/ruby-install/functions.sh line 22:
	ruby_dependencies=($(fetch "$ruby/$file" "$package_manager" || return $?))
                           ^-- SC2207: Prefer mapfile or read -a to split command output (or quote to avoid splitting).


In share/ruby-install/package_manager.sh line 32:
			local brew_owner="$(/usr/bin/stat -f %Su "$(command -v brew)")"
                              ^--------^ SC2155: Declare and assign separately to avoid masking return values.


In share/ruby-install/package_manager.sh line 43:
			local missing_pkgs=($(pacman -T "$@"))
                                            ^---------------^ SC2207: Prefer mapfile or read -a to split command output (or quote to avoid splitting).


In share/ruby-install/ruby-install.sh line 275:
	local fully_qualified_version="$(lookup_ruby_version "$ruby" "$ruby_version")"
              ^---------------------^ SC2155: Declare and assign separately to avoid masking return values.

When you put a command inside of $() the shell will implicitly ignore any returned error codes since it only cares about the output of the command. We might be able to just exclude SC2155 as well. Or we could rewrite all of those spots to use while IFS='' ... so that command being read could fail and also cause the while to fail as well.

@ixti
Copy link
Author

ixti commented Feb 23, 2023

I'm fine if those checks will be disabled (globally or locally via comments). Just went the lazy approach by doing what SC proposes and making sure tests pass after that on my machine :D

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.

make check is failing
2 participants