diff --git a/Cargo.lock b/Cargo.lock index 5927b9afca44b..34561deb3ce5b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3093,7 +3093,6 @@ dependencies = [ "etcetera", "filetime", "get-size2", - "glob", "ignore", "insta", "matchit", diff --git a/crates/ruff_db/Cargo.toml b/crates/ruff_db/Cargo.toml index f3db4a9673f09..1cfaca130752a 100644 --- a/crates/ruff_db/Cargo.toml +++ b/crates/ruff_db/Cargo.toml @@ -30,7 +30,6 @@ dashmap = { workspace = true } dunce = { workspace = true } filetime = { workspace = true } get-size2 = { workspace = true } -glob = { workspace = true } ignore = { workspace = true, optional = true } matchit = { workspace = true } path-slash = { workspace = true } diff --git a/crates/ruff_db/src/system.rs b/crates/ruff_db/src/system.rs index 9bd34c7432502..caf7d78e45e3c 100644 --- a/crates/ruff_db/src/system.rs +++ b/crates/ruff_db/src/system.rs @@ -1,4 +1,3 @@ -pub use glob::PatternError; pub use memory_fs::MemoryFileSystem; #[cfg(all(feature = "testing", feature = "os"))] @@ -11,9 +10,8 @@ use filetime::FileTime; use ruff_notebook::{Notebook, NotebookError}; use ruff_python_ast::PySourceType; use std::error::Error; +use std::fmt; use std::fmt::{Debug, Formatter}; -use std::path::{Path, PathBuf}; -use std::{fmt, io}; pub use test::{DbWithTestSystem, DbWithWritableSystem, InMemorySystem, TestSystem}; use walk_directory::WalkDirectoryBuilder; @@ -196,19 +194,6 @@ pub trait System: Debug + Sync + Send { /// yields a single entry for that file. fn walk_directory(&self, path: &SystemPath) -> WalkDirectoryBuilder; - /// Return an iterator that produces all the `Path`s that match the given - /// pattern using default match options, which may be absolute or relative to - /// the current working directory. - /// - /// This may return an error if the pattern is invalid. - fn glob( - &self, - pattern: &str, - ) -> std::result::Result< - Box> + '_>, - PatternError, - >; - /// Fetches the environment variable `key` from the current process. /// /// # Errors @@ -398,62 +383,6 @@ impl DirectoryEntry { } } -/// A glob iteration error. -/// -/// This is typically returned when a particular path cannot be read -/// to determine if its contents match the glob pattern. This is possible -/// if the program lacks the appropriate permissions, for example. -#[derive(Debug)] -pub struct GlobError { - path: PathBuf, - error: GlobErrorKind, -} - -impl GlobError { - /// The Path that the error corresponds to. - pub fn path(&self) -> &Path { - &self.path - } - - pub fn kind(&self) -> &GlobErrorKind { - &self.error - } -} - -impl Error for GlobError {} - -impl fmt::Display for GlobError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match &self.error { - GlobErrorKind::IOError(error) => { - write!( - f, - "attempting to read `{}` resulted in an error: {error}", - self.path.display(), - ) - } - GlobErrorKind::NonUtf8Path => { - write!(f, "`{}` is not a valid UTF-8 path", self.path.display(),) - } - } - } -} - -impl From for GlobError { - fn from(value: glob::GlobError) -> Self { - Self { - path: value.path().to_path_buf(), - error: GlobErrorKind::IOError(value.into_error()), - } - } -} - -#[derive(Debug)] -pub enum GlobErrorKind { - IOError(io::Error), - NonUtf8Path, -} - #[cfg(not(target_arch = "wasm32"))] pub fn file_time_now() -> FileTime { FileTime::now() diff --git a/crates/ruff_db/src/system/memory_fs.rs b/crates/ruff_db/src/system/memory_fs.rs index 8cea9799cd13e..fc3484a378491 100644 --- a/crates/ruff_db/src/system/memory_fs.rs +++ b/crates/ruff_db/src/system/memory_fs.rs @@ -8,13 +8,13 @@ use filetime::FileTime; use rustc_hash::FxHashMap; use crate::system::{ - DirectoryEntry, FileType, GlobError, GlobErrorKind, Metadata, Result, SystemPath, - SystemPathBuf, SystemVirtualPath, SystemVirtualPathBuf, file_time_now, walk_directory, + DirectoryEntry, FileType, Metadata, Result, SystemPath, SystemPathBuf, SystemVirtualPath, + SystemVirtualPathBuf, file_time_now, walk_directory, }; use super::walk_directory::{ - DirectoryWalker, ErrorKind, WalkDirectoryBuilder, WalkDirectoryConfiguration, - WalkDirectoryVisitor, WalkDirectoryVisitorBuilder, WalkState, + DirectoryWalker, WalkDirectoryBuilder, WalkDirectoryConfiguration, WalkDirectoryVisitor, + WalkDirectoryVisitorBuilder, WalkState, }; /// File system that stores all content in memory. @@ -270,46 +270,6 @@ impl MemoryFileSystem { WalkDirectoryBuilder::new(path, MemoryWalker { fs: self.clone() }) } - pub fn glob( - &self, - pattern: &str, - ) -> std::result::Result< - impl Iterator> + '_, - glob::PatternError, - > { - // Very naive implementation that iterates over all files and collects all that match the given pattern. - - let normalized = self.normalize_path(pattern); - let pattern = glob::Pattern::new(normalized.as_str())?; - let matches = std::sync::Mutex::new(Vec::new()); - - self.walk_directory("/").standard_filters(false).run(|| { - Box::new(|entry| { - match entry { - Ok(entry) => { - if pattern.matches_path(entry.path().as_std_path()) { - matches.lock().unwrap().push(Ok(entry.into_path())); - } - } - Err(error) => match error.kind { - ErrorKind::Loop { .. } => { - unreachable!("Loops aren't possible in the memory file system because it doesn't support symlinks.") - } - ErrorKind::Io { err, path } => { - matches.lock().unwrap().push(Err(GlobError { path: path.expect("walk_directory to always set a path").into_std_path_buf(), error: GlobErrorKind::IOError(err)})); - } - ErrorKind::NonUtf8Path { path } => { - matches.lock().unwrap().push(Err(GlobError { path, error: GlobErrorKind::NonUtf8Path})); - } - }, - } - WalkState::Continue - }) - }); - - Ok(matches.into_inner().unwrap().into_iter()) - } - pub fn remove_file(&self, path: impl AsRef) -> Result<()> { fn remove_file(fs: &MemoryFileSystem, path: &SystemPath) -> Result<()> { let mut by_path = fs.inner.by_path.write().unwrap(); @@ -1226,26 +1186,4 @@ mod tests { Ok(()) } - - #[test] - fn glob() -> std::io::Result<()> { - let root = SystemPath::new("/src"); - let fs = MemoryFileSystem::with_current_directory(root); - - fs.write_files_all([ - (root.join("foo.py"), "print('foo')"), - (root.join("a/bar.py"), "print('bar')"), - (root.join("a/.baz.py"), "print('baz')"), - ])?; - - let mut matches = fs.glob("/src/a/**").unwrap().flatten().collect::>(); - matches.sort_unstable(); - - assert_eq!(matches, vec![root.join("a/.baz.py"), root.join("a/bar.py")]); - - let matches = fs.glob("**/bar.py").unwrap().flatten().collect::>(); - assert_eq!(matches, vec![root.join("a/bar.py")]); - - Ok(()) - } } diff --git a/crates/ruff_db/src/system/os.rs b/crates/ruff_db/src/system/os.rs index f39fe7f0dccc1..0ce21a569eeee 100644 --- a/crates/ruff_db/src/system/os.rs +++ b/crates/ruff_db/src/system/os.rs @@ -6,8 +6,8 @@ use super::walk_directory::{ }; use crate::max_parallelism; use crate::system::{ - CaseSensitivity, DirectoryEntry, FileType, GlobError, GlobErrorKind, Metadata, Result, System, - SystemPath, SystemPathBuf, SystemVirtualPath, WhichError, WhichResult, WritableSystem, + CaseSensitivity, DirectoryEntry, FileType, Metadata, Result, System, SystemPath, SystemPathBuf, + SystemVirtualPath, WhichError, WhichResult, WritableSystem, }; use filetime::FileTime; use ruff_notebook::{Notebook, NotebookError}; @@ -202,30 +202,6 @@ impl System for OsSystem { ) } - fn glob( - &self, - pattern: &str, - ) -> std::result::Result< - Box>>, - glob::PatternError, - > { - glob::glob(pattern).map(|inner| { - let iterator = inner.map(|result| { - let path = result?; - - let system_path = SystemPathBuf::from_path_buf(path).map_err(|path| GlobError { - path, - error: GlobErrorKind::NonUtf8Path, - })?; - - Ok(system_path) - }); - - let boxed: Box> = Box::new(iterator); - boxed - }) - } - fn as_writable(&self) -> Option<&dyn WritableSystem> { Some(self) } diff --git a/crates/ruff_db/src/system/test.rs b/crates/ruff_db/src/system/test.rs index d43fa5570703e..18edfdb920967 100644 --- a/crates/ruff_db/src/system/test.rs +++ b/crates/ruff_db/src/system/test.rs @@ -1,4 +1,3 @@ -use glob::PatternError; use ruff_notebook::{Notebook, NotebookError}; use rustc_hash::FxHashMap; use std::panic::RefUnwindSafe; @@ -7,8 +6,8 @@ use std::sync::{Arc, Mutex}; use crate::Db; use crate::files::File; use crate::system::{ - CaseSensitivity, DirectoryEntry, GlobError, MemoryFileSystem, Metadata, Result, System, - SystemPath, SystemPathBuf, SystemVirtualPath, WhichError, WhichResult, + CaseSensitivity, DirectoryEntry, MemoryFileSystem, Metadata, Result, System, SystemPath, + SystemPathBuf, SystemVirtualPath, WhichError, WhichResult, }; use super::WritableSystem; @@ -148,16 +147,6 @@ impl System for TestSystem { self.system().walk_directory(path) } - fn glob( - &self, - pattern: &str, - ) -> std::result::Result< - Box> + '_>, - PatternError, - > { - self.system().glob(pattern) - } - fn as_writable(&self) -> Option<&dyn WritableSystem> { Some(self) } @@ -419,17 +408,6 @@ impl System for InMemorySystem { self.memory_fs.walk_directory(path) } - fn glob( - &self, - pattern: &str, - ) -> std::result::Result< - Box> + '_>, - PatternError, - > { - let iterator = self.memory_fs.glob(pattern)?; - Ok(Box::new(iterator)) - } - fn as_writable(&self) -> Option<&dyn WritableSystem> { Some(self) } diff --git a/crates/ty_server/src/system.rs b/crates/ty_server/src/system.rs index 325c195c9b1f0..0c157ddf2c932 100644 --- a/crates/ty_server/src/system.rs +++ b/crates/ty_server/src/system.rs @@ -13,9 +13,8 @@ use ruff_db::file_revision::FileRevision; use ruff_db::files::{File, FilePath}; use ruff_db::system::walk_directory::WalkDirectoryBuilder; use ruff_db::system::{ - CaseSensitivity, DirectoryEntry, FileType, GlobError, Metadata, PatternError, Result, System, - SystemPath, SystemPathBuf, SystemVirtualPath, SystemVirtualPathBuf, WhichResult, - WritableSystem, + CaseSensitivity, DirectoryEntry, FileType, Metadata, Result, System, SystemPath, SystemPathBuf, + SystemVirtualPath, SystemVirtualPathBuf, WhichResult, WritableSystem, }; use ruff_notebook::{Notebook, NotebookError}; use ruff_python_ast::PySourceType; @@ -256,16 +255,6 @@ impl System for LSPSystem { self.native_system.walk_directory(path) } - fn glob( - &self, - pattern: &str, - ) -> std::result::Result< - Box> + '_>, - PatternError, - > { - self.native_system.glob(pattern) - } - fn as_writable(&self) -> Option<&dyn WritableSystem> { self.native_system.as_writable() } diff --git a/crates/ty_test/src/db.rs b/crates/ty_test/src/db.rs index e98a93648318f..130cac6546392 100644 --- a/crates/ty_test/src/db.rs +++ b/crates/ty_test/src/db.rs @@ -344,17 +344,6 @@ impl System for MdtestSystem { self.as_system().walk_directory(&self.normalize_path(path)) } - fn glob( - &self, - pattern: &str, - ) -> Result< - Box> + '_>, - ruff_db::system::PatternError, - > { - self.as_system() - .glob(self.normalize_path(SystemPath::new(pattern)).as_str()) - } - fn as_writable(&self) -> Option<&dyn WritableSystem> { Some(self) } diff --git a/crates/ty_wasm/src/lib.rs b/crates/ty_wasm/src/lib.rs index 05ce49eb0911f..626348334862c 100644 --- a/crates/ty_wasm/src/lib.rs +++ b/crates/ty_wasm/src/lib.rs @@ -7,8 +7,8 @@ use ruff_db::files::{File, FilePath, FileRange, system_path_to_file, vendored_pa use ruff_db::source::{SourceText, line_index, source_text}; use ruff_db::system::walk_directory::WalkDirectoryBuilder; use ruff_db::system::{ - CaseSensitivity, DirectoryEntry, GlobError, MemoryFileSystem, Metadata, PatternError, System, - SystemPath, SystemPathBuf, SystemVirtualPath, WhichError, WhichResult, WritableSystem, + CaseSensitivity, DirectoryEntry, MemoryFileSystem, Metadata, System, SystemPath, SystemPathBuf, + SystemVirtualPath, WhichError, WhichResult, WritableSystem, }; use ruff_db::vendored::VendoredPath; use ruff_diagnostics::{Applicability, Edit}; @@ -1431,13 +1431,6 @@ impl System for WasmSystem { self.fs.walk_directory(path) } - fn glob( - &self, - pattern: &str, - ) -> Result> + '_>, PatternError> { - Ok(Box::new(self.fs.glob(pattern)?)) - } - fn as_writable(&self) -> Option<&dyn WritableSystem> { None }