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 some shellcheck warnings #6621

Merged
merged 12 commits into from
Sep 3, 2024
Merged

fix some shellcheck warnings #6621

merged 12 commits into from
Sep 3, 2024

Conversation

vnherdeiro
Copy link
Contributor

@vnherdeiro vnherdeiro commented Aug 24, 2024

Contributes to #6498

Following this attempt #6548 at contributing to LightGBM and @jameslamb guidance I am now trying to help with fixing the shellcheck warnings for all the bash scripts inside the package (at least I think so).

Screenshot to show that the warnings are gone in my local environment:
Screenshot 2024-08-24 at 21 05 13

Fixing https://www.shellcheck.net/wiki/SC1091 errors (source activate $SOME_CONDA_ENV) by pointing at at /dev/null is quite unsatisfactory but I did not manage to come up with something better.

Welcoming any feedback!

@vnherdeiro
Copy link
Contributor Author

@microsoft-github-policy-service agree

build-python.sh Outdated
@@ -352,7 +354,7 @@ if test "${BUILD_WHEEL}" = true; then
python -m build \
--wheel \
--outdir ../dist \
${BUILD_ARGS} \
$BUILD_ARGS \
Copy link
Contributor Author

@vnherdeiro vnherdeiro Aug 25, 2024

Choose a reason for hiding this comment

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

This raises shellcheck info level SC2086 (info): Double quote to prevent globbing and word splitting. but adding the double quotes breaks the command (BUILD_ARGS is empty) I don't understand the root cause so I leave it as it. This is repeated twice in the file.

@vnherdeiro
Copy link
Contributor Author

I had to revert some of the changes due to breaking commands. Now we have 3 SC2086 (info): Double quote to prevent globbing and word splitting. in build-python.sh. Pipelines seem to be passing (except the mac one, let me know if it's a regression please).

@jameslamb jameslamb changed the title Fixing all spellcheck warnings Fixing all shellcheck warnings Aug 26, 2024
Copy link
Collaborator

@jameslamb jameslamb 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 help, and for taking the time to contribute to LightGBM!

A few comments:

1. The tool is called shellcheck, not spellcheck

I've changed that in the description and title.

2. Please limit this to just a few files

I do really appreciate your help here, but think this PR is too large to review. As it says in #6498

It is not necessary to fix all of the problems in a single pull request.

There are some changes in this PR that are more controversial and require a bit more discussion, and I don't want all of this work to be blocked on those discussions... let's please reduce this to a smaller set of files and work together on fixing all the shellcheck errors in them. Once this PR is merged, hopefully you'll be able to help with the other shellcheck errors in a follow-up PR.

Pease remove the changes to the following files:

  • .ci/test.sh
  • .ci/test-r-package.sh
  • .ci/setup.sh
  • build-python.sh
  • build-cran-package.sh

Since you made this PR from a branch also called master on your fork (please do not do that in the future, as I asked in #6548 (comment)), the easiest way to do that is to just go to https://github.com/microsoft/LightGBM, copy the file content, paste it in one your local clone, commit the changes, and push.

I've left suggestions on the changes to the other files.

3. Please use linking here on GitHub to connect the discussions

I've added a reference to #6498 in the description. In the future, when you contribute here and the work is related to other discussions, please do link them.

.ci/check-python-dists.sh Outdated Show resolved Hide resolved
docs/build-docs.sh Outdated Show resolved Hide resolved
docs/build-docs.sh Outdated Show resolved Hide resolved
docs/build-docs.sh Outdated Show resolved Hide resolved
@@ -31,17 +31,17 @@ pr_branch=$3
runs=$(
curl -sL \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $SECRETS_WORKFLOW" \
-H "Authorization: token ${SECRETS_WORKFLOW}" \
Copy link
Collaborator

Choose a reason for hiding this comment

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

Was this intended to address a specific shellcheck error, or is it just something you're proposing for stylistic similarity? I don't mind this change (I generally support wrapping interpolation in ${} like this), just trying to understand the change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. This one was not raised by shellcheck. I was uniformizing the "binding" (interpolation in your words) with the rest of the file. Shall we agree on a way of binding/interpolating?

Copy link
Collaborator

Choose a reason for hiding this comment

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

ok just checking. This is fine, let's leave it (I do prefer the style with ${}) but please don't touch other lines that aren't directly related to the purpose of the PR.

.ci/check-python-dists.sh Outdated Show resolved Hide resolved
@vnherdeiro
Copy link
Contributor Author

vnherdeiro commented Aug 27, 2024

@jameslamb Thanks for the extensive review. I have noted all of your points. Apologies for pushing from master, I did not notice I had forgotten to branch out beforehand. I can close this PR and open it from a feature branch on my clone.

I have reversed the files you have asked me too, let's keep the changes for another PR. It think I have adressed all of your points so far.

@jameslamb jameslamb changed the title Fixing all shellcheck warnings fix some shellcheck warnings Aug 28, 2024
Copy link
Collaborator

@jameslamb jameslamb left a comment

Choose a reason for hiding this comment

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

Thanks very much, these changes look good to me.

It won't matter because we squash-merge PRs here, but just so you know... most of the commits you pushed here were not correctly tied to your GitHub account. You can see #3607 (comment) and the things it links to to learn more about that. If you plan to continue working on projects here on GitHub, I recommend adjusting your .gitconfig to ensure your commit are tied to your GitHub account.

docs/build-docs.sh Outdated Show resolved Hide resolved
@@ -31,17 +31,17 @@ pr_branch=$3
runs=$(
curl -sL \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $SECRETS_WORKFLOW" \
-H "Authorization: token ${SECRETS_WORKFLOW}" \
Copy link
Collaborator

Choose a reason for hiding this comment

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

ok just checking. This is fine, let's leave it (I do prefer the style with ${}) but please don't touch other lines that aren't directly related to the purpose of the PR.

@vnherdeiro
Copy link
Contributor Author

vnherdeiro commented Aug 28, 2024

Thanks for the approval @jameslamb

Copy link
Collaborator

@jameslamb jameslamb left a comment

Choose a reason for hiding this comment

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

I re-reviewed the changes you pushed after my last approval and found an issue with them. Please see the suggestion I left here.

.ci/lint-cpp.sh Outdated
find . -name CMakeLists.txt -o -path "./cmake/*.cmake" \
| grep -v external_libs
)
cmake_files=$(find . \( -name CMakeLists.txt -o -path "./cmake/*.cmake" \) -not -path "./external_libs/*" | tr '\n' ' ')
Copy link
Collaborator

Choose a reason for hiding this comment

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

What was the purpose of these changes that you pushed after I approved? It looks to me like they result in the files we care about all being ignored.

Ignoring file: ./cmake/IntegratedOpenCL.cmake ./cmake/Sanitizer.cmake ./cmake/modules/FindLibR.cmake ./CMakeLists.txt 

(build link)

If this was from that shellcheck warning about assigning the result of find to a variable... let's just use find -exec instead. Please try this:

find \
    . \
    -type f \
    \( -name CMakeLists.txt -o -name '*.cmake' \) \
    -not -path './external_libs/*' \
    -exec cmakelint \
        --linelength=120 \
        --filter=-convention/filename,-package/stdargs,-readability/wonkycase \
        {} \+

I tested this locally (on my mac) and found it worked well. This listed the expected files:

find \
    . \
    -type f \
    \( -name CMakeLists.txt -o -name '*.cmake' \) \
    -not -path './external_libs/*'
./CMakeLists.txt
./cmake/Sanitizer.cmake
./cmake/IntegratedOpenCL.cmake
./cmake/modules/FindLibR.cmake

And the full statement did raise warnings about CMake formatting when I changed some code in those scripts.

Copy link
Contributor Author

@vnherdeiro vnherdeiro Aug 31, 2024

Choose a reason for hiding this comment

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

@jameslamb First I did not mean to creep in a change after your approval, I am used to MR/PR settings in which a code change requires a re-approval. I changed the line because the cmakelint execution was failing the CI. I traced it to the file path arguments being passed with '\n' joins instead of single-whitespace. I changed the find + (negative) grep command for a more efficient find with a ignore path logic.

I missed the log line saying that the files were ignored, and I don't understand why it happens. I did think of uxing exec but I guessed the command would get quite long, hence less readable, and some one may want to print the value of $cmake_files for debugging purposes.

Now coming to the snipper you propose. I see you are changing the original -o -path "./cmake/*.cmake" for -o -name '*.cmake' and the file list passed to the cpp linting changes from

./CMakeLists.txt ./cmake/Sanitizer.cmake ./cmake/IntegratedOpenCL.cmake ./cmake/modules/FindLibR.cmake

to

./CMakeLists.txt ./cmake/Sanitizer.cmake ./cmake/IntegratedOpenCL.cmake ./cmake/modules/FindLibR.cmake ./build/CMakeFiles/_lightgbm.dir/DependInfo.cmake ./build/CMakeFiles/_lightgbm.dir/cmake_clean.cmake ./build/CMakeFiles/3.30.2/CMakeCXXCompiler.cmake ./build/CMakeFiles/3.30.2/CMakeCCompiler.cmake ./build/CMakeFiles/3.30.2/CMakeSystem.cmake ./build/CMakeFiles/lightgbm.dir/DependInfo.cmake ./build/CMakeFiles/lightgbm.dir/cmake_clean.cmake ./build/CMakeFiles/lightgbm_capi_objs.dir/DependInfo.cmake ./build/CMakeFiles/lightgbm_capi_objs.dir/cmake_clean.cmake ./build/CMakeFiles/Makefile.cmake ./build/CMakeFiles/lightgbm_objs.dir/DependInfo.cmake ./build/CMakeFiles/lightgbm_objs.dir/cmake_clean.cmake ./build/CMakeFiles/CMakeDirectoryInformation.cmake ./build/cmake_install.cmake

which causes the number of linting errors to jump from 0 to 65 locally. I am pushing your change with exec but using the old -o -path "./cmake/*.cmake" match pattern.

Copy link
Collaborator

Choose a reason for hiding this comment

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

ohhhhhhh thank you so much for the thorough explanation!

There shouldn't be a ./build/ directory in CI (because we don't actually run a CMake build in the lint CI task), but it is likely that there could be one lying around when you run this locally if you've also been doing other development in the project.

Keeping ./cmake/*.cmake for now is fine... this is how the linting here worked before (e.g. is not a change made by your PR) and all of the *.cmake files we currently want to lint do happen to be in that directory).

Copy link
Collaborator

@jameslamb jameslamb left a comment

Choose a reason for hiding this comment

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

thanks!

@jameslamb jameslamb merged commit 15ac2f7 into microsoft:master Sep 3, 2024
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants