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

[SC3028] When using if [ ! -z "${BASH_VERSION+x}" ] don't warn of using bash features like set -o pipefail or BASH_SOURCE #3111

Open
4 tasks done
SamuelMarks opened this issue Dec 31, 2024 · 4 comments

Comments

@SamuelMarks
Copy link

SamuelMarks commented Dec 31, 2024

For bugs

For new checks and feature suggestions

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

#!/bin/sh

if [ -n "${BASH_VERSION}" ]; then
  # shellcheck disable=SC3028 disable=SC3054
  this_file="${BASH_SOURCE[0]}"
elif [ -n "${ZSH_VERSION}" ]; then
  # shellcheck disable=SC2296
  this_file="${(%):-%x}"
else
  this_file="${0}"
fi

Here's what shellcheck currently says:

When I remove the comments it says:

In POSIX sh, BASH_SOURCE is undefined.
See SC3028.
In POSIX sh, array references are undefined.
See SC3054.

Here's what I wanted or expected to see:

No error

FYI: Similar errors occur with pipefail:

In POSIX sh, set option pipefail is undefined.
See SC3040.

@brother
Copy link
Collaborator

brother commented Jan 7, 2025

Just to make it clear.
The example should be if [ -n "${BASH_VERSION}" ]; then? The title makes me believe it should be something else.

@SamuelMarks
Copy link
Author

@brother This is my actual project with relevant line highlighted https://github.com/SamuelMarks/deploy-sh/blob/7b03f64/install.sh#L6

As you can see I've had to include a large number of # shellcheck disable 🤦

@brother
Copy link
Collaborator

brother commented Jan 8, 2025

That doesn't answer my question.
The title of the issue and the code example differs. It's hard to follow.
bild

I don't really see how shellcheck could behave different. You asked it to use sh/posix mode and then check one variable and want another one to be included too?

@SamuelMarks SamuelMarks changed the title [SC3028] In a if [ -n BASH_SOURCE ] block don't warn of BASH_SOURCE being undefined [SC3028] When using if [ ! -z "${BASH_VERSION+x}" ] don't warn of using bash features like set -o pipefail or BASH_SOURCE Jan 8, 2025
@SamuelMarks
Copy link
Author

@brother Removed the ambiguity from the issue title.

Basically if I have an if check to see if I'm in Bash then I want to be able to use Bash features without using # shellcheck disable everywhere.

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

2 participants