33// SPDX-License-Identifier: MIT
44
55use std:: {
6- cmp:: Ordering ,
76 env:: current_dir,
87 ffi:: OsStr ,
98 path:: { Path , PathBuf } ,
109 sync:: OnceLock ,
1110} ;
1211
13- use ignore:: WalkBuilder ;
12+ use ignore:: { WalkBuilder , gitignore :: GitignoreBuilder } ;
1413
1514use tauri_utils:: {
1615 config:: parse:: { ConfigFormat , folder_has_configuration_file, is_configuration_file} ,
@@ -32,23 +31,31 @@ static FRONTEND_DIR: OnceLock<PathBuf> = OnceLock::new();
3231static TAURI_DIR : OnceLock < PathBuf > = OnceLock :: new ( ) ;
3332
3433pub fn walk_builder ( path : & Path ) -> WalkBuilder {
35- let mut default_gitignore = std:: env:: temp_dir ( ) ;
36- default_gitignore. push ( ".gitignore" ) ;
37- if !default_gitignore. exists ( ) {
38- if let Ok ( mut file) = std:: fs:: File :: create ( default_gitignore. clone ( ) ) {
39- use std:: io:: Write ;
40- let _ = file. write_all ( TAURI_GITIGNORE ) ;
41- }
42- }
43-
4434 let mut builder = WalkBuilder :: new ( path) ;
4535 builder. add_custom_ignore_filename ( ".taurignore" ) ;
4636 builder. git_global ( false ) ;
4737 builder. parents ( false ) ;
48- let _ = builder . add_ignore ( default_gitignore ) ;
38+ skip_ignored_entries ( & mut builder , path , TAURI_GITIGNORE ) ;
4939 builder
5040}
5141
42+ /// Skips the walked entries matching the given `.gitignore`-style rules.
43+ ///
44+ /// The rules are matched in memory instead of being written to a temporary ignore file,
45+ /// which could be tampered with by other users.
46+ pub fn skip_ignored_entries ( builder : & mut WalkBuilder , root : & Path , rules : & [ u8 ] ) {
47+ let mut gitignore = GitignoreBuilder :: new ( root) ;
48+ for line in String :: from_utf8_lossy ( rules) . lines ( ) {
49+ let _ = gitignore. add_line ( None , line) ;
50+ }
51+ if let Ok ( gitignore) = gitignore. build ( ) {
52+ builder. filter_entry ( move |entry| {
53+ let is_dir = entry. file_type ( ) . is_some_and ( |t| t. is_dir ( ) ) ;
54+ !gitignore. matched ( entry. path ( ) , is_dir) . is_ignore ( )
55+ } ) ;
56+ }
57+ }
58+
5259fn lookup < F : Fn ( & PathBuf ) -> bool > ( dir : & Path , checker : F ) -> Option < PathBuf > {
5360 let mut builder = walk_builder ( dir) ;
5461 builder
@@ -62,12 +69,12 @@ fn lookup<F: Fn(&PathBuf) -> bool>(dir: &Path, checker: F) -> Option<PathBuf> {
6269 } )
6370 . unwrap_or ( 3 ) ,
6471 ) )
65- . sort_by_file_path ( |a , _| {
66- if a . extension ( ) . is_some ( ) {
67- Ordering :: Less
68- } else {
69- Ordering :: Greater
70- }
72+ // entries with an extension (files) first
73+ . sort_by_file_path ( |a , b| {
74+ a . extension ( )
75+ . is_none ( )
76+ . cmp ( & b . extension ( ) . is_none ( ) )
77+ . then_with ( || a . cmp ( b ) )
7178 } ) ;
7279
7380 for entry in builder. build ( ) . flatten ( ) {
@@ -165,3 +172,70 @@ pub fn resolve_frontend_dir() -> Option<PathBuf> {
165172 } )
166173 . map ( |p| p. parent ( ) . unwrap ( ) . to_path_buf ( ) )
167174}
175+
176+ #[ cfg( test) ]
177+ mod tests {
178+ use std:: { cell:: RefCell , fs, path:: PathBuf } ;
179+
180+ #[ test]
181+ fn walk_builder_skips_default_ignored_entries ( ) {
182+ let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
183+ let root = dir. path ( ) ;
184+ for path in [
185+ "node_modules/pkg/package.json" ,
186+ "target/debug/app" ,
187+ "src/main.rs" ,
188+ ] {
189+ let path = root. join ( path) ;
190+ fs:: create_dir_all ( path. parent ( ) . unwrap ( ) ) . unwrap ( ) ;
191+ fs:: write ( path, "" ) . unwrap ( ) ;
192+ }
193+
194+ let paths = super :: walk_builder ( root)
195+ . require_git ( false )
196+ . build ( )
197+ . flatten ( )
198+ . map ( |entry| entry. path ( ) . strip_prefix ( root) . unwrap ( ) . to_path_buf ( ) )
199+ . collect :: < Vec < _ > > ( ) ;
200+
201+ assert ! ( paths. contains( & PathBuf :: from( "src/main.rs" ) ) , "{paths:?}" ) ;
202+ assert ! (
203+ !paths
204+ . iter( )
205+ . any( |p| p. starts_with( "node_modules" ) || p. starts_with( "target" ) ) ,
206+ "{paths:?}"
207+ ) ;
208+ }
209+
210+ #[ test]
211+ fn lookup_visits_files_first ( ) {
212+ let dir = tempfile:: tempdir ( ) . unwrap ( ) ;
213+ let root = dir. path ( ) ;
214+ fs:: create_dir_all ( root. join ( "a/b" ) ) . unwrap ( ) ;
215+ fs:: write ( root. join ( "a/b/tauri.conf.json" ) , "" ) . unwrap ( ) ;
216+ fs:: write ( root. join ( "z.json" ) , "" ) . unwrap ( ) ;
217+ fs:: write ( root. join ( "c.json" ) , "" ) . unwrap ( ) ;
218+
219+ let visited = RefCell :: new ( Vec :: new ( ) ) ;
220+ super :: lookup ( root, |path| {
221+ visited
222+ . borrow_mut ( )
223+ . push ( path. strip_prefix ( root) . unwrap ( ) . to_path_buf ( ) ) ;
224+ false
225+ } ) ;
226+ let visited = visited. into_inner ( ) ;
227+
228+ // the walk root comes first, then the files of each directory before its subdirectories
229+ assert_eq ! (
230+ visited,
231+ vec![
232+ PathBuf :: from( "" ) ,
233+ PathBuf :: from( "c.json" ) ,
234+ PathBuf :: from( "z.json" ) ,
235+ PathBuf :: from( "a" ) ,
236+ PathBuf :: from( "a/b" ) ,
237+ PathBuf :: from( "a/b/tauri.conf.json" ) ,
238+ ]
239+ ) ;
240+ }
241+ }
0 commit comments