Skip to content
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: graceful error handling for grit check in uninitialized contexts #61

Merged
merged 2 commits into from
Mar 23, 2024

Conversation

mparker3
Copy link
Contributor

@mparker3 mparker3 commented Mar 22, 2024

(hopefully!) no functional changes.

calling grit check before grit init right now panics:

❯ marzano check
thread '<unnamed>' panicked at /Users/mattparker/code/gritql/crates/gritcache/src/new_cache.rs:43:65:
called `Result::unwrap()` on an `Err` value: Failed to open cache file

Caused by:
    No such file or directory (os error 2)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Checking no_println_in_lsp
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0/1thread 'main' panicked at /Users/mattparker/code/gritql/crates/cli/src/commands/check.rs:197:58:
called `Result::unwrap()` on an `Err` value: sending on a closed channel

fix results in:

    ❯ marzano check
Error: Failed to read cache file /Users/mattparker/.cargo/.grit/.gritmodules/mismatches_cache

Caused by:
    No such file or directory (os error 2)

probably not a super common occurence but it was one of the first things I ran into test driving the CLI, can replicate with the added test in the absence of the fix. Happy to take this PR in a different direction if needed

Summary by CodeRabbit

  • Refactor
    • Simplified error handling in cache operations to enhance reliability.
    • Improved testing for cache behavior when encountering missing files.

Copy link
Contributor

coderabbitai bot commented Mar 22, 2024

Walkthrough

The recent update streamlines the error handling within the ThreadedCache component, making the system more efficient and easier to debug. By eliminating unnecessary file existence checks and consolidating error responses, the codebase becomes cleaner and more straightforward. An additional test ensures the robustness of the error handling mechanism, particularly when the cache cannot be refreshed due to absent files.

Changes

File Path Change Summary
.../gritcache/src/new_cache.rs Simplified error handling in ThreadedCache, removed file existence check, unified file read error responses, and added an assertion for testing error behavior with no file and disabled cache refresh.

🐇✨
In the realm of code, where the bits do hop,
A rabbit tweaked the cache, with a skip and a bop.
No more checks in the night, for files that aren't there,
Just a smooth error flow, with nary a care.
So here's to the changes, may they make your code light,
As it runs through the tunnels, swift and bright.
🌟🐾

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?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between cf59ebf and 4173cc9.
Files selected for processing (1)
  • crates/gritcache/src/new_cache.rs (2 hunks)
Additional comments: 1
crates/gritcache/src/new_cache.rs (1)
  • 182-184: The addition of this test case effectively validates the improved error handling behavior when the cache file is missing, and cache refresh is disabled. This is a valuable addition to ensure the robustness of the application.

Comment on lines 69 to 72
return Err(e).context(format!(
"Failed to read cache file {}",
path.to_string_lossy()
));
Copy link
Contributor

Choose a reason for hiding this comment

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

The simplification of error handling by removing specific checks for file existence is a positive change. However, consider adding more context to the error message to inform the user about potential actions, such as running grit init if the cache file does not exist.

-                return Err(e).context(format!(
-                    "Failed to read cache file {}",
-                    path.to_string_lossy()
-                ));
+                return Err(e).context(format!(
+                    "Failed to read cache file {}. Please ensure the cache has been initialized by running `grit init`.",
+                    path.to_string_lossy()
+                ));

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
return Err(e).context(format!(
"Failed to read cache file {}",
path.to_string_lossy()
));
return Err(e).context(format!(
"Failed to read cache file {}. Please ensure the cache has been initialized by running `grit init`.",
path.to_string_lossy()
));

Copy link
Contributor

@morgante morgante left a comment

Choose a reason for hiding this comment

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

Thanks for contributing! This doesn't look quite right, since if the cache doesn't exist we actually want to initialize an empty cache so it could be saved afterwards.

Can you adjust this so that's preserved (and add a test confirming it)?

@mparker3
Copy link
Contributor Author

mparker3 commented Mar 23, 2024

playing with this a little more -- the creation logic for the backing file is correct as implemented assuming exists on the FS, so this definitely isn't the right fix.

EDIT: setting a GRIT_CACHE_DIR or creating a .gritmodules dir got the command running correctly as is, for clarity

would a better approach be to add a check here that the cache_dir in question exists + bail if not w/ a message to grit init? (vs. trying to do .grit/.gritmodules initialization ad-hoc)

@morgante
Copy link
Contributor

Yeah, I think we want to do it this way:

  • If the directories don't exist, fail gracefully with a message about the cache
  • If the directories exist (grit init ran), initialize like we did here.

would a better approach be to add a check here that the cache_dir in question exists + bail if not w/ a message to grit init? (vs. trying to do .grit/.gritmodules initialization ad-hoc)

Generally we should avoid doing pre-emptive checks but instead handle failures if they come up. I think it might actually be fine to edit this so instead of panicking that thread gracefully terminates with a suggestion to run grit init first.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 4173cc9 and 9df7895.
Files selected for processing (1)
  • crates/gritcache/src/new_cache.rs (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • crates/gritcache/src/new_cache.rs

@@ -39,8 +39,8 @@ impl ThreadedCache {
};

let (sender, receiver) = mpsc::channel::<HashKey>();
let mut writer = Self::new_writer(&mismatches_path)?;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@morgante let me know if there's a reason not to do this i'm missing - seems more straightforward to bail during creation than to do so from the writer thread

Copy link
Contributor

Choose a reason for hiding this comment

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

This looks fine - if the compiler is happy, I'm happy.

@mparker3 mparker3 changed the title remove handling for NotFound error in grit cache initialization fix: graceful error handling for grit check in uninitialized contexts Mar 23, 2024
@@ -39,8 +39,8 @@ impl ThreadedCache {
};

let (sender, receiver) = mpsc::channel::<HashKey>();
let mut writer = Self::new_writer(&mismatches_path)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks fine - if the compiler is happy, I'm happy.

@morgante morgante merged commit ff58de2 into getgrit:main Mar 23, 2024
3 of 5 checks passed
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.

2 participants