mvdan / sh

A shell parser, formatter, and interpreter with bash support; includes shfmt
https://pkg.go.dev/mvdan.cc/sh/v3
BSD 3-Clause "New" or "Revised" License
7.29k stars 345 forks source link

syntax: got `"}" can only be used to close a block` on a seemingly normal block #739

Closed andras-kth closed 2 years ago

andras-kth commented 3 years ago

I'm struggling to understand what's wrong with this sample:

$ shfmt
@() {
    echo "$@"
}
@ hello world\!
<standard input>:3:1: "}" can only be used to close a block
andras-kth commented 3 years ago

Apparently, the "bug(?)" has to do with the "at" sign (@), since using an equal sign (=) instead works fine...

$ shfmt
=() {
    echo "$@"
}
= Hello world\!
=() {
        echo "$@"
}
= Hello world\!
$
andras-kth commented 3 years ago

Found a workaround (I just love the internet...):

$ shfmt 
@()
{
    echo "$@"
}
@ Hello world\!
@()
{
        echo "$@"
}
@ Hello world\!

I'm still not sure why shfmt treats @ special, or why the added line-break makes it tick, but this solves my issue for the time being...

andras-kth commented 3 years ago

Interestingly, it's not only the "at" sign (@) that appears to be special but the plus sign (+) as well:

$ for i in @ + - = , ^; do echo; echo __ $i __; echo "$i() { echo yay; }" | shfmt; echo; done

__ @ __
<standard input>:1:17: "}" can only be used to close a block

__ + __
<standard input>:1:17: "}" can only be used to close a block

__ - __
-() { echo yay; }

__ = __
=() { echo yay; }

__ , __
,() { echo yay; }

__ ^ __
^() { echo yay; }
mvdan commented 3 years ago

Thanks for the report! This might be a bug in the parser. I'd be happy to review a patch if you're up for looking into it :)

andras-kth commented 3 years ago

Thanks for the report! This might be a bug in the parser. I'd be happy to review a patch if you're up for looking into it :)

Actually, I'm not. The only reason I use this tool is because I'm forced to. I'm fully capable of writing shell code in a format that I like, and I truly hate some of the formatting choices made for me...

One clear and present danger with tools like this is that they are often misused by local micro-dictators to force a straight-jacket of uniformity on their work force, in the name of maintainability.

mvdan commented 3 years ago

I'm not sure how to answer to that, so I won't :)

riacataquian commented 2 years ago

I'll take a look at this @mvdan :)

mvdan commented 2 years ago

Sounds good!