sh icon indicating copy to clipboard operation
sh copied to clipboard

`indent_style = space` in .editorconfig is not respected by shfmt

Open Karmenzind opened this issue 1 year ago • 5 comments

The file is:

#!/usr/bin/env bash

if [[ true ]]; then
    echo 1  # leading with 4 spaces
fi

And shfmt will change the leading spaces to tab: image

My ~/.editorconfig:

root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line          = lf
insert_final_newline = true
indent_style         = space
indent_size          = 4

[*.sh]
indent_size        = 4
indent_style       = space
shell_variant      = bash
binary_next_line   = false

switch_case_indent = true
space_redirects    = true
keep_padding       = true

function_next_line = false

Karmenzind avatar Aug 28 '24 08:08 Karmenzind

What shfmt version are you running? Did you double check this? Because we have code and tests for this feature, so this seems odd.

mvdan avatar Sep 15 '24 10:09 mvdan

Sorry for the missing information. I'm using the latest version 3.9.0:

added cat -T to visualize the tabs

image

OS ENV:

> uname -a                                                                                                                                                                                                                   
Linux ringo 6.10.8-arch1-2.1-g14 #1 SMP PREEMPT_DYNAMIC Sat, 07 Sep 2024 06:01:43 +0000 x86_64 GNU/Linux

Karmenzind avatar Sep 16 '24 08:09 Karmenzind

Does your shell script file have a .sh file extension? If not you might need to use [[shell]] instead of or in addition to [*.sh].

wwuck avatar Jan 24 '25 05:01 wwuck

The file's name and content are given in my previous comment @wwuck

Karmenzind avatar Jan 24 '25 06:01 Karmenzind

@Karmenzind apologies for missing that. I had image display turned off in my browser.

wwuck avatar Jan 24 '25 12:01 wwuck

When you are in /tmp .editorconfig cannot be found.

I can recreate the issue if I create /tmp/a.sh and then run shfmt. If you then copy your .editorconfig to /tmp/.editorconfig and re-run shfmt, there is no issue.

I would guess that editorconfig searches for root configs traversing up the file tree, so if you have your config in ~/.editorconfig it is expected for it to not be found when in /tmp

nord-erik avatar Aug 06 '25 20:08 nord-erik

@nord-erik is completely right. Per the EditorConfig spec, the config file must be in the same directory, or a parent directory, relative to the source file.

mvdan avatar Aug 06 '25 21:08 mvdan