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

When processing a bash script, `shfmt` defaults to using `POSIX` syntax rules instead of bash-specific syntax #1057

Closed Montana closed 4 months ago

Montana commented 4 months ago

Hey all,

I came across a similar discussion regarding this issue. It appears that format-all prioritizes the SHELL environment variable (which is configured to zsh) over the current buffer's sh-shell setting. When I changed the SHELL environment to bash using SHELL=bash doom env and then reloaded, the formatter started recognizing bash, moving away from its previous zsh default. To test this further, I added (message "SHELL: %s" sh-shell) to the format-all--buffer-hard function. Interestingly, even though my current buffer's sh-shell was set to bash, format-all still opted for zsh.

I'm thinking the way format-all is implemented could inherently give precedence to system-wide settings (like environment variables) over Emacs buffer-local variables. This could be an oversight or a deliberate design decision based on the tool's intended use cases, I understand this behavior ensures faster operation but at the cost of flexibility in specific scenarios.

The next idea was feasibly that the logic within format-all for detecting the shell might not be robust enough to handle cases where the buffer's shell differs from the system's default shell. I'm aware Doom Emacs aggressively caches environment variables for performance and simplicity, which might cause this issue.

It's also feasible that this behavior might even serve as a safeguard, with format-all defaulting to system-wide configurations in instances where it faces challenges in accurately identifying or implementing buffer-local settings, thus guaranteeing operation within a consistent environment.

Steps to reproduce:

Any help would be greatly appreciated. I thank you for your time.

Cheers, Michael Mendy

mvdan commented 4 months ago

Sorry but I have no idea about much of what you wrote there. I assume you're talking about an Emacs plugin of some sort? I don't maintain that nor is it in this repository. I also have no clue what format-all is.