Error highlighting sometimes missing for local variables

Hi Joachim,

Here is a small highlighting issue. In the following code (trimmed to just show the error) I show a function that shows a number of errors and seems to all work correctly for highlighting and error reporting:

if I hover over the correctly highlighted ‘$awsIp’ on line 11, the tooltip pops up and tells me that the variable is referenced but not assigned, and tells me to read “SC1254” for more details. So far, so good!

Then I cut-n-paste to duplicate the entire function and give it a different name. The error highlighting now becomes confused. Some of the reported errors in the first function disappear, and some of the errors in the second function are not shown at all:

If I hover over the missing ‘$awsIp’ variable on line 23 I see that you still know it is unresolved, although the usual tooltip help referring to “SC1254” is missing. And the variable no longer shows error highlighting.

Here is the actual code showing the problem:

#!/bin/bash 

function awsAppAvrSshOpenTunnel()
{
  local nwInterface="${1:?mising network interface arg}"
  local port="${2:?missing port arg}" pvtKeyArg appsvrHost

  [[ -e "$AWS_LOCAL_HOME/aws-pvt-key.pem" ]] && pvtKeyArg="-i $AWS_LOCAL_HOME/aws-pvt-key.pem"
  ! appsvrHost="$(__currentAppsvrIp)" && echo "can't find an AWS appsvr IP" >&2 && return 1
  
  echo "opening remote tunnel on ${awsIp}: -R ${nwInterface}:${port}:localhost:${port}"

}

function awsAppAvrSshOpenTunnel2()
{
  local nwInterface="${1:?mising network interface arg}"
  local port="${2:?missing port arg}" pvtKeyArg appsvrHost

  [[ -e "$AWS_LOCAL_HOME/aws-pvt-key.pem" ]] && pvtKeyArg="-i $AWS_LOCAL_HOME/aws-pvt-key.pem"
  ! appsvrHost="$(__currentAppsvrIp)" && echo "can't find an AWS appsvr IP" >&2 && return 1
  
  echo "opening remote tunnel on ${awsIp}: -R ${nwInterface}:${port}:localhost:${port}"

}


Thanks!
The warning ... is referenced by not assigned comes from ShellCheck. ShellCheck only warns for the first occurrence of an unresolved name, that’s why it’s not highlighted again in the 2nd function.
The Unresolved tooltip in the last screenshot is generated by BashSupport Pro’s quick documentation feature.

I’m working on changes for the upcoming 2.0 major version.
It’s going to ship improvements to the “Unresolved variable” inspections and others, like “Ununused global variable”. The plan is to disable ShellCheck’s SC2154 (and related) when these inspections are enabled by default. That’s allows improved highlighting, to avoid duplicate warnings and also improved logic, e.g. to track declarations and references in including files.

For now and 1.x, there’s inspection “Unresolved variable [beta]” (link), but it’s currently not good enough to be used as an equivalent replacement. It’s also complicated to make this coexist with ShellCheck’s own warnings.

Joachim

Thanks Joachim!

Coming at this from an old salt Java programmer’s perspective I guess I am surprised that shellcheck considers a local variable with the same name in two different functions as ‘the same variable’. This is another example of how you have to be careful to not expect too much from BASH!
J

Well, it’s possibly local, but the scoping is unknown. By default, everything is global in POSIX and Bash.
For 2.0 I’ll most likely highlight all unresolved variables, perhaps with a configuration to only do this for the first global variable in a file.

BashSupport Pro also treats non-local variables in functions as global, i.e. for rename, find usages, etc.
The scoping is a constant source of troubles and there will be conflicts, espcially with multiple functions declaring the same globals.

a() {
  myVar="$myVar a"
}
b() {
  myVar="$myVar b"
}
a # leaks global "myVar"
b # modifies global
a
# print " a b a"
echo $myVar