Programatic sourcing of dependencies

Hi!

I am having a little trouble with a particular bash project. Typically each script sources its dependencies by looping though the files:

function source_dependencies() {
  for file in ../../utils/*; do
    source "${file}"
  done
}

In any script which does this, I cannot go to the defention of any function contained within these files.

For example if functionX is in one of those scripts and I have a script like:

#!/bin/bash

function source_dependencies() {
  for file in ../../utils/*; do
    source "${file}"
  done
}

source_dependencies
functionX

I cannot click through to the definition.

Would this be a valid feature request?

Hi @mashawan,
you’re right that this kind of project is still a bit hard to use with the current version.

Feature proposal

I’ve been thinking about this for a while now. Larger shell projects, e.g. bashdb or the letsencrypt tools, are also using this approach. I’m planning to improve this for 2.0, but haven’t decided yet about the best approach.
Possible solutions are:

  • A directive like #bashsupport source=../utils/*.bash in a script. You’d have to put this into every script, which uses declarations of utils/*.bash. This would pollute a lot of files, esp. if sourced files use the utils. Using # shellcheck source= isn’t possible, because it’s not supporting wildcards.
  • something like .bashsupportrc in the filetree. Such a file would define options for the directory and its subtree. If could be stored at the top-level of your project and could look like this:
    source: ./utils/*.bash
    
    or
    source-dir: ./utils
    

So far, I’m leaning towards the file-based solution. Are you aware of any other, common solution which could/should be used?

Workaround

Here’s a workaround for the current version, which might be possible for your project.
You could use multiple shellcheck source directive in a script. If put right after the shebang they’re valid for all of the file. This might be a bit hard to use for large scripts or lots of utility files.

#!/bin/bash
# shellcheck source=../utils/math.bash
# shellcheck source=../utils/message.bash

function source_dependencies() {
  for file in …/…/utils/*; do
    source “${file}”
  done
}

source_dependencies
functionX

Thanks for your reply. I am not really a bash expert so IU’m leaning on this plugin a lot to help me. I would vote for the file based solution too. But for now the shell check workaround gives me jump to declaration. Not sure if I can check these in as we are not all using IntelliJ + Bash Pro, but it gets me moving at least. Thanks!