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.1k stars 336 forks source link

syntax: line continuation characters inside a string #873

Closed ColemanTom closed 2 years ago

ColemanTom commented 2 years ago

I have just noticed that recently shfmt has changed expectations about strings. My guess is it relates to https://github.com/mvdan/sh/pull/837/commits/4556b2f2a22aa51bcfa4e5fde67ed5a89eccab78

I was wondering if this was expected/desired behaviour?

Originally this was fine for shfmt (at least with v3.4.2)

read -r pt ypt mpt <<< \
    "$(date -d "${DATE} -$DIFFERENCE hours" \
        "+%YmdH %Y %m")"

Now shfmt wants to change it to

read -r pt ypt mpt <<< "\
$(date -d "${DATE} -$DIFFERENCE hours" \
    "+%YmdH %Y %m")"

My personal view is the original was more readable. Otherwise changing it to something like what I have below is possibly clearer still (at least for python devs).

read -r pt ypt mpt <<< "$(
    "$(date -d "${DATE} -$DIFFERENCE hours" \
        "+%YmdH %Y %m"
)"
mvdan commented 2 years ago

Thanks for raising this; I'm fairly sure this is an unintended regression.

mvdan commented 2 years ago

Err, I actually wrote a complete fix for this three weeks ago, and I was sure I had already merged it to master. Somehow, I kept it in a local branch and completely forgot about it :) See https://github.com/mvdan/sh/pull/896.