shellcheck icon indicating copy to clipboard operation
shellcheck copied to clipboard

[BATS] BATS_TEST_DIRNAME is referenced but not assigned (SC2154)

Open jakub-bochenski opened this issue 6 months ago • 5 comments

I don't understand why would shellcheck warn about this?

This is a predefined BATS variable

Also shouldn't shellcheck ignore uppercase variables per https://github.com/koalaman/shellcheck/issues/356#issuecomment-95713643 ?

jakub-bochenski avatar Jun 25 '25 15:06 jakub-bochenski

Could you provide a code sample of where this is happening & the version of shellcheck being used?

When I make a simple test script, the uppercase BATS_TEST_DIRNAME is ignored by SC2154, using latest release 0.10.0. I understand that 0.10.0 is a bit old, but I get the same result on https://www.shellcheck.net/ which the readme claims is based on unreleased master branch.

The lowercase bats_test_dirname does produce the warning.

$ shellcheck -V
ShellCheck - shell script analysis tool
version: 0.10.0
license: GNU General Public License, version 3
website: https://www.shellcheck.net
$ cat SC2154.sh
#!/bin/bash
echo "$BATS_TEST_DIRNAME"
echo "$bats_test_dirname"

$ shellcheck SC2154.sh
In SC2154.sh line 3:
echo "$bats_test_dirname"
      ^----------------^ SC2154 (warning): bats_test_dirname is referenced but not assigned.

bwenrich avatar Jul 12 '25 02:07 bwenrich

Could be that OP is executing shellcheck with all checks active, even the optional.

$ shellcheck --list-optional | grep -A4 check-unassigned-uppercase
name:    check-unassigned-uppercase
desc:    Warn when uppercase variables are unassigned
example: echo $VAR
fix:     VAR=hello; echo $VAR

I agree that we need some more information to progress this task. Leaning towards closing as not planned.

brother avatar Aug 14 '25 10:08 brother

I am running with optional checks enabled.

I now noticed the detection is actually in setup_suite.bash, which is a BATS file, but the language is Bash

jakub-bochenski avatar Aug 14 '25 13:08 jakub-bochenski

With the new knowledge and lights can you please refine the description of the issue? As far as I can tell shellcheck is behaving exactly as intended.

Having all optional checks active is generally not ideal. Doing it once in a while to get a overview and consider some actions might be worth it by all means but not for the daily setup imho.

brother avatar Aug 15 '25 07:08 brother

With the new knowledge and lights can you please refine the description of the issue? As far as I can tell shellcheck is behaving exactly as intended.

I guess the issue is to be closed as invalid then, what's the point of updating it?

Having all optional checks active is generally not ideal. Doing it once in a while to get a overview and consider some actions might be worth it by all means but not for the daily setup imho.

I have only 5 of them enabled, maybe that will go down to 4 now :)

jakub-bochenski avatar Aug 18 '25 17:08 jakub-bochenski