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

wip: syntax: flush heredocs before right parens #924

Closed
wants to merge 1 commit into from
Closed

wip: syntax: flush heredocs before right parens #924

wants to merge 1 commit into from

Conversation

andreykaipov
Copy link
Contributor

Fixes #923 😄

e.g. when using heredocs in command substitutions, process
substitutions, and subshells
},
{
"diff -y <(cat <<EOF\n1\n2\n3\nEOF\n) <(cat <<EOF\n1\n4\n3\nEOF\n)",
"diff -y <(cat \\\n<<EOF\n1\n2\n3\nEOF\n) <(\ncat <<EOF\n1\n4\n3\nEOF\n)",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something I realized while writing these examples is minified heredocs might not necessarily be minified! For example the following is 21 bytes:

a=$(cat <<EOF
EOF
)

And produces the following 22 bytes:

a=$(cat \
<<EOF
EOF
)

It's because of the cat \, but digging through the code I couldn't immediately see what causing that, or how to address it without breaking too much else. In the meantime, I figure valid shell is a lot better than invalid shell though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff example in the test is very funky, because the minified code has the first cat continuing its <<EOF onto the next line, but the next cat in the second proc subst is left alone as cat <<EOF.

@andreykaipov
Copy link
Contributor Author

Oh no, sorry this is embarrassing... I foolishly was only running go test ./... -run=TestPrintMinify during my testing...

This change as it is breaks a few other wacky cases, like:

foo <<EOF | $(bar)
3
EOF

I did some more digging through the code to see if I can satisfy both (the existing test case of a subshell after the beginning heredoc word, and the new one I added where the heredoc is within a subshell), but I think it would involve a new printer field to keep track of if we're in a heredoc or not.

@andreykaipov andreykaipov changed the title syntax: flush heredocs before right parens wip: syntax: flush heredocs before right parens Oct 2, 2022
@mvdan
Copy link
Owner

mvdan commented Nov 18, 2022

Thanks for the PR! I did see it, I just haven't had the time to sit down and debug what's going on :)

@mvdan
Copy link
Owner

mvdan commented Apr 15, 2023

Finally figured it out, it was a bit subtle :) I also noticed that the escaped newlines right before << weren't necessary with the minify mode, that's fixed as well. Fix coming shortly, and I'll credit you for the test cases. Thank you!

@mvdan mvdan closed this Apr 15, 2023
@andreykaipov
Copy link
Contributor Author

Oh - thanks! Sorry this got lost in the inbox. Time sure does fly. Thanks so much! Really appreciate you and this project! 🥳

@andreykaipov andreykaipov deleted the bugfix/heredocs-right-parens branch May 29, 2023 21:40
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

Successfully merging this pull request may close these issues.

Minifying heredoc values in command substitutions produces invalid shell
2 participants