-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[red-knot] Watch search paths #12407
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
use crate::db::RootDatabase; | ||
use crate::watch::Watcher; | ||
use ruff_db::system::SystemPathBuf; | ||
use rustc_hash::FxHashSet; | ||
use std::fmt::{Formatter, Write}; | ||
use tracing::info; | ||
|
||
/// Wrapper around a [`Watcher`] that watches the relevant paths of a workspace. | ||
pub struct WorkspaceWatcher { | ||
watcher: Watcher, | ||
|
||
/// The paths that need to be watched. This includes paths for which setting up file watching failed. | ||
watched_paths: FxHashSet<SystemPathBuf>, | ||
|
||
/// Paths that should be watched but setting up the watcher failed for some reason. | ||
/// This should be rare. | ||
errored_paths: Vec<SystemPathBuf>, | ||
} | ||
|
||
impl WorkspaceWatcher { | ||
/// Create a new workspace watcher. | ||
pub fn new(watcher: Watcher, db: &RootDatabase) -> Self { | ||
let mut watcher = Self { | ||
watcher, | ||
watched_paths: FxHashSet::default(), | ||
errored_paths: Vec::new(), | ||
}; | ||
|
||
watcher.update(db); | ||
|
||
watcher | ||
} | ||
|
||
pub fn update(&mut self, db: &RootDatabase) { | ||
let new_watch_paths = db.workspace().watch_paths(db); | ||
|
||
let mut added_folders = new_watch_paths.difference(&self.watched_paths).peekable(); | ||
let mut removed_folders = self.watched_paths.difference(&new_watch_paths).peekable(); | ||
|
||
if added_folders.peek().is_none() && removed_folders.peek().is_none() { | ||
return; | ||
} | ||
|
||
for added_folder in added_folders { | ||
// Log a warning. It's not worth aborting if registering a single folder fails because | ||
// Ruff otherwise stills works as expected. | ||
if let Err(error) = self.watcher.watch(added_folder) { | ||
// TODO: Log a user-facing warning. | ||
tracing::warn!("Failed to setup watcher for path '{added_folder}': {error}. You have to restart Ruff after making changes to files under this path or you might see stale results."); | ||
self.errored_paths.push(added_folder.clone()); | ||
} | ||
} | ||
|
||
for removed_path in removed_folders { | ||
if let Some(index) = self | ||
.errored_paths | ||
.iter() | ||
.position(|path| path == removed_path) | ||
{ | ||
self.errored_paths.swap_remove(index); | ||
continue; | ||
} | ||
|
||
if let Err(error) = self.watcher.unwatch(removed_path) { | ||
info!("Failed to remove the file watcher for the path '{removed_path}: {error}."); | ||
} | ||
} | ||
|
||
info!( | ||
"Set up file watchers for {}", | ||
DisplayWatchedPaths { | ||
paths: &new_watch_paths | ||
} | ||
); | ||
|
||
self.watched_paths = new_watch_paths; | ||
} | ||
|
||
/// Returns `true` if setting up watching for any path failed. | ||
pub fn has_errored_paths(&self) -> bool { | ||
!self.errored_paths.is_empty() | ||
} | ||
|
||
pub fn flush(&self) { | ||
self.watcher.flush(); | ||
} | ||
|
||
pub fn stop(self) { | ||
self.watcher.stop(); | ||
} | ||
} | ||
|
||
struct DisplayWatchedPaths<'a> { | ||
paths: &'a FxHashSet<SystemPathBuf>, | ||
} | ||
|
||
impl std::fmt::Display for DisplayWatchedPaths<'_> { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
f.write_char('[')?; | ||
|
||
let mut iter = self.paths.iter(); | ||
if let Some(first) = iter.next() { | ||
write!(f, "\"{first}\"")?; | ||
|
||
for path in iter { | ||
write!(f, ", \"{path}\"")?; | ||
} | ||
} | ||
|
||
f.write_char(']') | ||
} | ||
} |
This file contains 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
Oops, something went wrong.
Oops, something went wrong.
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.
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 use a
Vec
here because this should almost always be empty and searching small vecs tends to be faster than hash maps, not that it matters much 😆