@@ -4,7 +4,7 @@ use ruff_db::files::{File, system_path_to_file};
4
4
use ruff_db:: system:: walk_directory:: { ErrorKind , WalkDirectoryBuilder , WalkState } ;
5
5
use ruff_db:: system:: { SystemPath , SystemPathBuf } ;
6
6
use ruff_python_ast:: PySourceType ;
7
- use rustc_hash:: { FxBuildHasher , FxHashSet } ;
7
+ use rustc_hash:: FxHashSet ;
8
8
use std:: path:: PathBuf ;
9
9
use thiserror:: Error ;
10
10
@@ -163,20 +163,24 @@ impl<'a> ProjectFilesWalker<'a> {
163
163
164
164
/// Walks the project paths and collects the paths of all files that
165
165
/// are included in the project.
166
- pub ( crate ) fn walk_paths ( self ) -> ( Vec < SystemPathBuf > , Vec < IOErrorDiagnostic > ) {
167
- let paths = std:: sync:: Mutex :: new ( Vec :: new ( ) ) ;
166
+ pub ( crate ) fn collect_vec ( self , db : & dyn Db ) -> ( Vec < File > , Vec < IOErrorDiagnostic > ) {
167
+ let files = std:: sync:: Mutex :: new ( Vec :: new ( ) ) ;
168
168
let diagnostics = std:: sync:: Mutex :: new ( Vec :: new ( ) ) ;
169
169
170
170
self . walker . run ( || {
171
- Box :: new ( |entry| {
171
+ let db = db. dyn_clone ( ) ;
172
+ let filter = & self . filter ;
173
+ let files = & files;
174
+ let diagnostics = & diagnostics;
175
+
176
+ Box :: new ( move |entry| {
172
177
match entry {
173
178
Ok ( entry) => {
174
179
// Skip excluded directories unless they were explicitly passed to the walker
175
180
// (which is the case passed to `ty check <paths>`).
176
181
if entry. file_type ( ) . is_directory ( ) {
177
182
if entry. depth ( ) > 0 {
178
- let directory_included = self
179
- . filter
183
+ let directory_included = filter
180
184
. is_directory_included ( entry. path ( ) , GlobFilterCheckMode :: TopDown ) ;
181
185
return match directory_included {
182
186
IncludeResult :: Included => WalkState :: Continue ,
@@ -210,8 +214,7 @@ impl<'a> ProjectFilesWalker<'a> {
210
214
// For all files, except the ones that were explicitly passed to the walker (CLI),
211
215
// check if they're included in the project.
212
216
if entry. depth ( ) > 0 {
213
- match self
214
- . filter
217
+ match filter
215
218
. is_file_included ( entry. path ( ) , GlobFilterCheckMode :: TopDown )
216
219
{
217
220
IncludeResult :: Included => { } ,
@@ -232,8 +235,11 @@ impl<'a> ProjectFilesWalker<'a> {
232
235
}
233
236
}
234
237
235
- let mut paths = paths. lock ( ) . unwrap ( ) ;
236
- paths. push ( entry. into_path ( ) ) ;
238
+ // If this returns `Err`, then the file was deleted between now and when the walk callback was called.
239
+ // We can ignore this.
240
+ if let Ok ( file) = system_path_to_file ( & * db, entry. path ( ) ) {
241
+ files. lock ( ) . unwrap ( ) . push ( file) ;
242
+ }
237
243
}
238
244
}
239
245
Err ( error) => match error. kind ( ) {
@@ -274,39 +280,14 @@ impl<'a> ProjectFilesWalker<'a> {
274
280
} ) ;
275
281
276
282
(
277
- paths . into_inner ( ) . unwrap ( ) ,
283
+ files . into_inner ( ) . unwrap ( ) ,
278
284
diagnostics. into_inner ( ) . unwrap ( ) ,
279
285
)
280
286
}
281
287
282
- pub ( crate ) fn collect_vec ( self , db : & dyn Db ) -> ( Vec < File > , Vec < IOErrorDiagnostic > ) {
283
- let ( paths, diagnostics) = self . walk_paths ( ) ;
284
-
285
- (
286
- paths
287
- . into_iter ( )
288
- . filter_map ( move |path| {
289
- // If this returns `None`, then the file was deleted between the `walk_directory` call and now.
290
- // We can ignore this.
291
- system_path_to_file ( db, & path) . ok ( )
292
- } )
293
- . collect ( ) ,
294
- diagnostics,
295
- )
296
- }
297
-
298
288
pub ( crate ) fn collect_set ( self , db : & dyn Db ) -> ( FxHashSet < File > , Vec < IOErrorDiagnostic > ) {
299
- let ( paths, diagnostics) = self . walk_paths ( ) ;
300
-
301
- let mut files = FxHashSet :: with_capacity_and_hasher ( paths. len ( ) , FxBuildHasher ) ;
302
-
303
- for path in paths {
304
- if let Ok ( file) = system_path_to_file ( db, & path) {
305
- files. insert ( file) ;
306
- }
307
- }
308
-
309
- ( files, diagnostics)
289
+ let ( files, diagnostics) = self . collect_vec ( db) ;
290
+ ( files. into_iter ( ) . collect ( ) , diagnostics)
310
291
}
311
292
}
312
293
0 commit comments