-
-
Notifications
You must be signed in to change notification settings - Fork 7
Fix tidy review write conflicts #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix tidy review write conflicts #130
Conversation
cpp_linter/clang_tools/clang_tidy.py
Outdated
timeout = time.monotonic_ns() + 1000000 | ||
# wait 1s for file to become accessible | ||
success = False | ||
while time.monotonic_ns() < timeout: | ||
try: | ||
src = open(filename, "r+b") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this timeout idea works reliably?
Would really be great to have a reproducer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It fixes the issue from the code base in the original issue ticket. I tried it several times with the header file changed to have tidy fixes found and with no tidy fixes to be found and all times the review output the correct information.
Not sure how easy it is to make a public reproducible test case, in theory anything with modified cpp files that all include the same header and that header has also been modified. Run it on a windows github action and see what it does. Im not familiar with your test suite so ill leave that to you
Co-authored-by: Brendan <2bndy5@gmail.com>
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #130 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 24 24
Lines 1837 1854 +17
=========================================
+ Hits 1837 1854 +17 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without a way to reproduce, our tests can't cover the lines specific to this problem. Instead I think some # pragma: no cover
comments would suffice.
This comment was marked as resolved.
This comment was marked as resolved.
5eaa197
to
30a0e34
Compare
Co-authored-by: Brendan <2bndy5@gmail.com>
Tested an seems to work correctly, updated the PR to match |
WalkthroughThe changes in this pull request involve modifications to the Changes
Assessment against linked issues
Suggested labels
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (1)
cpp_linter/clang_tools/clang_tidy.py (1)
285-285
: Usewrite()
instead ofwritelines()
for writing byte contentSince
original_buf
is a bytes object containing the entire file content, usingsrc.write(original_buf)
is more appropriate thansrc.writelines([original_buf])
, which is intended for writing a list of lines.Apply this diff:
-src.writelines([original_buf]) +src.write(original_buf)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
cpp_linter/clang_tools/__init__.py
(1 hunks)cpp_linter/clang_tools/clang_tidy.py
(3 hunks)
🔇 Additional comments (3)
cpp_linter/clang_tools/clang_tidy.py (2)
8-8
: Imports are correctly updated
The time
module is imported to handle time-related functions used in the timeout mechanism.
250-250
: Successfully stored original file content for restoration
Reading the original file content to original_buf
ensures that the file can be restored after clang-tidy modifications.
cpp_linter/clang_tools/__init__.py (1)
49-60
: Reordering formatting before tidying ensures code is formatted even if clang-tidy fails
By running clang-format
before clang-tidy
, the code will be formatted even if clang-tidy
encounters an error. This change enhances the robustness of the formatting process.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (1)
cpp_linter/clang_tools/clang_tidy.py (1)
273-274
: Optimize busy-wait loops to reduce CPU usage.The current implementation uses busy-wait loops that could consume unnecessary CPU cycles. Consider adding a small sleep to reduce CPU usage.
- while not src.readable() and time.monotonic_ns() < read_timeout: - pass # pragma: no cover + while not src.readable() and time.monotonic_ns() < read_timeout: + time.sleep(0.01) # pragma: no cover - while not src.writable() and time.monotonic_ns() < write_timeout: - pass # pragma: no cover + while not src.writable() and time.monotonic_ns() < write_timeout: + time.sleep(0.01) # pragma: no coverAlso applies to: 282-283
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
cpp_linter/clang_tools/clang_tidy.py
(3 hunks)
🔇 Additional comments (2)
cpp_linter/clang_tools/clang_tidy.py (2)
8-8
: LGTM!
The time
module import is correctly placed and necessary for the new timeout functionality.
264-299
: LGTM! Robust implementation of file access with timeouts.
The implementation correctly handles file access issues with proper timeouts and error handling. The use of context managers ensures proper resource cleanup, and the separate timeouts for different operations provide fine-grained control.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Outside diff range and nitpick comments (1)
cpp_linter/clang_tools/clang_tidy.py (1)
264-299
: Consider extracting file operations into helper functions.The current implementation has complex nested structures. Consider extracting the file operations into helper functions to improve readability and maintainability.
Here's a suggested refactoring:
def _wait_for_file_operation(timeout_ns: int, check_fn) -> bool: while time.monotonic_ns() < timeout_ns: if check_fn(): return True return False def write_back_original_content(filename: str, original_buf: bytes, advice: TidyAdvice) -> bool: timeout = time.monotonic_ns() + 1_000_000_000 try: with open(filename, "r+b") as src: if not _wait_for_file_operation( time.monotonic_ns() + 1_000_000_000, lambda: src.readable() ): return False advice.patched = b"".join(src.readlines()) src.seek(0) if not _wait_for_file_operation( time.monotonic_ns() + 1_000_000_000, lambda: src.writable() ): return False src.write(original_buf) return True except OSError as exc: logger.error( "Failed to write back contents of file: %s. %s", filename, f"{exc.__class__.__name__}: {', '.join(exc.args)}", ) return False # Usage in run_clang_tidy: if tidy_review: original_buf = Path(filename).read_bytes() cmds.append("--fix-errors") # ... rest of the code ... if not write_back_original_content(filename, original_buf, advice): logger.error("Failed to write back contents of file: %s. timeout.", filename)
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
cpp_linter/clang_tools/clang_tidy.py
(3 hunks)
🔇 Additional comments (2)
cpp_linter/clang_tools/clang_tidy.py (2)
8-8
: LGTM!
The time
module import is correctly placed with other standard library imports and is necessary for the timeout implementation.
250-250
:
Add timeout and error handling for file read operation.
The file read operation should have similar timeout and error handling mechanisms as implemented for write operations to ensure consistency and robustness.
Apply this diff to implement consistent error handling:
- original_buf = Path(filename).read_bytes()
+ timeout = time.monotonic_ns() + 1_000_000_000
+ success = False
+ exception = None
+ while not success and time.monotonic_ns() < timeout:
+ try:
+ original_buf = Path(filename).read_bytes()
+ success = True
+ except OSError as exc:
+ exception = exc
+ if not success:
+ error_msg = " timeout." if not exception else f" {exception.__class__.__name__}: {', '.join(exception.args)}"
+ logger.error("Failed to read contents of file: %s.%s", filename, error_msg)
+ return advice
Likely invalid or redundant comment.
Superseded by #133. In #133, I went with
@maoliver-amd I'm sorry to have put you through all that and yet I went in a slightly different direction. |
This fixes write failures when writing back the unmodified contents of files modified by clang-tidy.
Closes #129
Summary by CodeRabbit
New Features
Bug Fixes
Refactor
_run_on_single_file
function for better readability.