-
Notifications
You must be signed in to change notification settings - Fork 14
Support glob patterns in the exclude config
#901
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ use crate::{ | |
| job_queue::{Job, JobQueue}, | ||
| }; | ||
| use crossbeam_channel::{Sender, unbounded}; | ||
| use glob::Pattern; | ||
| use std::{ | ||
| collections::HashSet, | ||
| fs, | ||
|
|
@@ -16,7 +17,7 @@ pub struct FileDiscoveryJob { | |
| queue: Arc<JobQueue>, | ||
| paths_tx: Sender<PathBuf>, | ||
| errors_tx: Sender<Errors>, | ||
| excluded_paths: Arc<HashSet<PathBuf>>, | ||
| excluded_patterns: Arc<Vec<Pattern>>, | ||
| } | ||
|
|
||
| impl FileDiscoveryJob { | ||
|
|
@@ -26,14 +27,14 @@ impl FileDiscoveryJob { | |
| queue: Arc<JobQueue>, | ||
| paths_tx: Sender<PathBuf>, | ||
| errors_tx: Sender<Errors>, | ||
| excluded_paths: Arc<HashSet<PathBuf>>, | ||
| excluded_patterns: Arc<Vec<Pattern>>, | ||
| ) -> Self { | ||
| Self { | ||
| path, | ||
| queue, | ||
| paths_tx, | ||
| errors_tx, | ||
| excluded_paths, | ||
| excluded_patterns, | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -43,6 +44,10 @@ fn is_indexable_file(path: &Path) -> bool { | |
| .is_some_and(|ext| ext == "rb" || ext == "rake" || ext == "rbs" || ext == "ru") | ||
| } | ||
|
|
||
| fn is_excluded(excluded_patterns: &[Pattern], path: &Path) -> bool { | ||
| excluded_patterns.iter().any(|pattern| pattern.matches_path(path)) | ||
| } | ||
|
|
||
| impl FileDiscoveryJob { | ||
| fn handle_file(&self, path: &Path) { | ||
| if is_indexable_file(path) { | ||
|
|
@@ -62,7 +67,7 @@ impl FileDiscoveryJob { | |
| return; | ||
| }; | ||
|
|
||
| if self.excluded_paths.contains(&canonicalized) { | ||
| if is_excluded(&self.excluded_patterns, &canonicalized) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -71,7 +76,7 @@ impl FileDiscoveryJob { | |
| Arc::clone(&self.queue), | ||
| self.paths_tx.clone(), | ||
| self.errors_tx.clone(), | ||
| Arc::clone(&self.excluded_paths), | ||
| Arc::clone(&self.excluded_patterns), | ||
| ))); | ||
| } | ||
|
|
||
|
|
@@ -107,7 +112,7 @@ impl Job for FileDiscoveryJob { | |
| let kind = entry.file_type().unwrap(); | ||
|
|
||
| if kind.is_dir() { | ||
| if self.excluded_paths.contains(&entry.path()) { | ||
| if is_excluded(&self.excluded_patterns, &entry.path()) { | ||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -116,7 +121,7 @@ impl Job for FileDiscoveryJob { | |
| Arc::clone(&self.queue), | ||
| self.paths_tx.clone(), | ||
| self.errors_tx.clone(), | ||
| Arc::clone(&self.excluded_paths), | ||
| Arc::clone(&self.excluded_patterns), | ||
| ))); | ||
| } else if kind.is_file() { | ||
| self.handle_file(&entry.path()); | ||
|
|
@@ -154,14 +159,29 @@ impl Job for FileDiscoveryJob { | |
| #[must_use] | ||
| pub fn collect_file_paths<S: BuildHasher>( | ||
| paths: Vec<String>, | ||
| excluded: &HashSet<PathBuf, S>, | ||
| excluded: &HashSet<Box<str>, S>, | ||
| ) -> (Vec<PathBuf>, Vec<Errors>) { | ||
| let queue = Arc::new(JobQueue::new()); | ||
| let (files_tx, files_rx) = unbounded(); | ||
| let (errors_tx, errors_rx) = unbounded(); | ||
|
|
||
| // Canonicalize the excluded paths since they may be symlinks | ||
| let excluded: Arc<HashSet<PathBuf>> = Arc::new(excluded.iter().filter_map(|p| fs::canonicalize(p).ok()).collect()); | ||
| // Canonicalize concrete excluded paths (they may be symlinks) and escape them so they match exactly, not | ||
| // as globs. Globs are kept as written. | ||
| let excluded_patterns: Arc<Vec<Pattern>> = Arc::new( | ||
| excluded | ||
| .iter() | ||
| .filter_map(|entry| { | ||
| let entry: &str = entry; | ||
|
|
||
| if entry.contains(['*', '?', '[']) { | ||
| Pattern::new(entry).ok() | ||
| } else { | ||
| let canonical = fs::canonicalize(entry).ok()?; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you find that treating all entries as glob patterns was too slow? Do we need the two separate paths? Also, I think there's potential for this to be slightly confusing. Consider this example: Trying to exclude the same directory using a pattern vs a path results in different behaviour: I would vote for us to do the following:
|
||
| Pattern::new(&Pattern::escape(&canonical.to_string_lossy())).ok() | ||
| } | ||
| }) | ||
| .collect(), | ||
| ); | ||
|
|
||
| for path in paths { | ||
| let Ok(canonicalized) = fs::canonicalize(&path) else { | ||
|
|
@@ -172,7 +192,7 @@ pub fn collect_file_paths<S: BuildHasher>( | |
| continue; | ||
| }; | ||
|
|
||
| if excluded.contains(&canonicalized) { | ||
| if is_excluded(&excluded_patterns, &canonicalized) { | ||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -181,7 +201,7 @@ pub fn collect_file_paths<S: BuildHasher>( | |
| Arc::clone(&queue), | ||
| files_tx.clone(), | ||
| errors_tx.clone(), | ||
| Arc::clone(&excluded), | ||
| Arc::clone(&excluded_patterns), | ||
| ))); | ||
| } | ||
|
|
||
|
|
@@ -205,7 +225,7 @@ mod tests { | |
| fn collect_document_paths_with_exclusions( | ||
| context: &Context, | ||
| paths: &[&str], | ||
| excluded: &HashSet<PathBuf>, | ||
| excluded: &HashSet<Box<str>>, | ||
| ) -> (Vec<String>, Vec<Errors>) { | ||
| let (files, errors) = collect_file_paths( | ||
| paths | ||
|
|
@@ -333,7 +353,7 @@ mod tests { | |
| context.touch(&excluded_file); | ||
|
|
||
| let mut excluded = HashSet::new(); | ||
| excluded.insert(context.absolute_path_to("excluded")); | ||
| excluded.insert(context.absolute_path_to("excluded").to_string_lossy().into()); | ||
|
|
||
| let (files, errors) = collect_document_paths_with_exclusions(&context, &["included", "excluded"], &excluded); | ||
|
|
||
|
|
@@ -350,7 +370,7 @@ mod tests { | |
| context.touch(&nested); | ||
|
|
||
| let mut excluded = HashSet::new(); | ||
| excluded.insert(context.absolute_path_to("root/skip")); | ||
| excluded.insert(context.absolute_path_to("root/skip").to_string_lossy().into()); | ||
|
|
||
| let (files, errors) = collect_document_paths_with_exclusions(&context, &["root"], &excluded); | ||
|
|
||
|
|
@@ -372,11 +392,28 @@ mod tests { | |
|
|
||
| // Excluding the real directory while requesting to index the symlink should properly exclude the link | ||
| let mut excluded = HashSet::new(); | ||
| excluded.insert(context.absolute_path_to("real_dir")); | ||
| excluded.insert(context.absolute_path_to("real_dir").to_string_lossy().into()); | ||
|
|
||
| let (files, errors) = collect_document_paths_with_exclusions(&context, &["included", "link"], &excluded); | ||
|
|
||
| assert!(errors.is_empty()); | ||
| assert_eq!(files, [included.to_str().unwrap().to_string()]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn collect_files_excludes_glob_patterns() { | ||
| let context = Context::new(); | ||
| let kept = PathBuf::from("lib").join("foo.rb"); | ||
| let nested = PathBuf::from("lib").join("test/fixtures").join("bar.rb"); | ||
| context.touch(&kept); | ||
| context.touch(&nested); | ||
|
|
||
| let mut excluded = HashSet::new(); | ||
| excluded.insert("**/fixtures".into()); | ||
|
|
||
| let (files, errors) = collect_document_paths_with_exclusions(&context, &["lib"], &excluded); | ||
|
|
||
| assert!(errors.is_empty()); | ||
| assert_eq!(files, [kept.to_str().unwrap().to_string()]); | ||
| } | ||
| } | ||
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.
Why are we substituting the separator here? Note that this PR changes
excluded_pathsintoexcluded_glob_patterns. For file paths, backslashes are valid (on Windows), but not for glob patterns (which always use forward slashes).