Skip to content

Ensure single trailing newline in source files - #6782

Draft
rhcarvalho wants to merge 1 commit into
phoenixframework:mainfrom
praialabs:ensure-src-single-trailing-newline
Draft

Ensure single trailing newline in source files#6782
rhcarvalho wants to merge 1 commit into
phoenixframework:mainfrom
praialabs:ensure-src-single-trailing-newline

Conversation

@rhcarvalho

Copy link
Copy Markdown
Contributor

Reduce future code churn by gating source files with a test that ensures tracked text files in the repository end with a single trailing newline (UNIX convention).

Alternatives to enforce this without a test:

An .editorconfig file at the root of the repository makes most editors automatically insert a final newline and trim trailing whitespace on save, preventing the problem at the source:

root = true

[*]
insert_final_newline = true
trim_trailing_whitespace = true
end_of_line = lf
charset = utf-8

A .gitattributes file makes Git itself flag whitespace issues in diffs, on apply, and on merge:

* text=auto eol=lf whitespace=blank-at-eol,blank-at-eof

Both files support per-path exceptions. In .editorconfig, a section like [*.png] can override any setting. In .gitattributes, paths can opt out with -whitespace or be marked as binary (e.g. *.png binary).

@rhcarvalho

Copy link
Copy Markdown
Contributor Author

This is if we want to go all in with ensuring source files (not only installer and generator templates, as in #6781) follow the UNIX convention. The two PRs are complementary, not mutually exclusive.

The .gitattributes approach would allow us to add something like git diff --check origin/main...HEAD to the CI workflow, so to check only the changes introduced by the PR, without flagging pre-existing issues in untouched files.

But it has one caveat: the .gitattributes whitespace checks (blank-at-eof, blank-at-eol) catch extra blank lines at EOF and trailing whitespace on lines, but they don't catch a missing final newline. Git shows the \ No newline at end of file marker in diffs, but --check doesn't flag it as an error. So .gitattributes + git diff --check covers most of the rule but not all of it.

@rhcarvalho
rhcarvalho force-pushed the ensure-src-single-trailing-newline branch from 7849f1b to fbaf32c Compare August 3, 2026 18:37
Reduce future code churn by gating source files with a test that ensures
tracked text files in the repository end with a single trailing newline
(UNIX convention).

Alternatives to enforce this without a test:

An .editorconfig file at the root of the repository makes most editors
automatically insert a final newline and trim trailing whitespace on
save, preventing the problem at the source:

    root = true

    [*]
    insert_final_newline = true
    trim_trailing_whitespace = true
    end_of_line = lf
    charset = utf-8

A .gitattributes file makes Git itself flag whitespace issues in diffs,
on apply, and on merge:

    * text=auto eol=lf whitespace=blank-at-eol,blank-at-eof

Both files support per-path exceptions. In .editorconfig, a section like
[*.png] can override any setting. In .gitattributes, paths can opt out
with -whitespace or be marked as binary (e.g. "*.png binary").
@rhcarvalho
rhcarvalho force-pushed the ensure-src-single-trailing-newline branch from fbaf32c to 67fa0be Compare August 3, 2026 18:41
@rhcarvalho

Copy link
Copy Markdown
Contributor Author

Rebased on top of main after #6781 has been merged.

Many of the trailing newlines in source files were handled there. The only "offender" left is the PR template in .github/ISSUE_TEMPLATE/bug_report.md, which can get an HTML comment in the trailing "describe expected" part to match the "describe actual" part.

### Expected behavior

<!--
Describe the expected behaviour.

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 noticed the template uses both "behavior" (US spelling) and "behaviour" (UK spelling, also technical term in Erlang/Elixir).

Maybe we could update towards one or the other for local consistency.

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.

1 participant