Skip to content

Conversation

@L-series
Copy link
Contributor

@L-series L-series commented Dec 1, 2025

Add linting for shell scripts in the scipts directory as well as any
*.sh file accross the project using shellcheck.

@L-series L-series force-pushed the shellcheck branch 2 times, most recently from 3b2e198 to ad1f9e0 Compare December 1, 2025 01:49
@L-series L-series marked this pull request as ready for review December 1, 2025 01:59
@L-series L-series requested a review from a team as a code owner December 1, 2025 01:59
@L-series L-series force-pushed the shellcheck branch 2 times, most recently from e61a5c5 to 8635352 Compare December 1, 2025 15:15
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 '' :/)")"
Copy link
Contributor

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?

Copy link
Contributor

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?

Copy link
Contributor Author

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:

  1. 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.
  2. In the context of checkerr, since we aren't calling the functions with the redirection of stderr to stdout, it receives an empty $out$ as shfmt was writing those logs to stderr.
  3. Since $out was empty and the return value was 1, success would 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.

Copy link
Contributor

@hanno-becker hanno-becker left a 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?

@L-series
Copy link
Contributor Author

L-series commented Dec 1, 2025

@hanno-becker Hello, please see my comment above regarding what the issue was with the shfmt syntax. I've changed that syntax and I simplified the logic behind the shellcheck implementation.

I'm adding a variable which tracks the shells scripts which exist across the project and I pass that to both shfmt and shellcheck instead of recomputing it for shellcheck.

@L-series L-series force-pushed the shellcheck branch 3 times, most recently from 054002b to 67a36aa Compare December 1, 2025 20:01
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
Copy link
Contributor

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?

Copy link
Contributor

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?

Copy link
Contributor Author

@L-series L-series Dec 9, 2025

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.

Copy link
Contributor Author

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

@mkannwischer mkannwischer changed the title linting: add shelcheck for shell script lint linting: add shellcheck for shell script lint Dec 3, 2025
@L-series L-series force-pushed the shellcheck branch 4 times, most recently from a474a92 to c2209ca Compare December 9, 2025 02:30
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>
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.

3 participants