tree-sitter-bash icon indicating copy to clipboard operation
tree-sitter-bash copied to clipboard

parse error when negating a variable assignment in an if statement

Open spikegrobstein opened this issue 5 years ago • 3 comments

I was sent over here after filing this issue: https://github.com/mads-hartmann/bash-language-server/issues/135

I'm getting a parse error when I have an if statement where I negate a variable assignement. For example:

#! /usr/bin/env bash

bar() {
  return 1
}

foo=''

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

The error message in the comment is coming from the language server that's implementing your library.

Hopefully this is enough info for a fix. thanks so much!

spikegrobstein avatar Jun 06 '19 13:06 spikegrobstein

Issue-Label Bot is automatically applying the label bug to this issue, with a confidence of 0.87. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

issue-label-bot[bot] avatar Jun 06 '19 13:06 issue-label-bot[bot]

I have the same issue: it seems like the parser is expecting a Simple Command, where we start with some assignments with local scope, followed by a command (word). However, if there is no word, then we simply have an assignment, and follow the rules of exit code for such assignments. In my case, the exit code should be extracted from command substitution:

if ! _exe=$(command -v dummy); then
  echo "Dummy command does not exist"
fi

The false error goes away when we're taking away the expression negation (!)... so a work around is:

if _exe=$(command -v dummy); then
  ...
else
  echo "Dummy command does not exist"
fi

But that's just not practical in all scenarios.

dseynhae avatar Sep 09 '19 23:09 dseynhae

Luckily I’m just using this in my editor so I’m living with the errors. I don’t want to go add no-op handlers for the successful situations especially if this gets fixed and I can switch back.

spikegrobstein avatar Sep 09 '19 23:09 spikegrobstein