`.editorconfig` `[[bash]]` does not work
Code editor
vscode
Platform
Linux
Version
What steps will reproduce the bug?
No response
How often does it reproduce? Is there a required condition?
Always.
What is the expected behavior?
When editorconfig
[[bash]]
switch_case_indent = true
x.sh formatted into
#!/bin/bash
case "$1" in
42)
echo 42
;;
esac
What do you see instead?
When editorconfig
[[bash]]
switch_case_indent = true
#!/bin/bash
case "$1" in
42)
echo 42
;;
esac
Additional information
When editorconfig empty
x.sh formatted into
#!/bin/bash
case "$1" in
42)
echo 42
;;
esac
When editorconfig
[*.sh]
switch_case_indent = true
x.sh formatted into
#!/bin/bash
case "$1" in
42)
echo 42
;;
esac
Bash language server doesn't support formatting.
@skovhus
Bash language server doesn't support formatting.
?
https://github.com/bash-lsp/bash-language-server/blob/371913c741df554d5be2139aa9833814dabcd42c/server/src/shfmt/index.ts#L151
There should be incorrect logic calling shfmt.
Because using shfmt within terminal, shfmt respect [[bash]].
Oh my, forgot that shfmt was added.
@chris-reeves do you mind having a look at this issue? Or @loynoir, contributions are more than welcome.
The [[shell]] and [[bash]] section markers are not part of the EditorConfig spec and therefore not supported by the core editorconfig package that we use - these are custom extensions implemented by the author of shfmt.
Supporting this would either require us to implement our own parser for EditorConfig which supports these custom extensions, or hack the existing code in some not entirely pleasant way.
$ cat x.sh | shfmt --filename=/tmp/reproduce/x.sh -i=4 -ln=auto -
#!/bin/bash
case "$1" in
42)
echo 42
;;
esac
Can't we just simplify the calling command into below?
$ shfmt x.sh
#!/bin/bash
case "$1" in
42)
echo 42
;;
esac
$ cat x.sh | shfmt
#!/bin/bash
case "$1" in
42)
echo 42
;;
esac
No - that wouldn't work because it provides no way for the user to configure shfmt (it depends entirely upon EditorConfig).
If your shell scripts have names ending in .sh then you're probably better off simply updating your .editorconfig to use the standard [*.sh] glob. Granted, that won't work for scripts that don't have matching names, but there's no supported way to handle that via EditorConfig.
I think @chris-reeves is correct in https://github.com/bash-lsp/bash-language-server/issues/1351#issuecomment-3506618714. shfmt does support EditorConfig with a custom extension for the sake of configuring formatting for suffix-less files. You can see the history in https://github.com/mvdan/sh/issues/664, but the tl;dr is:
- this was a popular request, which is understandable, as scripts often don't have extensions
- EditorConfig had a proposal in place for such a use case (https://github.com/editorconfig/editorconfig/issues/404) but it was stalled
- After three years, we just added support ourselves, with the understanding that it's a backwards-compatible extension to EditorConfig
Our code is all open source if you want to copy any of it, but it is in Go, and I understand you wanting to stick to vanilla EditorConfig. The only other suggestion that comes to mind is to offer the user an option to run shfmt in "only EditorConfig" mode, so that no flags are passed to it, akin to the examples in https://github.com/bash-lsp/bash-language-server/issues/1351#issuecomment-3506689009. Because passing any parsing/formatting flags to shfmt will cause it to ignore EditorConfig files, to avoid mixing options from different sources.