bash-language-server icon indicating copy to clipboard operation
bash-language-server copied to clipboard

`.editorconfig` `[[bash]]` does not work

Open loynoir opened this issue 1 month ago • 8 comments

Code editor

vscode

Platform

Linux

Version

[email protected]

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

loynoir avatar Nov 01 '25 10:11 loynoir

Bash language server doesn't support formatting.

skovhus avatar Nov 02 '25 19:11 skovhus

@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]].

loynoir avatar Nov 05 '25 03:11 loynoir

Oh my, forgot that shfmt was added.

skovhus avatar Nov 05 '25 08:11 skovhus

@chris-reeves do you mind having a look at this issue? Or @loynoir, contributions are more than welcome.

skovhus avatar Nov 05 '25 08:11 skovhus

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.

chris-reeves avatar Nov 08 '25 15:11 chris-reeves

$ 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

loynoir avatar Nov 08 '25 16:11 loynoir

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.

chris-reeves avatar Nov 08 '25 17:11 chris-reeves

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.

mvdan avatar Nov 08 '25 23:11 mvdan