11use std:: {
22 fs:: File ,
33 path:: { Path , PathBuf } ,
4+ sync:: { Arc , Mutex } ,
45} ;
56
67use error_stack:: { Result , ResultExt } ;
78use fast_glob:: glob_match;
8- use ignore:: WalkBuilder ;
9+ use ignore:: { WalkBuilder , WalkParallel , WalkState } ;
910use rayon:: iter:: { IntoParallelIterator , ParallelIterator } ;
1011use tracing:: { instrument, warn} ;
1112
@@ -53,12 +54,29 @@ impl<'a> ProjectBuilder<'a> {
5354 let mut entry_types = Vec :: with_capacity ( INITIAL_VECTOR_CAPACITY ) ;
5455 let mut builder = WalkBuilder :: new ( & self . base_path ) ;
5556 builder. hidden ( false ) ;
56- let walkdir = builder. build ( ) ;
57+ let walk_parallel : WalkParallel = builder. build_parallel ( ) ;
5758
58- for entry in walkdir {
59- let entry = entry. change_context ( Error :: Io ) ?;
59+ let collected = Arc :: new ( Mutex :: new ( Vec :: with_capacity ( INITIAL_VECTOR_CAPACITY ) ) ) ;
60+ let collected_for_threads = Arc :: clone ( & collected) ;
61+
62+ walk_parallel. run ( move || {
63+ let collected = Arc :: clone ( & collected_for_threads) ;
64+ Box :: new ( move |res| {
65+ if let Ok ( entry) = res {
66+ if let Ok ( mut v) = collected. lock ( ) {
67+ v. push ( entry) ;
68+ }
69+ }
70+ WalkState :: Continue
71+ } )
72+ } ) ;
73+
74+ // Process sequentially with &mut self
75+ let collected_entries = Arc :: try_unwrap ( collected) . unwrap ( ) . into_inner ( ) . unwrap ( ) ;
76+ for entry in collected_entries {
6077 entry_types. push ( self . build_entry_type ( entry) ?) ;
6178 }
79+
6280 self . build_project_from_entry_types ( entry_types)
6381 }
6482
@@ -216,7 +234,10 @@ impl<'a> ProjectBuilder<'a> {
216234}
217235
218236fn matches_globs ( path : & Path , globs : & [ String ] ) -> bool {
219- globs. iter ( ) . any ( |glob| glob_match ( glob, path. to_str ( ) . unwrap ( ) ) )
237+ match path. to_str ( ) {
238+ Some ( s) => globs. iter ( ) . any ( |glob| glob_match ( glob, s) ) ,
239+ None => false ,
240+ }
220241}
221242
222243fn ruby_package_owner ( path : & Path ) -> Result < Option < String > , Error > {
@@ -241,14 +262,11 @@ mod tests {
241262
242263 #[ test]
243264 fn test_matches_globs ( ) {
244- // should fail because hidden directories are ignored by glob patterns unless explicitly included
245265 assert ! ( matches_globs( Path :: new( "script/.eslintrc.js" ) , & [ OWNED_GLOB . to_string( ) ] ) ) ;
246266 }
247267
248268 #[ test]
249269 fn test_glob_match ( ) {
250- // Exposes bug in glob-match https://github.com/devongovett/glob-match/issues/9
251- // should fail because hidden directories are ignored by glob patterns unless explicitly included
252270 assert ! ( glob_match( OWNED_GLOB , "script/.eslintrc.js" ) ) ;
253271 }
254272}
0 commit comments