-
Notifications
You must be signed in to change notification settings - Fork 27
linting: add shellcheck for shell script lint #756
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
base: main
Are you sure you want to change the base?
Conversation
3b2e198 to
ad1f9e0
Compare
e61a5c5 to
8635352
Compare
scripts/format
Outdated
| exit 1 | ||
| fi | ||
| shfmt -s -w -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/)) | ||
| shfmt -s -w -l -i 2 -ci -fn "$(shfmt -f "$(git grep -l '' :/)")" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not a shell expert, but this looks wrong? Do we really want to pass the list of files as a single quoted argument to shfmt here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fails locally for me. How come the CI does not catch this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey Hanno, I just spent some time looking at this - it turns out that the command
shfmt -s -w -l -i 2 -ci -fn "$(shfmt -f "$(git grep -l '' :/)")"
has the following odd behavior:
- It fails loudly (in the sense that it was printing an incorrect list of files which needed to be formatted to the console) - I missed this when rerunning my lint script.
- In the context of
checkerr, since we aren't calling the functions with the redirection of stderr to stdout, it receives an empty$out$asshfmtwas writing those logs to stderr. - Since
$outwas empty and the return value was 1,successwould still remain true and it was appearing as if the linting had succeeded.
In general it would probably be wise to have checkerr fail if the return value is anything other than 0. I can implement this change if you'd like.
I've fixed the syntax and now CI should be passing.
hanno-becker
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you @L-series, in general I think linting the shell scripts is a great idea -- esp. seeing that at least I'm not a shell expert.
However, it looks like some of the quoting has broken scripts/lint, at least the Lint Shell command. Can you have a look, both at the issue itself and why the CI did not catch it?
|
@hanno-becker Hello, please see my comment above regarding what the issue was with the I'm adding a variable which tracks the shells scripts which exist across the project and I pass that to both |
054002b to
67a36aa
Compare
scripts/format
Outdated
| # Find all scripts in the repo | ||
| ALL_FILES=$(git grep -l '' :/) | ||
| SHELL_SCRIPTS=$(echo "$ALL_FILES" | xargs shfmt -f) | ||
| shfmt -s -l -i 2 -ci -fn $SHELL_SCRIPTS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why have we lost -w?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this work if scripts ever contain spaces in their file names?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added back the missing -w and fixed the command to use xargs as per the lint script. I've checked and this will not work for files which contain spaces in the filename, however the original implementation also suffers from this issue. The output of renaming lint to lint new and calling shfmt on it:
shfmt -s -w -l -i 2 -ci -fn $(shfmt -f $(git grep -l '' :/))
lstat scripts/lint: no such file or directory
lstat new: no such file or directory
I'll spend some time tomorrow thinking on how to resolve this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could use a loop of the sort to bypass this issue:
git grep -l '' :/ | while IFS= read -r file; do
shfmt -f "$file"
done | while IFS= read -r shellfile; do
shfmt -s -w -l -i 2 -ci -fn "$shellfile"
done
a474a92 to
c2209ca
Compare
Add linting for shell scripts in the scipts directory as well as any *.sh file accross the project using shellcheck. Signed-off-by: Andreas Hatziiliou <andreas.hatziiliou@savoirfairelinux.com>
Fix errors brought up by the linter. Signed-off-by: Andreas Hatziiliou <andreas.hatziiliou@savoirfairelinux.com>
Add linting for shell scripts in the scipts directory as well as any
*.sh file accross the project using shellcheck.