profile
viewpoint

Ask questionsSC1090/SC1091: sourcing should find files relative to script

When using the directive source=foobar.sh, then it seems to look up the source relative to the path shellcheck is being run from. For example, I have the following scripts: foo.sh and foobar.sh in the same directory (for example bar/baz from the project root).

Suppose there is the following line in foo.sh:

source "${0%/*}/foobar.sh"

I will get a SC1090 error because shellcheck is unable to resolve the path. I have to do this:

# shellcheck source bar/baz/foobar.sh
source "${0%/*}/foobar.sh"

This works, but only if I run the checker from the project root (either by specifying foo/bar/foobar.sh as an input file, or with -x), but not if I run it from anywhere else. There should be a way to tell shellcheck to look for a file relative to the checked file's location, not where shellcheck is run. For example, this way:

# shellcheck source ./foobar.sh
source "${0%/*}/foobar.sh"
koalaman/shellcheck

Answer questions iyerusad

I recently added shellcheck to my workflow within VS Code (via this extension). As such, shellcheck is called per file. Very helpful in IDE, I tend to jump between several languages/syntax in any given day - Thank you.

In my limited view, I'm thinking #shellcheck source= directive should attempt to be relative by default:

  1. If I'm specifying a shellcheck source directive, it means higher up helpers (such as recent https://github.com/koalaman/shellcheck/commit/301705edea829c5153c43bf52215d09b5e00d182) have been unsuccessful (or are undefined)
  2. shellcheck executable location not likely same as script being checked.

My particular instance of SC1090 is (where dotfiles is a repo):

if [ -f "$HOME/dotfiles/1" ];then
  . "$HOME/dotfiles/1"
fi

It would be nice if could handle case of $HOME - I rather avoid helper directives in code unless explicitly breaking a rule for a really good reason.

useful!

Related questions

SC2181 suggests to check exit code directly when I check for several possible values hot 1
source:https://uonfu.com/
Github User Rank List