Sitelet https://github.com/starkware-libs/cairo/pull/10057
Skip to content

fix(formatter): drop trailing empty lines when building the output - #10057

Merged
orizi merged 1 commit into
mainfrom
orizi/06-07-fix_formatter_drop_trailing_empty_lines_when_building_the_output
Jun 8, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/06-07-fix_formatter_drop_trailing_empty_lines_when_building_the_output

Conversation

@orizi

@orizi orizi commented Jun 7, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes the Cairo formatter to strip extra trailing newlines from formatted output. Previously, files ending with a trailing comment (or other constructs that produced empty trailing lines) would have an extra blank line appended. The LineBuilder::build method now removes any empty trailing lines before joining, ensuring the output ends with exactly one newline. Trailing newlines have also been removed from several corelib and test files to reflect the corrected formatter behavior.

A new formatter test case (trailing_comment.cairo) is added to cover files that end with a comment and no trailing blank line.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

The formatter was emitting an extra trailing newline at the end of files that ended with certain constructs such as trailing comments. This caused formatted output to differ from the expected single-newline termination, producing spurious diffs when formatting was applied repeatedly.


What was the behavior or documentation before?

Files ending with a trailing comment or similar construct would be formatted with an extra blank line appended at the end of the file.


What is the behavior or documentation after?

The formatter strips any empty trailing lines before emitting output, so files end with exactly one newline regardless of their content. Files that previously had a spurious trailing newline in the corelib and test suite have been corrected to match.


Related issue or discussion (if any)

None specified.


Additional context

The fix is localized to LineBuilder::build in formatter_impl.rs, where a loop removes empty strings from the end of the line vector before joining them with "\n" and appending the final newline.

  A single-line comment sets `empty_lines_allowance = 1`; when the comment is the
  last element before end-of-file, that adds a trailing empty-line break point,
  which `LineBuilder::build` rendered as a blank line before the final newline -
  accumulating on every reformat (`format(format(x)) != format(x)`). Drop trailing
  empty lines from the line list in `build`.
  Memory updated + pushed (with your "more internal" rationale recorded).
@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@orizi
orizi marked this pull request as ready for review June 7, 2026 14:08
@cursor

cursor Bot commented Jun 7, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Localized formatter output change with no runtime or security impact; risk is limited to formatting diffs on files that previously had an extra trailing blank line.

Overview
Fixes extra blank lines at EOF when formatting files that end with trailing comments (or similar constructs that produced empty trailing line segments).

LineBuilder::build now strips empty strings from the end of the line vector after break_line_tree, then joins and appends a single final \n, so output ends with exactly one newline instead of an extra blank line.

Adds formatter regression coverage via trailing_comment.cairo. Several corelib, Starknet contract, and test .cairo files drop a spurious trailing blank line so they match the corrected formatter output.

Reviewed by Cursor Bugbot for commit 1fb1213. Bugbot is set up for automated code reviews on this repo. Configure here.

@eytan-starkware eytan-starkware left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

:lgtm:

@eytan-starkware reviewed 14 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on TomerStarkware).

@orizi
orizi added this pull request to the merge queue Jun 8, 2026
Merged via the queue into main with commit 422de73 Jun 8, 2026
54 checks passed
@orizi
orizi deleted the orizi/06-07-fix_formatter_drop_trailing_empty_lines_when_building_the_output branch June 8, 2026 09:58
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.

3 participants