You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix: skip a file when clang-format fails to run - #550
clang-format advice is derived by running clang-format <file> and diffing its stdout against the file's current contents. But the exit status was ignored — stderr was only logged at debug level — and the (empty) stdout of a failed run was diffed anyway.
When clang-format exits non-zero, its stdout is empty, so the diff reports the entire file as needing reformatting. The consequences:
The report flags every line of the file.
With --fix / --gen-patch-path, the generated patch deletes the whole file's contents.
In PR-review mode, a suggestion to delete the file's content is posted.
Two realistic triggers:
An invalid --style value (eg. a typo like --style=googel).
--style=file with a .clang-format that uses a key the installed clang-format version does not recognise (a common version mismatch — clang-format exits with error: unknown key ...).
Reproduction (before this change):
mkdir t &&cd t && git init -q
printf'#include <cstdio>\nint main() {\n printf("hi");\n return 0;\n}\n'> a.cpp
git add .&& git -c user.name=t -c user.email=t@t commit -qm init
cpp-linter -l false --tidy-checks='-*' --style=googel -F
# => "does not conform ... (lines 1,2,3,4,5)" and an auto-fix.patch that deletes a.cpp
The v1 (Python) linter does not have this issue: it reads advice from --output-replacements-xml, so a failed run yields empty XML and therefore no advice.
Fix
After running clang-format, check output.status.success(). On failure, log clang-format's stderr at warning level (so the real cause is visible, not hidden in debug output) and skip the file with no format advice — matching the behaviour when clang-format reports no fixes. On success, stderr is still logged at debug as before.
Tests
nonzero_exit_skips_file_with_warning: a fake clang-format that exits non-zero with an error on stderr now produces one warning log and empty format advice (no whole-file deletion).
errors_reported_in_stderr updated to cover the success-with-stderr path (stderr still logged at debug on a successful run).
cargo nextest run (clang_format + run tests), cargo clippy --all-features --all-targets, and cargo fmt --check all pass.
Summary by CodeRabbit
Bug Fixes
Failed formatting runs are now reported as failures, with diagnostic details included in a warning. Format suggestions are no longer generated from unsuccessful runs.
When clang-format exits non-zero (eg. an invalid `--style` value or a
`.clang-format` config that the installed clang-format version rejects),
its stdout is empty. Diffing that empty output against the file's
contents reported the entire file as needing reformatting and, with
`--fix`/`--gen-patch-path`, produced a patch that deleted the whole
file (a PR review would post a suggestion to delete the file's content).
Check the exit status: on failure, log clang-format's stderr at warning
level and skip the file with no format advice, matching the behavior
when clang-format reports no fixes. On success, stderr is still logged
at debug as before.
run_clang_format now returns without diffing output or generating a patch when clang-format exits unsuccessfully. Tests cover this behavior and confirm that successful runs with stderr retain the existing debug-log path.
Changes: clang-format Failure Handling
Layer / File(s)
Summary
Exit status handling and tests cpp-linter/src/clang_tools/clang_format.rs
A nonzero exit logs a warning with stderr, sets empty format advice, and returns early. Tests cover this behavior and successful runs that write to stderr.
A file whose clang-format run fails is now skipped with a warning instead of getting a destructive whole-file patch. That file counts as clean in the final status, so a persistent formatter configuration failure could let checks pass. Owners should be aware of this, but it does not need to block the merge.
Pre-merge checks: 5
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title "fix: skip a file when clang-format fails to run" matches the main change. The raw summary says run_clang_format now treats any nonzero clang-format exit as a failed run. It logs a warning w…
Docstring Coverage
Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Linked Issues check
Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check
Check skipped because no linked issues were found for this pull request.
Full details: Title check
Explanation
The title "fix: skip a file when clang-format fails to run" matches the main change. The raw summary says run_clang_format now treats any nonzero clang-format exit as a failed run. It logs a warning with stderr, sets empty format advice, and stops before diffing output. The PR objectives describe the same behavior: a nonzero exit skips format advice for that file with a warning. The title is specific and uses the conventional commit prefix. It names the behavior change, not the implementation detail.
Full details: Docstring Coverage
Explanation
Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 1 files.
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
107-108: Major · Data Integrity & Integration · Heavy lift
Unverified finding
Verification ran but could not confirm this finding. It is shown for review, not as a verified issue.
Keep failed formatting checks distinct from clean files.
If clang-format rejects the configuration, this returns success with empty advice. post_feedback then reports zero failed formatting checks, and the pre-commit hook can pass without checking the files. A configured PR review can also approve them. (raw.githubusercontent.com)
Track tool failures separately from formatting advice and propagate them to the final status. Continue processing other files without generating a patch for the failed file.
Review info
Run configuration
Source mode: Auto
Repository: Unavailable
Branch: Unavailable
File: No configuration file from this repository applied
Reviewing files that changed from the base of the PR and between 8bf0fa8 and 6a17b53.
Files selected for processing: 1
cpp-linter/src/clang_tools/clang_format.rs
Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.
✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.89%. Comparing base (389dde6) to head (6a17b53). ⚠️ Report is 1 commits behind head on main.
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
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.
Problem
clang-format advice is derived by running
clang-format <file>and diffing its stdout against the file's current contents. But the exit status was ignored — stderr was only logged at debug level — and the (empty) stdout of a failed run was diffed anyway.When clang-format exits non-zero, its stdout is empty, so the diff reports the entire file as needing reformatting. The consequences:
--fix/--gen-patch-path, the generated patch deletes the whole file's contents.Two realistic triggers:
--stylevalue (eg. a typo like--style=googel).--style=filewith a.clang-formatthat uses a key the installed clang-format version does not recognise (a common version mismatch — clang-format exits witherror: unknown key ...).Reproduction (before this change):
The v1 (Python) linter does not have this issue: it reads advice from
--output-replacements-xml, so a failed run yields empty XML and therefore no advice.Fix
After running clang-format, check
output.status.success(). On failure, log clang-format's stderr at warning level (so the real cause is visible, not hidden in debug output) and skip the file with no format advice — matching the behaviour when clang-format reports no fixes. On success, stderr is still logged at debug as before.Tests
nonzero_exit_skips_file_with_warning: a fake clang-format that exits non-zero with an error on stderr now produces one warning log and empty format advice (no whole-file deletion).errors_reported_in_stderrupdated to cover the success-with-stderr path (stderr still logged at debug on a successful run).cargo nextest run(clang_format + run tests),cargo clippy --all-features --all-targets, andcargo fmt --checkall pass.Summary by CodeRabbit