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

Add option to look for sources in alternate root paths #1553

Merged
merged 1 commit into from
Apr 25, 2019

Conversation

epontan
Copy link
Contributor

@epontan epontan commented Apr 22, 2019

Add a new optional flag -r|--root ROOTPATHS, where ROOTPATHS is a colon separated list of paths, that will look for external sources in alternate roots.

This is particular useful when the run-time environment does not fully match the development environment. The #shellcheck source=file directive is useful, but has its limitations in certain scenarios. Also, in many cases the directive could be removed from scripts when the root flag is used.

Script example.bash:

#!/bin/bash
source /etc/foo/config

Example usage where etc/foo/config exists in skel/foo:

shellcheck -x -r skel/foo:skel/core example.bash

@austin987
Copy link
Contributor

The help and your comment say semi colon, but the code is using a colon for delimiter.

Add a new optional flag "-r|--root ROOTPATHS", where ROOTPATHS is a
colon separated list of paths, that will look for external sources in
alternate roots.

This is particular useful when the run-time environment does not fully
match the development environment. The #shellcheck source=file directive
is useful, but has its limitations in certain scenarios. Also, in many
cases the directive could be removed from scripts when the root flag is
used.

Script example.bash:
  #!/bin/bash
  source /etc/foo/config

Example usage where etc/foo/config exists in skel/foo:
  # shellcheck -x -r skel/foo:skel/core example.bash
@epontan
Copy link
Contributor Author

epontan commented Apr 22, 2019

The help and your comment say semi colon, but the code is using a colon for delimiter.

Thanks for noticing that! I don't know why I wrote semi-colon when I really was meaning colon :)
Anyway, it is fixed now.

@knirch
Copy link

knirch commented Apr 23, 2019

This would resolve #510 #882 and potentially be useful for #539 #769

@koalaman koalaman merged commit af46758 into koalaman:master Apr 25, 2019
@koalaman
Copy link
Owner

Very nice, thanks!

I expanded this a bit into "source paths" (-P):

  • For absolute paths, it behaves the same
  • It also looks up relative paths based on these dirs (so -P "$PATH" will find . foo in any path entry)
  • It accepts a special name SCRIPTDIR to allow you to search relative to the current script's directory
  • It uses platform canonical search path parsing, so that c:\foo;d:\bar works as expected on Windows
  • It allows you to specify multiple -P flags that accumulate, e.g. with export SHELLCHECK_OPTS='-P foo'; shellcheck -P bar ..
  • It checks whether the files are allowed by the sandboxing to avoid errors exposing which files exist (only really matters for shellcheck.net)

@blueyed
Copy link
Contributor

blueyed commented Jun 18, 2019

  • It accepts a special name SCRIPTDIR to allow you to search relative to the current script's directory

I suggest using something more explicit here - since "SCRIPTDIR" might be a valid path.
It might make sense to have "./" be handled as "local to script file" instead.

I.e. instead of -P SCRIPTDIR/include you would use ./include, but include would still be relative to cwd.
Alternatively something like ::SCRIPTDIR:: could be used (i.e. something that contains characters not valid for filenames - would be ;;SCRIPTDIR;; on Windows then.
I can see that this might be ugly, but wanted to mention this since it is easy to adjust before having it release now still.

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

Successfully merging this pull request may close these issues.

5 participants