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

syntax: joins lines if escaped newlines aren't preceded by a space #953

Open
ainar-g opened this issue Dec 3, 2022 · 1 comment
Open

Comments

@ainar-g
Copy link

ainar-g commented Dec 3, 2022

In my scripts, I have a few commands that follow this general pattern:

env\
	VAR1='1'\
	VAR2='2'\
	somecommand\
	--big-flag-1='big_flag_value_1'\
	--big-flag-2='big_flag_value_2'\
	--big-flag-3='big_flag_value_3'\
	-x\
	-y\
	-z\
	file_with_a_very_long_name_1\
	file_with_a_very_long_name_2\
	file_with_a_very_long_name_3\
	file_with_a_very_long_name_4\
	;

This is line-oriented to simplify diffing, sorting, etc. I would expect shfmt -p to either leave the lines as-is (maybe adding or removing a space before the final \), but the result is rather unexpected and weird:

env VAR1='1' \
       VAR2='2' \
       somecommand --big-flag-1='big_flag_value_1' \
       --big-flag-2='big_flag_value_2' \
       --big-flag-3='big_flag_value_3' \
       -x -y -z file_with_a_very_long_name_1 file_with_a_very_long_name_2 file_with_a_very_long_name_3 file_with_a_very_long_name_4

I get the addition of extra spaces, but then it also combines all of the single-letter flags and all non-option arguments into one unreadable line. But not the envs or the long-form flags.

I feel like shfmt shouldn't interfere with the placement of arguments on separate lines at all, but if it should, it should also at the very least not produce these overlong lines of mixed short options and positional arguments.

@mvdan mvdan changed the title Joining of lines with options/arguments is inconsistent syntax: joining of lines with options/arguments is inconsistent Apr 16, 2023
@mvdan
Copy link
Owner

mvdan commented Apr 16, 2023

This appears to happen because you are using \ at the end of each line instead of \ (with a space):

$ cat spaced.bash
env \
	VAR1='1' \
	VAR2='2' \
	somecommand \
	--big-flag-1='big_flag_value_1' \
	--big-flag-2='big_flag_value_2' \
	--big-flag-3='big_flag_value_3' \
	-x \
	-y \
	-z \
	file_with_a_very_long_name_1 \
	file_with_a_very_long_name_2 \
	file_with_a_very_long_name_3 \
	file_with_a_very_long_name_4 \
	;
$ shfmt -d spaced.bash
[no diff]

I can't say why some arguments get a newline and some don't. I'm going to guess it's due to the same bug. Either way, there is a workaround for the bug - use spaces before your escaped newlines, which is the most common formatting as well.

@mvdan mvdan changed the title syntax: joining of lines with options/arguments is inconsistent syntax: joins lines if escaped newlines aren't preceded by a space Apr 16, 2023
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

2 participants