Skip to content

Commit

Permalink
Address code review feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jul 24, 2024
1 parent b396613 commit 0dcb095
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
2 changes: 1 addition & 1 deletion crates/red_knot/src/watch/workspace_watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl WorkspaceWatcher {
}

pub fn update(&mut self, db: &RootDatabase) {
let new_watch_paths = db.workspace().watch_paths(db);
let new_watch_paths = db.workspace().paths_to_watch(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();
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ impl Workspace {
/// Returns the paths that should be watched.
///
/// The paths that require watching might change with every revision.
pub fn watch_paths(self, db: &dyn Db) -> FxHashSet<SystemPathBuf> {
pub fn paths_to_watch(self, db: &dyn Db) -> FxHashSet<SystemPathBuf> {
ruff_db::system::deduplicate_nested_paths(
std::iter::once(self.root(db)).chain(system_module_search_paths(db.upcast())),
)
Expand Down
56 changes: 26 additions & 30 deletions crates/red_knot/tests/file_watching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,23 @@ impl TestCase {
all_events
}

fn update_search_path_settings(
&mut self,
f: impl FnOnce(&SearchPathSettings) -> SearchPathSettings,
) {
let program = Program::get(self.db());
let search_path_settings = program.search_paths(self.db());

let new_settings = f(search_path_settings);

program.set_search_paths(&mut self.db).to(new_settings);

if let Some(watcher) = &mut self.watcher {
watcher.update(&self.db);
assert!(!watcher.has_errored_paths());
}
}

fn collect_package_files(&self, path: &SystemPath) -> Vec<File> {
let package = self.db().workspace().package(self.db(), path).unwrap();
let files = package.files(self.db());
Expand Down Expand Up @@ -673,22 +690,11 @@ fn add_search_path() -> anyhow::Result<()> {

assert!(resolve_module(case.db().upcast(), ModuleName::new_static("a").unwrap()).is_none());

let program = Program::get(case.db());
let search_path_settings = program.search_paths(case.db()).clone();

// Register site-packages as a search path.
program
.set_search_paths(&mut case.db)
.to(SearchPathSettings {
site_packages: Some(site_packages.clone()),
..search_path_settings
});

// Start observing the site-packages folder
let watcher = case.watcher.as_mut().unwrap();
watcher.update(&case.db);

assert!(!watcher.has_errored_paths());
case.update_search_path_settings(|settings| SearchPathSettings {
site_packages: Some(site_packages.clone()),
..settings.clone()
});

std::fs::write(site_packages.join("a.py").as_std_path(), "class A: ...")?;
std::fs::write(site_packages.join("__init__.py").as_std_path(), "")?;
Expand All @@ -714,22 +720,12 @@ fn remove_search_path() -> anyhow::Result<()> {
}
})?;

// Remove site packages from the search path settings.
let site_packages = case.root_path().join("site_packages");

let program = Program::get(case.db());
let search_path_settings = program.search_paths(case.db()).clone();

// Register site-packages from the search paths.
program
.set_search_paths(&mut case.db)
.to(SearchPathSettings {
site_packages: None,
..search_path_settings
});

let watcher = case.watcher.as_mut().unwrap();
watcher.update(&case.db);
assert!(!watcher.has_errored_paths());
case.update_search_path_settings(|settings| SearchPathSettings {
site_packages: None,
..settings.clone()
});

std::fs::write(site_packages.join("a.py").as_std_path(), "class A: ...")?;

Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_db/src/system/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,9 @@ impl<'a> DeduplicatedNestedPathsIter<'a> {
I: IntoIterator<Item = &'a SystemPath>,
{
let mut paths = paths.into_iter().collect::<Vec<_>>();
// Sort the path to ensure that e.g. `/a/b/c`, comes right after `/a/b`.
paths.sort_unstable();

let mut iter = paths.into_iter();

Self {
Expand Down

0 comments on commit 0dcb095

Please sign in to comment.