bash-language-server
bash-language-server copied to clipboard
Handle SCRIPTDIR in `source-path`-directives
What is the problem this feature will solve?
The lsp currently handles constructs like
# foo/test.
# shellcheck source=bar/baz.sh
source $someDir/baz.sh
# shellcheck source-path=SCRIPTDIR
source ./bar/baz.sh
What's missing is paths containing SCRIPTDIR, i.e.
# shellcheck source=SCRIPTDIR/bar/baz.sh
source baz.sh
# shellcheck source-path=SCRIPTDIR/bar/
What is the feature you are proposing to solve the problem?
Shellcheck directives should handle SCRIPTDIR
in paths, e.g. SCRIPTDIR/foo.sh
or SCRIPTDIR/../someFolder/
What alternatives have you considered?
Changing the paths in the source statements, but the analyzed files will be copied to different locations depending on runtime factors.
It should be a pretty easy addition, let me know if you want to contribute.
I'd be happy to, but I have almost no experience with JS/TS and VS Code doesn't properly debug the code when I run a local copy of the extension. I would start by replacing SCRIPTDIR
in resolvedSourceUri
(https://github.com/bash-lsp/bash-language-server/blob/eed44ddb970fe15587b605696a9a658648057c28/server/src/util/sourcing.ts#L147 ), but I couldn't test ist so far.
I can provide a testcase covering the main two issues, however:
main/main.sh
:
$ cat main/test.sh
#!/bin/bash
foo_path="${BASH_SOURCE[0]%/*}/../inc/"
# not found by bash-ide and bash-ide shellcheck
# shellcheck source-path=SCRIPTDIR/../inc/
source "$foo_path/foo.sh"
# not found by bash-ide shellcheck
# shellcheck source=SCRIPTDIR/../inc/bar.sh
source "$foo_path/bar.sh"
bar
foo
ts@liag0002:/tmp/a$ cat inc/foo.sh
#!/bin/bash
# foo: does stuff
foo() { echo 'Hello World'; }
ts@liag0002:/tmp/a$ cat inc/bar.sh
#!/bin/bash
# bar: does stuff
bar() { echo 'Also hello world'; }
The shellcheck source=SCRIPTDIR/../inc/bar.sh
is already resolved correctly, but shellcheck called by the bash ide can't find both files. shellcheck called from a shell (shellcheck -x main/test.sh; (cd main; shellcheck -x test.sh)
) has no issue with either source
statement.
Thanks for providing the additional information here.
I've been running into exactly the same problem every day for many weeks, but I didn't report it here, as I thought it was caused by shellcheck... :)