Skip to content

Fix trailing newline bug - #1323

Merged
woodruffw merged 10 commits into
zizmorcore:mainfrom
mostafa:fix-trailing-newline-bug
Nov 9, 2025
Merged

woodruffw merged 10 commits into
zizmorcore:mainfrom
mostafa:fix-trailing-newline-bug

Conversation

@mostafa

@mostafa mostafa commented Nov 7, 2025

Copy link
Copy Markdown
Contributor

Pre-submission checks

Please check these boxes:

  • Mandatory: This PR corresponds to an issue (if not, please create
    one first).

  • I hereby disclose the use of an LLM or other AI coding assistant in the
    creation of this PR. PRs will not be rejected for using AI tools, but
    will be rejected for undisclosed use.

If a checkbox is not applicable, you can leave it unchecked.

Summary

Fixed trailing newline removal when adding cooldown config to dependabot files by preserving the document's trailing newline in yamlpatch's Op::Add handler if the original document ended with one. This fixes #1315.

Test Plan

Added a test case to the yamlpatch unit-tests.

Comment thread crates/yamlpatch/src/lib.rs Outdated
Comment on lines +358 to +362
// Preserve trailing newline if original document had one and result doesn't
// This handles the case where adding content at the end removes the trailing newline
if content.ends_with('\n') && !result.ends_with('\n') {
result.push('\n');
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Huh, I'm kind of confused as to how this happens -- my understanding of tree-sitter-yaml is that our parse is "greedy" i.e. always includes the trailing newline, so I don't understand how we end up losing it.

(Also, do we need to do this newline check more generally, or only for the Add op?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Tree-sitter's feature byte span (node.start_byte() to node.end_byte()) ends at the feature's content, so a trailing newline after the feature is outside the span. Replacing only that span drops the trailing newline if the replacement doesn't include it. I'll update the code to fix it in all places along with more tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I tried to reimplement the fix in a better way and fix it for Replace, ReplaceComment, RewriteFragment and Add operations. Also, I added more unit-tests.

Comment thread crates/yamlpatch/src/lib.rs Resolved
Signed-off-by: William Woodruff <william@yossarian.net>
Comment on lines +276 to +277
let replacement_range = compute_replacement_range(&feature, document);
patched_content.replace_range(replacement_range, &patched_feature);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Not a blocker, just thinking out loud: it's not super ideal that we need these helpers now, since I could easily see us forgetting to call them in relevant places in the future. We might want to do more type state encoding here, i.e. have a RawSpan and AdjustedSpan or whatever and only allow replacements on ranges with the latter.

(That isn't necessarily the right approach either, just something that might help.)

Signed-off-by: William Woodruff <william@yossarian.net>
Signed-off-by: William Woodruff <william@yossarian.net>
Signed-off-by: William Woodruff <william@yossarian.net>
@woodruffw woodruffw added bugfix Fixes a known bug autofix Auto-fix functionality labels Nov 9, 2025
@woodruffw

Copy link
Copy Markdown
Member

Thanks @mostafa!

@woodruffw
woodruffw merged commit 2ebb910 into zizmorcore:main Nov 9, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autofix Auto-fix functionality bugfix Fixes a known bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: zizmor --fix=safe does not preserve trailing newlines when adding cooldown config to dependabot config

2 participants