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
6.97k stars 332 forks source link

shfmt breaks hashes for associative arrays that contain dashes #1052

Closed puetz-partec closed 4 months ago

puetz-partec commented 5 months ago

Hi,

I came across the following problem using shgmt (v3.7.0) on the following valid bash code. I am using hashes for associative arrays that contain dahes (in my example --shfmt-bug However, shfmt insert blanks between --shfmt - bug, which breaks the array lookup:

test.sh:

#!/bin/bash
. lib.sh # defines readargs function
declare -A args
readargs args
echo "${args[--shfmt-bug]}

lib.sh

readargs() {
  local -n _args
  args[--shfmt-bug]="hello"
}

lib.sh is coming from a library, which isn't formatted with shfmt. While my own code (represented by test.sh) is formatted with shfmt. Because the blanks change the hash in a meaningful way, the array lookup fails. I can use "--shfmt-bug" in my code to prevent shfmt from doing that, but I shouldn't have to.

mvdan commented 5 months ago

Did you see the caveats section in the README?

mvdan commented 4 months ago

Closing; this is covered there.