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

allow the expansion of ~ when sourcing files #507

Closed
travi opened this issue Oct 15, 2015 · 13 comments
Closed

allow the expansion of ~ when sourcing files #507

travi opened this issue Oct 15, 2015 · 13 comments

Comments

@travi
Copy link

travi commented Oct 15, 2015

In scripts that I use across multiple systems, I use ~ when referencing my home directory since it is at a different location on each system. However, recent versions have been giving me warnings related to SC1091 even when running as shellcheck -x for files that do exist.

Would it be reasonable to enable shellcheck to follow paths that include ~ like this?

@deryni
Copy link

deryni commented Oct 15, 2015

Why ~ instead of $HOME? Which I would imagine shellcheck would handle better.

@travi
Copy link
Author

travi commented Oct 15, 2015

I would be open to that too, but the same problem exists. Shellcheck does not expand $HOME and gives the (very similar) SC1090 warning. This warning was added to shellcheck earlier, which ended up being part of the reason I became more consistent with using ~ instead.

@deryni
Copy link

deryni commented Oct 15, 2015

Ah. Sounds like shellcheck should possibly understand ~ in that location as non-constant and get you back to SC1090 then. =)

shellcheck not assuming $HOME is valid for the current environment somewhat unreasonable. It might be nice to have a way to tell shellcheck what the "environment" of the running script will look like for things like this to work though.

@travi
Copy link
Author

travi commented Oct 15, 2015

Sounds reasonable, but I do hope that expanding both ~ and $HOME could be supported as both are valid and I personally think ~ is a bit cleaner.

Also, I've realized that not all of the cases where I use shellcheck are in a shell instance (like the atom plugin, so env vars would not be available directly. I do think that populating the "environment" would be important.

@deryni
Copy link

deryni commented Oct 16, 2015

$HOME is unambiguous in its expansion.

~ is a prefix to a number of possible expansions (which isn't a reason not to support the simple expansion this is just a comment).

$HOME also expands in variables where ~ doesn't (and while this doesn't come up much in general it is surprisingly common for people to try and get confused when it doesn't just look on StackOverflow for example).

That being said I'm not sure using either of those in scripts is all that good of an idea. Finding the location of the currently executing script and using that to find bundled scripts seems like a more robust plan to me (and would be something that shellcheck could, I think, easily detect the common idioms for and support but that's not my decision).

@travi
Copy link
Author

travi commented Oct 16, 2015

All good points. I do believe that shellcheck already warns about ~ not expanding in certain circumstances. Probably related: when using $HOME, shellcheck also applies its normal variable rules and recommends things like quoting to prevent word splitting, etc. Using ~ should be able to avoid those steps safely because of the difference in how expansion works, correct?

My primary use case for using either of these is related to my dotfiles where many of the scripts need to be relative to the home directory. However, I version all of the scripts in a separate directory and symlink them from there to the proper location. Because of that, referencing them relative to the home directory definitely simplifies things.

@deryni
Copy link

deryni commented Oct 16, 2015

Yes, tilde expansion is implicitly quoted and not subject to field splitting and pathname expansion.

From the spec:

The pathname resulting from tilde expansion shall be treated as if quoted to prevent it being altered by field splitting and pathname expansion.

And yeah, I see why you want to use ~ there instead of $HOME.

@koalaman
Copy link
Owner

It would be both reasonable and convenient, and source resolution needs more work in general.

However, to postpone any decisions about whether to use shellcheck's HOME or the script's HOME or the user's home dir or whether to support ~foo and ~+ and ~- and whether to account for PATH with ./ as fallback like bash does or PATH with no fallback like dash does, and again which PATH to use, 630f20e just adds a kludge so shellcheck doesn't consider ~/ a static path (just so you'll get a more understandable error message).

Maybe you can add an example config in the script's source dir and use that, or run shellcheck from the dir with the include files you want?

@travi
Copy link
Author

travi commented Oct 19, 2015

I think the change to get the other error reporting was an appropriate step, but I don't understand what you mention about a config? I found some other issues mentioning a ~/.shellcheck file, but don't find any concrete reference to something like that.

Could you expand on how I could do what you are referring to?

@travi
Copy link
Author

travi commented Aug 28, 2016

any chance there is a better recommendation than simply disabling the SC1090 rule for this use case by this point?

@reynoldscem
Copy link

@travi Did you ever find a nice way around this? I also want to use shellcheck with my dotfiles (and so refer to things relative to my home).

@travi
Copy link
Author

travi commented Jan 17, 2019

I don't believe so, but it has been a bit of time since I've tried very hard. It looks like i have a mix of switching ~ to $HOME and leaving ~, but disabling SC1090 with a comment. I dislike the fact that I'm not consistent, but I must have lost motivation since there wasn't a supported was to lint for consistently using the "right" way.

Interesting timing with your question though, I also got a notification about a thread I've been following for nvm, which suggests that ~ is faster than $HOME. I haven't done anything to verify the claim, but if true, I'd want to undo any of my changes to use $HOME.

SuJiKiNen added a commit to SuJiKiNen/dotfiles that referenced this issue Mar 2, 2019
SuJiKiNen added a commit to SuJiKiNen/dotfiles that referenced this issue Mar 2, 2019
SuJiKiNen added a commit to SuJiKiNen/dotfiles that referenced this issue Dec 10, 2019
@b0o b0o mentioned this issue Sep 22, 2020
2 tasks
asbjornu added a commit to asbjornu/dotfiles that referenced this issue Jul 8, 2021
Ignore SC1090: ShellCheck can't follow non-constant source. Use a directive to specify location.

https://www.shellcheck.net/wiki/SC1090

See the following discussion for why SC1090 is disabled and not fixed:

koalaman/shellcheck#507
@stdedos
Copy link
Contributor

stdedos commented Apr 19, 2022

Maybe you can add an example config in the script's source dir and use that, or run shellcheck from the dir with the include files you want?

Well, an example file could be the Ubuntu's default .bashrc:

...
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi
...

We can freely argue semantics; but I'd like for it to be "clean cut and working out of the box" without (much) additional changes/configs/lintings.

sleicht pushed a commit to sleicht/dotfiles that referenced this issue Mar 9, 2023
Fix SC2140

Fix SC2140: Word is of the form "A"B"C" (B indicated). Did you mean "ABC" or "A\"B\"C"?

https://www.shellcheck.net/wiki/SC2140

Ignore SC2139

Ignore SC2139: This expands when defined, not when used. Consider escaping.

https://www.shellcheck.net/wiki/SC2139

Fix SC2046

Fix SC2046: Quote this to prevent word splitting.

https://www.shellcheck.net/wiki/SC2046

Fix SC2181

Fix SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.

https://www.shellcheck.net/wiki/SC2181

Fix SC2199

Fix SC2199: Arrays implicitly concatenate in [[ ]]. Use a loop (or explicit * instead of @).

https://www.shellcheck.net/wiki/SC2199

Fix SC2124

Fix SC2124: Assigning an array to a string! Assign as array, or use * instead of @ to concatenate.

https://www.shellcheck.net/wiki/SC2124

Fix SC1018

Fix SC1018: This is a unicode non-breaking space. Delete and retype it.

https://www.shellcheck.net/wiki/SC1018

Fix SC2155

Fix SC2155: Declare and assign separately to avoid masking return values.

https://www.shellcheck.net/wiki/SC2155

Ignore SC2154

Ignore SC2154: yellow is referenced but not assigned.

https://www.shellcheck.net/wiki/SC2154

Ignore SC2034

Ignore SC2034: foo appears unused. Verify use (or export if used externally).

https://www.shellcheck.net/wiki/SC2034

Fix SC2091

Fix SC2091: Remove surrounding $() to avoid executing output (or use eval if intentional).

https://www.shellcheck.net/wiki/SC2091

Fix SC2162

Fix SC2162: read without -r will mangle backslashes.

https://www.shellcheck.net/wiki/SC2162

Fix SC2166

Fix SC2166: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.

https://www.shellcheck.net/wiki/SC2166

Ignore SC1090

Ignore SC1090: ShellCheck can't follow non-constant source. Use a directive to specify location.

https://www.shellcheck.net/wiki/SC1090

See the following discussion for why SC1090 is disabled and not fixed:

koalaman/shellcheck#507

Fix SC2128

Fix SC2128: Expanding an array without an index only gives the first element.

https://www.shellcheck.net/wiki/SC2128

Fix SC2164

Fix SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

https://www.shellcheck.net/wiki/SC2164

Fix SC2197

Fix SC2197: fgrep is non-standard and deprecated. Use grep -F instead.

https://www.shellcheck.net/wiki/SC2197
sleicht pushed a commit to sleicht/dotfiles that referenced this issue Mar 9, 2023
Fix SC2140

Fix SC2140: Word is of the form "A"B"C" (B indicated). Did you mean "ABC" or "A\"B\"C"?

https://www.shellcheck.net/wiki/SC2140

Ignore SC2139

Ignore SC2139: This expands when defined, not when used. Consider escaping.

https://www.shellcheck.net/wiki/SC2139

Fix SC2046

Fix SC2046: Quote this to prevent word splitting.

https://www.shellcheck.net/wiki/SC2046

Fix SC2181

Fix SC2181: Check exit code directly with e.g. 'if mycmd;', not indirectly with $?.

https://www.shellcheck.net/wiki/SC2181

Fix SC2199

Fix SC2199: Arrays implicitly concatenate in [[ ]]. Use a loop (or explicit * instead of @).

https://www.shellcheck.net/wiki/SC2199

Fix SC2124

Fix SC2124: Assigning an array to a string! Assign as array, or use * instead of @ to concatenate.

https://www.shellcheck.net/wiki/SC2124

Fix SC1018

Fix SC1018: This is a unicode non-breaking space. Delete and retype it.

https://www.shellcheck.net/wiki/SC1018

Fix SC2155

Fix SC2155: Declare and assign separately to avoid masking return values.

https://www.shellcheck.net/wiki/SC2155

Ignore SC2154

Ignore SC2154: yellow is referenced but not assigned.

https://www.shellcheck.net/wiki/SC2154

Ignore SC2034

Ignore SC2034: foo appears unused. Verify use (or export if used externally).

https://www.shellcheck.net/wiki/SC2034

Fix SC2091

Fix SC2091: Remove surrounding $() to avoid executing output (or use eval if intentional).

https://www.shellcheck.net/wiki/SC2091

Fix SC2162

Fix SC2162: read without -r will mangle backslashes.

https://www.shellcheck.net/wiki/SC2162

Fix SC2166

Fix SC2166: Prefer [ p ] && [ q ] as [ p -a q ] is not well defined.

https://www.shellcheck.net/wiki/SC2166

Ignore SC1090

Ignore SC1090: ShellCheck can't follow non-constant source. Use a directive to specify location.

https://www.shellcheck.net/wiki/SC1090

See the following discussion for why SC1090 is disabled and not fixed:

koalaman/shellcheck#507

Fix SC2128

Fix SC2128: Expanding an array without an index only gives the first element.

https://www.shellcheck.net/wiki/SC2128

Fix SC2164

Fix SC2164: Use 'cd ... || exit' or 'cd ... || return' in case cd fails.

https://www.shellcheck.net/wiki/SC2164

Fix SC2197

Fix SC2197: fgrep is non-standard and deprecated. Use grep -F instead.

https://www.shellcheck.net/wiki/SC2197
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

No branches or pull requests

5 participants