-
-
Notifications
You must be signed in to change notification settings - Fork 105
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
Fix unbound shell variable errors on strict mode #512
Fix unbound shell variable errors on strict mode #512
Conversation
How can we catch this moving forward? Can we enable this in shellcheck or something? |
I was surprised that shellcheck didn't warn about this, and as far as I could tell there's no way to make it work that way [1, 2]. I can maybe add tests checking for this behaviour, but I don't think it's ideal given that it would only detect the specific bad cases we write. I'll play around with |
Should we simply add |
If it was a standalone script I'd say it would make sense, but in this case I think this should be opted-in by the user, for example via direnv's stdlib ( Even more, the project's own I couldn't find anything useful for this in the shellcheck rules, so maybe enforcing it during code review is enough. For what I see, most tests run with strict mode on and off, so the happy path should always be correct in that regard. |
If we're fixing these errors, we should also attempt to avoid introducing these errors in the first place. I have been convinced that I'll keep searching for some way of getting some tooling to complain about this staticallly, but in the meantime - there's no use standing on principle and I'll merge. |
We could in our shellcheck run prepend set -u to direnvrc without doing it in our code otherwise. |
The problem with that approach is that it only catches runtime errors. I want shellcheck (or something like it) to figure this out statically rather than relying on execution to move through the script branch by branch. We should've caught these already, but because they're in error handling, we didn't (since we already know how to properly invoke nix-direnv). That might not be a bad approach in general, but I think we maybe also want to add error cases that fail in argument handling? I'm not entirely sure yet and will have to think on this a bit. |
is shellcheck not getting more strict if you add |
100% agree
First of all, the rule specifies: So it will never work with But # -*- mode: sh -*-
# shellcheck shell=bash
echo $direnv # triggers the warning However, this file doesn't trigger any warning: # -*- mode: sh -*-
# shellcheck shell=bash
echo $direnv # triggers the warning
if [[ -z $direnv ]]; then
:
fi On the other hand, executing this file results in an # -*- mode: sh -*-
# shellcheck shell=bash
set -u
if [[ -z $direnv ]]; then
:
fi So, can this rule help us avoid these errors? Yes. Just not all of them. And the moment we reference a variable inside a conditional all the checks and warnings for that variable magically disappear. Potential improvementI just read about the # -*- mode: sh -*-
# shellcheck shell=bash
# shellcheck enable=check-unassigned-uppercase
# ... rest of the file ... It still doesn't work for arguments (
The project's
It doesn't look like it. Funnily enough, while looking for it I found a report of the exact same issue I described above koalaman/shellcheck#2779 (comment) |
These errors only happen with
strict_env
or equivalent (set -u
), and in the following cases:use flake
incorrectly, for example passing a flag as first and only argument:use flake --impure
use nix
with flags that require extra arguments, without the arguments:use nix -A
,use nix -o optionName
,use nix -o
...