bash-language-server icon indicating copy to clipboard operation
bash-language-server copied to clipboard

falsely reported syntax error for negated conditional assignment statements

Open spikegrobstein opened this issue 6 years ago • 4 comments

I'm getting the message Syntax error: expected "word" somewhere in the file when I have statements like if ! myvar="$( some_func )"; then

A minimal, complete example:

#! /usr/bin/env bash

bar() {
  echo "bar"
  return 1
}

foo=''

if ! foo="$(bar)"; then # <-- Syntax error: expected "word" somewhere in the file
  echo "ok."
fi

If I change it to just if foo="$( bar )"; then, then it doesn't bark. It's the ! that makes it angry.

this is valid bash and executes without issue. This also didn't happen in an older version of bash-language-server. I upgraded the other day, but I'm not sure what the old version I had running was. I use this structure in my code all the time so it's kinda annoying to constantly see this syntax error.

spikegrobstein avatar Jun 05 '19 22:06 spikegrobstein

Sorry to hear. The grammar we are using were recently upgraded, so I'm pretty sure this is an issue with https://github.com/tree-sitter/tree-sitter-bash

Can you create an issue over there?

skovhus avatar Jun 06 '19 06:06 skovhus

Will do! I’ll try to relay the info as best I can since the error message comes from you so it may be hard to properly describe the issue. But I’ll do my best. Thanks!

spikegrobstein avatar Jun 06 '19 13:06 spikegrobstein

This seems to still be an issue. Is there a workaround?

belminf avatar May 12 '20 22:05 belminf

This is still a problem:

#!/usr/bin/env bash

# Problem message on next line: Syntax error: expected "word" somewhere in the file"
if ! current_directory=$(pwd); then
    printf "FAILLD with code %s: message is %s\n" "$?" "${current_directory}"
else
    printf "Directory is %s\n" "${current_directory}"
fi

# no messages
if current_directory=$(pwd); then
    printf "Directory is %s\n" "${current_directory}"
else
    printf "FAILLD with code %s: message is %s\n" "$?" "${current_directory}"
fi

This is with the Bash IDE Visual Studio Code Plugin v1.14.0 - I've got no idea how that corresponds to the tree-sitter-bash version so I don't know if the problem is still open with them under #51 or if the fix just hasn't been pulled across yet.

rbairwell avatar Jul 23 '22 02:07 rbairwell

This seems to have been fixed in the newer version of the grammar

skovhus avatar Nov 25 '22 13:11 skovhus