shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

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

Open moonfruit opened this issue 1 year ago • 9 comments

For new checks and feature suggestions

  • [x] https://www.shellcheck.net/ (i.e. the latest commit) currently gives no useful warnings about this
  • [x] I searched through https://github.com/koalaman/shellcheck/issues and didn't find anything related

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.

moonfruit avatar Apr 24 '24 03:04 moonfruit

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 

brother avatar Apr 24 '24 08:04 brother

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 avatar Apr 24 '24 09:04 moonfruit

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.

moonfruit avatar Apr 24 '24 09:04 moonfruit

maybe that should stop doing such things then?

brother avatar Apr 24 '24 15:04 brother

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

moonfruit avatar Apr 25 '24 01:04 moonfruit

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.

moonfruit avatar Apr 25 '24 01:04 moonfruit

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.

brother avatar Apr 25 '24 06:04 brother

@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).

ale5000-git avatar Apr 26 '24 01:04 ale5000-git

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

moonfruit avatar Apr 26 '24 01:04 moonfruit