koalaman / shellcheck

ShellCheck, a static analysis tool for shell scripts
https://www.shellcheck.net
GNU General Public License v3.0
35.55k stars 1.74k forks source link

Feature Request: Provide an option to specify the fallback shell type #2971

Open moonfruit opened 2 months ago

moonfruit commented 2 months ago

For new checks and feature suggestions

Description

Some tools like bash-language-server detect the shell type themselves and add the option --shell=SHELLNAME to shellcheck. But this option will overwrite shellcheck directive for type in file or .shellcheckrc.

I think a new option should be provided to specify a fallback shell type.

brother commented 2 months ago

Not sure this is a shellcheck problem really.

Would it be possible to add the shell directive at the top of the file?

brother ~$ cat /tmp/test.sh 
#!/bin/sh

myvar=(1 2 3)

echo "${myvar[1]}"
brother ~$ shellcheck --norc /tmp/test.sh 

In /tmp/test.sh line 3:
myvar=(1 2 3)
      ^-----^ SC3030 (warning): In POSIX sh, arrays are undefined.

In /tmp/test.sh line 5:
echo "${myvar[1]}"
      ^---------^ SC3054 (warning): In POSIX sh, array references are undefined.

For more information:
  https://www.shellcheck.net/wiki/SC3030 -- In POSIX sh, arrays are undefined.
  https://www.shellcheck.net/wiki/SC3054 -- In POSIX sh, array references are...
brother ~$ shellcheck --norc --shell=bash /tmp/test.sh 
brother ~$ cat /tmp/test.sh 
#!/bin/sh
# shellcheck shell=bash

myvar=(1 2 3)

echo "${myvar[1]}"
brother ~$ shellcheck --norc --shell=bash /tmp/test.sh 
brother ~$ shellcheck --norc /tmp/test.sh 
moonfruit commented 2 months ago

For example:

test.sh:

#!/bin/sh
# shellcheck shell=bash
echo -e "abc"

And check with shellcheck

$ shellcheck test.sh
$ shellcheck --shell=sh test.sh

In test.sh line 3:
echo -e "abc"
     ^-- SC3037 (warning): In POSIX sh, echo flags are undefined.

For more information:
  https://www.shellcheck.net/wiki/SC3037 -- In POSIX sh, echo flags are undef...

You can find that the command line option override the shell directive defined in the file.

moonfruit commented 2 months ago

bash-language-server always sets --shell= to what it thinks the shell is. I think it should set it to a fallback shell type to allow # shellcheck shell=bash to work. But shellcheck does not have this function.

brother commented 2 months ago

maybe that should stop doing such things then?

moonfruit commented 2 months ago

Well, now that you've said that, all I can do now is post this question to bash-language-server.

moonfruit commented 2 months ago

It seems to me that a small change on both sides would be the feature that still works. Otherwise, an external tool may be needed to parse the shellcheck directive to solve the problem more perfectly.

brother commented 2 months ago

I don't think this need to get all crazy but it looks like bash-lang-server is (lack of words) abusing the shell directive. We developers and and end consumers can set those options ourselves if needed.

ale5000-git commented 2 months ago

@moonfruit My opinion: shellcheck already detect the shell by itself in multiple ways so there isn't any need for the server to suggest it (even more so if the suggestions are wrong).

moonfruit commented 2 months ago

I'm not the maintainer of bash-language-server, I can't decide what it will do. I just made a suggestion.