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

RFC: Should SC2312 warn on testing pipelines? #3025

Open
4 tasks done
hseg opened this issue Jul 17, 2024 · 1 comment
Open
4 tasks done

RFC: Should SC2312 warn on testing pipelines? #3025

hseg opened this issue Jul 17, 2024 · 1 comment

Comments

@hseg
Copy link

hseg commented Jul 17, 2024

For bugs

  • Rule Id (if any, e.g. SC1000): SC2312
  • My shellcheck version (shellcheck --version or "online"): 0.10.0 and online
  • The rule's wiki page does not already cover this (e.g. https://shellcheck.net/wiki/SC2086)
  • I tried on https://www.shellcheck.net/ and verified that this is still a problem on the latest commit

For new checks and feature suggestions

Here's a snippet or screenshot that shows the problem:

#!/bin/bash
# shellcheck enable=all

if false | grep hello; then echo failure; fi

Here's what shellcheck currently says:

Line 4:
if false | grep hello; then echo failure; fi
   ^-- SC2312 (info): Consider invoking this command separately to avoid masking its return value (or use '|| true' to ignore).

Here's what I wanted or expected to see:

Either nothing (after all, the fact that I'm testing the result of the pipeline indicates I am capturing the return value, albeit only of the last command), or a tailored warning indicating that the test as written will make me blind to which command failed.

Judging by the wiki page, the intent of the warning is more to flag that command substitutions can fail and leave null strings in their place. If this is the intent, then the wording of the warning doesn't communicate it efectively.

@hseg
Copy link
Author

hseg commented Jul 17, 2024

In fact, this fires on any usage of a pipeline:

#!/bin/bash
# shellcheck enable=all

echo "hello world" | grep hello | grep world

I wouldn't consider the "corrected" script to be idiomatic bash, especially considering PIPESTATUS exists. The only case I would consider perhaps leaving intact is

f() { echo "hello world" | grep hello | grep world; }

where the user has left the return code to implicitly be that of the final command, which might be surprising -- perhaps warn to set -o pipefail instead? Though I'm not sure how to make that setting scope only to the function, and am not sure we want to advise users to use it globally.

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

No branches or pull requests

1 participant