Repository navigation
Don't strip newlines that sit directly before a comment - #8057
Merged
Merged
Conversation
When a parent layout constraint sets strip_newlines (e.g. spacing_within = single:inline on an expression), determine_constraints only checked whether the *previous* block was a comment before allowing the newline to be removed. If the *next* block was a comment, the newline in front of it got stripped, gluing the preceding token straight onto the -- marker and silently changing what the statement means. Check both the previous and the next block for comment segments, so a newline that sits immediately before a comment survives as well. Fixes sqlfluff#6801
CodeBlackwell
marked this pull request as ready for review
July 7, 2026 19:13
Contributor
Coverage Results ✅ |
peterbud
approved these changes
Jul 10, 2026
peterbud
left a comment
Contributor
There was a problem hiding this comment.
Thanks @CodeBlackwell !
1 task
benfdking
added a commit
to quarylabs/sqruff
that referenced
this pull request
Oct 5, 2026
Port SQLFluff commit 5f268847afc97b1ab8218d00d81add2ffb84e191 from sqlfluff/sqlfluff#8057. Do not strip a newline when the following block is a comment; add a regression test for an inline spacing constraint that previously glued AND to a line comment.
benfdking
added a commit
to quarylabs/sqruff
that referenced
this pull request
Oct 5, 2026
Port SQLFluff commit 5f268847afc97b1ab8218d00d81add2ffb84e191 from sqlfluff/sqlfluff#8057. Do not strip a newline when the following block is a comment; add a regression test for an inline spacing constraint that previously glued AND to a line comment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #6801
What was happening
When a layout constraint asks for newlines to be stripped between two tokens (for example
spacing_within = single:inlineon anexpression), the reflow code could delete a newline that sat right in front of a comment. The result was that the token before the comment got glued straight onto the--marker:came back as
which quietly changes what the statement means, since everything after
--is now commented out on that line.Root cause
In
determine_constraints(src/sqlfluff/utils/reflow/respace.py), the guard that keeps newlines around comments only looked at the previous block. If the previous block was a comment it kept the newline, but if the next block was the comment it did nothing, so an inheritedstrip_newlines=Truewent ahead and removed the newline in front of it.The fix
Check the next block for comment segments as well as the previous one. If either side of the point is a comment, we leave the newline alone. A newline that sits immediately before a comment now survives the respace.
Before / after
a AND\n-- comment\nb->a AND-- comment\nb(meaning changed)a AND\n-- comment\nb-> unchangedTests
Added
test_reflow__respace_does_not_strip_newline_before_commentintest/utils/reflow/respace_test.py, which sets thesingle:inlineexpression constraint and asserts the newline before the comment is preserved. The existing reflow suite (test/utils/reflow/) still passes.Summary by cubic
Stop stripping a newline that sits directly before a comment during respace. Prevents code being glued to the
--marker and changing SQL meaning. Fixes #6801.determine_constraints, also check the next block for comment segments; if previous or next is a comment, do not strip newlines.test_reflow__respace_does_not_strip_newline_before_comment; existing reflow tests still pass.Written for commit 56e06b5. Summary will update on new commits.