1

What is the best practice to source a local bash file? I get the following shellcheck error on my bash script as it can't follow the path provided in the source:

Code snippet that gives the formatting error:

source ../<local-bash-file>.sh

Shellcheck error:

SC1091: Not following: ../<local-bash-file>.sh was not specified as input (see shellcheck -x).

It is important to note that the build trigger that gives this error doesn't use the shellcheck -x. How should I reformat my source line to pass this check?

Tried adding absolute and relative path

Usama Ijaz
  • 31
  • 2
  • 3
    it's not clear (to me) if you're trying to address an issue with shellcheck, or you're trying to address an issue with your (build trigger) code; if the latter then consider updating the question with a reproduction of your actual (build trigger) code and the exact message provided – markp-fuso Aug 29 '23 at 19:32
  • The message is pretty clear, right? You need to tell shellcheck it has permission to read other source files before it will do so. – Charles Duffy Aug 29 '23 at 20:00
  • What is a **local** file? For bash, it does not matter whether the file is stored on your local hard disk or on a mounted volume. Aside from this, I don't see any error in your `source` command. – user1934428 Aug 30 '23 at 08:48

1 Answers1

4

The shellcheck wiki has more information on all shellcheck messages.

You have a few options:

  • Tell shellcheck to follow all sources in your code. In your case, you need to modify how you call shellcheck and pass the -x flag. This is probably the easiest way and will work for all shellcheck calls in your build.

  • Specify external-sources=true in your shellcheck configuration file, if you use one. This may be the preferred way when running shellcheck locally, but in a CI environment you may want to be more explicit with the -x flag above.

  • Hard-code the file name you source, by adding # shellcheck source=somefile on the line above the source. Make sure you pass the same file name. This requires touching all sources (and . somefile.sh) lines and may be a bit labor intensive, but you're very explicit about what gets checked, and it's the most fine-grained approach (in case you care about this).

  • Suppress the warnings, one at a time: Add # shellcheck disable=SC1091 on the line before the source. I'd try to avoid this, as the sourced code won't get checked here. It may be necessary to silence this when you dynamically build the file name of the file you want to source.

  • Suppress the warning everywhere: call shellcheck with -e SC1091 to exclude the given warning. This doesn't rely on the configuration file, but you may have to add it to multiple shellcheck calls.

Robert
  • 7,394
  • 40
  • 45
  • 64
  • 1
    To clarify, this is a sandboxing feature and `# shellcheck source=somefile` will not allow ShellCheck to read `somefile` (unless already specifying it as a file to check, or using `-x`/`external-sources=true`). However, you can use `# shellcheck source=/dev/null` to ignore the file completely. – that other guy Aug 31 '23 at 19:36