Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trailing whitespace added at the start of a multi-line if condition #1104

Open
bradzacher opened this issue Oct 28, 2024 · 3 comments
Open

Comments

@bradzacher
Copy link

Input

if [[
  true ]]; then
  echo 1
fi

Expected

if [[
  true ]]; then
  echo 1
fi

Actual

if [[ 
  true ]]; then
  echo 1
fi

Note the trailing whitespace after the [[

Repro command

$ shfmt --version
3.10.0
$ shfmt --indent 2 --write /tmp/foo.sh

Additional Information

Ignore the fact that true could fit on one line -- the real code is a condition long enough to require line breaks to fit.
It's worth noting that part of me was also surprised that shfmt doesn't emit if [[ true ]] -- but that's separate to the issue of a trailing whitespace.

@paddy-hack
Copy link

paddy-hack commented Jan 7, 2025

Seeing something similar (using 3.8.0) for multi-line sub-shells even though .editorconfig says to trim them.

Original code is something like

(some_very_long_command | sed '$d';
 another_longish_command | tac;
 echo Hello)

On the first shfmt invocation, that becomes

(
    some_very_long_command | sed '$d';
    another_longish_command | tac;
    echo Hello
)

conform indent_size = 4 in .editorconfig, without any trailing spaces.
Re-running shfmt introduces three spaces after the first parenthesis, i.e spaces to line up with some_very_long_command.

In case it matters, shfmt runs from the directory that contains .editorconfig on a script in a subdirectory.

@giggio
Copy link

giggio commented Mar 10, 2025

I noticed this is coming from --keep-padding. If you remove it the bug is gone. As it is going away (see #658), maybe just remove it.

@paddy-hack
Copy link

Thanks. I've confirmed that keep_padding in my .editorconfigs triggers this.
I don't really remember why I added it but I guess I'll find out soon enough. either after removing keep_padding or when I get hit by v4 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants
@giggio @paddy-hack @bradzacher and others