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

Add shellcheck support and robustify present shell scripts #5294

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

yarikoptic
Copy link

## 📑 Summary

https://www.shellcheck.net/ is great to robustify shell scripts, in particular make them work correctly when paths with spaces in them are involved somehow.

TODOs

  • decide what to do about args=${@:2} since that semantic is unclear. ATM I just ignored that line while fixing the rest. See beed5fd for the description of the gotcha there. One solution is to not do anything and keep that TODO. Another -- do fix it: I think it was intended to be used as an array, hence should become args=( "${@:2}" ) but then uses should be adjusted accordingly. Please confirm that it was intended use.

Make sure you

=== Do not change lines below ===
{
 "chain": [
  "f724281d8f3cd1dc4f3538186d91fdf0bc8cf997"
 ],
 "cmd": "shellcheck -f diff run | patch -p1",
 "exit": 0,
 "extra_inputs": [],
 "inputs": [],
 "outputs": [],
 "pwd": "."
}
^^^ Do not change lines above ^^^
It is not clear from the code what was envisioned "exactly". But as given it is
analogous to "${*:2}" if what was really wanted is to get a single string with
all args from 2nd concatenated.

Here are the helpers used here:

    ❯ cat /tmp/show
    #!/bin/bash

    args=${@:2}
    echo "$args"
    /home/yoh/bin/printargs "$args"

    qargs="${@:2}"  # just quoted to pacify shellcheck
    /home/yoh/bin/printargs "$qargs"

    qargs="${*:2}"  # quoted and * instead of @ to pacify shellcheck
    /home/yoh/bin/printargs "$qargs"

    corrected_args=( "${@:2}" )
    echo "$corrected_args"
    /home/yoh/bin/printargs "$corrected_args"
    # as array
    /home/yoh/bin/printargs "${corrected_args[@]}"
    changes on filesystem:
     run | 6 ++++++

    ❯ cat ~/bin/printargs
    #!/bin/bash

    for a in "$@"; do
    echo ">$a<"
    done

which would produce following output

    ❯ bash -x /tmp/show 1 "2 2" 3 4 5
    + args='2 2 3 4 5'
    + echo '2 2 3 4 5'
    2 2 3 4 5
    + /home/yoh/bin/printargs '2 2 3 4 5'
    >2 2 3 4 5<
    + qargs='2 2 3 4 5'
    + /home/yoh/bin/printargs '2 2 3 4 5'
    >2 2 3 4 5<
    + qargs='2 2 3 4 5'
    + /home/yoh/bin/printargs '2 2 3 4 5'
    >2 2 3 4 5<
    + corrected_args=("${@:2}")
    + echo '2 2'
    2 2
    + /home/yoh/bin/printargs '2 2'
    >2 2<
    + /home/yoh/bin/printargs '2 2' 3 4 5
    >2 2<
    >3<
    >4<
    >5<

and shellcheck would report following issues on the helper:

	❯ shellcheck /tmp/show

	In /tmp/show line 3:
	args=${@:2}
		 ^----^ SC2124 (warning): Assigning an array to a string! Assign as array, or use * instead of @ to concatenate.

	In /tmp/show line 7:
	qargs="${@:2}"  # just quoted to pacify shellcheck
		  ^------^ SC2124 (warning): Assigning an array to a string! Assign as array, or use * instead of @ to concatenate.

	In /tmp/show line 14:
	echo "$corrected_args"
		  ^-------------^ SC2128 (warning): Expanding an array without an index only gives the first element.

	In /tmp/show line 15:
	/home/yoh/bin/printargs "$corrected_args"
							 ^-------------^ SC2128 (warning): Expanding an array without an index only gives the first element.

	For more information:
	  https://www.shellcheck.net/wiki/SC2124 -- Assigning an array to a string! A...
	  https://www.shellcheck.net/wiki/SC2128 -- Expanding an array without an ind...
Copy link

netlify bot commented Feb 16, 2024

Deploy Preview for mermaid-js ready!

Name Link
🔨 Latest commit b477d0e
🔍 Latest deploy log https://app.netlify.com/sites/mermaid-js/deploys/65cf973f547dfa0008ea16d2
😎 Deploy Preview https://deploy-preview-5294--mermaid-js.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

run
$(bold ./$name pnpm build) # Prepare it for production
$(bold ./"$name" pnpm vitest) # Run watcher for unit tests
$(bold ./"$name" cypress) # Run integration tests (after starting dev server)
$(bold ./"$name" pnpm build) # Prepare it for production
Copy link
Author

Choose a reason for hiding this comment

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

although looks odd, it is the only way to make it robust if script would ever be renamed to include space in the name. Alternatively that check could be skipped indeed.

Copy link

codecov bot commented Feb 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (6be91bc) 43.20% compared to head (b477d0e) 79.15%.

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##           develop    #5294       +/-   ##
============================================
+ Coverage    43.20%   79.15%   +35.94%     
============================================
  Files           23      175      +152     
  Lines         5115    14514     +9399     
  Branches        23      866      +843     
============================================
+ Hits          2210    11488     +9278     
+ Misses        2904     2823       -81     
- Partials         1      203      +202     
Flag Coverage Δ
e2e 84.72% <ø> (?)
unit 43.20% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

see 170 files with indirect coverage changes

Copy link
Member

@aloisklink aloisklink left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution :)

Your PR looks mostly great to me; however, I think adding quotes that "$args" (e.g. with quotes) instead of $args in the run script might break some behaviour. Can we change this to an array, like you recommend?

run
Comment on lines +13 to 19
# TODO: fix this one to become spaces tollerant etc
# If to be used as an array, make it
# args=( "${@:2}" )
# If to concatenated into a single string and thus loosing ability to pass args with spaces
# args="${*:2}"
# shellcheck disable=SC2124
args=${@:2}
Copy link
Member

Choose a reason for hiding this comment

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

todo(blocking): I'm fairly certain quoting "$args" will break this script.

Do you think it makes sense to change it to something like:

diff --git a/run b/run
index f01302c43..ecb392e58 100755
--- a/run
+++ b/run
@@ -10,18 +10,12 @@ red()           { ansi 31 "$@"; }
 
 name=$(basename "$0")
 command=$1
-# TODO: fix this one to become spaces tollerant etc
-#       If to be used as an array, make it
-# args=( "${@:2}" )
-#       If to concatenated into a single string and thus loosing ability to pass args with spaces
-# args="${*:2}"
-# shellcheck disable=SC2124
-args=${@:2}
+args=( "${@:2}" )
 
 case $command in
 
 build)
-docker compose build "$args"
+docker compose build "${args[@]}"
 ;;
 
 sh)
@@ -29,7 +23,7 @@ $RUN mermaid sh
 ;;
 
 pnpm)
-$RUN mermaid sh -c "pnpm $args"
+$RUN mermaid pnpm "${args[@]}"
 ;;
 
 dev)
@@ -41,7 +35,7 @@ $RUN --service-ports mermaid sh -c "pnpm run --filter mermaid docs:dev:docker"
 ;;
 
 cypress)
-$RUN cypress "$args"
+$RUN cypress "${args[@]}"
 ;;
 
 help|"")

I'll be honest, I don't really use this run script, but a quick test of the above changes seemed to work.

Comment on lines +14 to +26
shellcheck:
name: Check shell scripts
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4
- name: Install dependencies
run: |
sudo apt update && sudo apt install -y shellcheck
- name: shellcheck
run: |
git grep -l '^#\( *shellcheck \|!\(/bin/\|/usr/bin/env \)\(sh\|bash\|dash\|ksh\)\)' | xargs shellcheck
Copy link
Member

Choose a reason for hiding this comment

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

Love this!

Just to tidy up, do you think it makes sense to move this into the .github/workflows/lint.yml with the other lint job?

You'll have to move the permissions: contents: read into the jobs.shellcheck too.

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

2 participants