8000
Skip to content

Add pre-commit hooks to fix common formatting issues causing CI failures - #9494

Merged
jesserockz merged 3 commits into
devfrom
legacy_precommit
Jul 15, 2025
Merged

Add pre-commit hooks to fix common formatting issues causing CI failures#9494
jesserockz merged 3 commits into
devfrom
legacy_precommit

Conversation

@bdraco
@bdraco bdraco commented Jul 14, 2025
Copy link
Copy Markdown
Member

What does this implement/fix?

This PR adds the end-of-file-fixer and trailing-whitespace pre-commit hooks to automatically fix common formatting issues that frequently cause CI failures. These simple additions will prevent numerous CI failures and save significant time and resources that are currently wasted on fixing these issues manually.

The hooks automatically fix:

  • end-of-file-fixer: Ensures all files end with exactly one newline character
    • Files that don't end with a newline
    • Files that end with multiple newlines
  • trailing-whitespace: Removes trailing whitespace from all lines

This standardization helps maintain consistent file formatting across the codebase and eliminates common sources of CI failures.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Code quality improvements to existing code or addition of tests
  • Other

Related issue or feature (if applicable): N/A

Pull request in esphome-docs with documentation (if applicable): N/A

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040
  • BK72xx
  • RTL87xx

Example entry for config.yaml:

# N/A - This is a development tooling change

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

Additional changes:

  1. Added end-of-file-fixer and trailing-whitespace hooks to .pre-commit-config.yaml
  2. Modified script/clang_tidy_hash.py to ensure consistent newline handling when writing the hash file
  3. Excluded .clang-tidy and .clang-format files from yamllint since they use special formatting

Benefits:

  • Reduces CI failures: No more failing builds due to missing/extra newlines or trailing whitespace
  • Saves time: Developers won't need to manually fix formatting issues
  • Saves resources: Fewer CI re-runs means less compute resources wasted
  • Consistency: All files will have standardized formatting
  • Automatic: The fixes happen automatically during pre-commit, no manual intervention needed

Copilot AI review requested due to automatic review settings July 14, 2025 17:44
Copilot AI 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.

Pull Request Overview

Adds automation and consistency for end-of-file newline handling by integrating a pre-commit hook and normalizing newline output in tooling.

  • Introduce end-of-file-fixer hook and exclude certain files from yamllint
  • Update script/clang_tidy_hash.py to strip and enforce a single trailing newline when writing the hash
  • Remove stray trailing blank lines/dashes across numerous YAML fixtures and CI config

Reviewed Changes

8000

Copilot reviewed 33 out of 35 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/integration/fixtures/*.yaml Removed extra trailing - lines to enforce single newline policy
tests/components/**/*.yaml/.gitattributes Cleared stray trailing - or blank lines per hook requirement
script/clang_tidy_hash.py Strip and append exactly one newline when writing .clang-tidy.hash
.pre-commit-config.yaml Bump pre-commit-hooks rev, add end-of-file-fixer, exclude lint
.github/workflows/ci-clang-tidy-hash.yml Remove extra trailing blank line
Comments suppressed due to low confidence (2)

script/clang_tidy_hash.py:126

  • Update the docstring to reflect that write_hash now normalizes trailing newlines (strips extras and enforces exactly one).
def write_hash(hash_value: str) -> None:

script/clang_tidy_hash.py:130

  • [nitpick] Consider adding a unit test for write_hash to verify it always writes exactly one newline, preventing future regressions.
    write_file_content(hash_file, hash_value.strip() + "\n")

@bdraco bdraco changed the title Add end-of-file-fixer pre-commit hook to reduce CI failures Add pre-commit hooks to fix common formatting issues causing CI failures Jul 14, 2025
@bdraco
bdraco commented Jul 14, 2025
Copy link
Copy Markdown
Member Author

We can also remove the separate yamllint job after this as pre-commit run --all-files passes now

That will save even more ci time

@codecov-commenter
codecov-commenter commented Jul 14, 2025
Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 59.70%. Comparing base (e7d819a) to head (0e22933).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #9494      +/-   ##
==========================================
+ Coverage   59.67%   59.70%   +0.02%     
==========================================
  Files          50       50              
  Lines       10268    10268              
  Branches     1379     1379              
==========================================
+ Hits         6127     6130       +3     
+ Misses       3782     3780       -2     
+ Partials      359      358       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jesserockz
jesserockz merged commit b2a8b0a into dev Jul 15, 2025
27 checks passed
@jesserockz
jesserockz deleted the legacy_precommit branch July 15, 2025 00:25
@bdraco
bdraco commented Jul 15, 2025
Copy link
Copy Markdown
Member Author

Thanks. This one was particularly annoying to fix to fix manually.

@github-actions github-actions Bot locked and limited conversation to collaborators Jul 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

0