Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support leading dynamic section when sourcing #1086

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

aarondill
Copy link

This does the same thing that ShellCheck does (here).

To support the common pattern of . "$CONFIGDIR/mylib.sh", ShellCheck strips one leading, dynamic section before trying to locate the rest.

fixes #926, fixes #659

@aarondill aarondill marked this pull request as draft January 21, 2024 12:18
@aarondill
Copy link
Author

this can now handle all the below cases:
. "$dir/file"
. "$dir/other/file"
. "$dir"/file
. $dir/file
. $dir"/file"
. "$dir"/"file"
. "${dir:-.}"/"file"
. ${dir}/"fi"l'e'
source "$dir/file"
It intentionally fails on these:
. "$dir/$file"
. "$dir/other/$file"
. "$(cmd)/file"
. $(cmd)/file
Tests are coming soon! (once i figure out this testing framework)

@Shane-XB-Qian
Copy link
Contributor

It intentionally fails on these:

is there a way to make it work? seems that's the much more usage case.

@aarondill
Copy link
Author

It intentionally fails on these:

is there a way to make it work? seems that's the much more usage case.

potentially, but these cases are process substitution and dynamic file sourcing. if you really want to use process substitution, assign it to a variable, then use that - this way it will work with shellcheck too. as to dynamic file sourcing, at the point this is evaluated, there's no way to get the "static" value of a variable, so there's no meaningful way to support that.

the current implementation matches shellcheck's behavior

@aarondill aarondill marked this pull request as ready for review February 6, 2024 14:47
This does the same thing that ShellCheck does ([here](https://github.com/koalaman/shellcheck/blob/ba86c6363c30a5dbefd0b8b9a7c5f4ab0478dc91/src/ShellCheck/Parser.hs#L2277-L2283)).

`To support the common pattern of . "$CONFIGDIR/mylib.sh", ShellCheck strips one leading, dynamic section before trying to locate the rest.`

fixes bash-lsp#926, fixes bash-lsp#659
This fixes `${var}/path` and `"${var}"/path` as well as countless variations of these patterns

Note: this turned out to be rather complex task, so it's broken out into a seperate function, which allows for early returns
This seemed the most appropriate place for it.
Adds a simple test for dynamic source paths and updates the snapshot to match new tests / behaivor.

Note: all the line numbers in the snapshot increased by 2 because I added two new lines (the test and a blank one)
I added this to the bottom of the file to minimize snapshot diffs. they're large enough as it is.
@Shane-XB-Qian
Copy link
Contributor

Shane-XB-Qian commented Feb 6, 2024

there's no way

🤔 ok, anyway, this looks was an good improvement.

@isc-aray
Copy link

isc-aray commented Jun 3, 2024

This would be great! All of our sources are of the form . "$ScriptDir/something.sh" or . "$ScriptDir/dir/something.sh".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants