Skip to content

Commit dacddfe

Browse files
authored
fix(cli): apply default ignore rules in memory instead of shared temp files (#16094)
1 parent 8062864 commit dacddfe

3 files changed

Lines changed: 143 additions & 43 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-cli": patch:sec
3+
"@tauri-apps/cli": patch:sec
4+
---
5+
6+
The CLI no longer writes its default ignore rules to shared files in the system temporary directory (`.gitignore` and `.tauri/.gitignore`) and reads them back, which let other local users change which files the project lookup and the dev watcher skip. The rules are now applied in memory. `TAURI_CLI_WATCHER_IGNORE_FILENAME` is now consistently treated as an ignore file name looked up in each directory, as documented.

‎crates/tauri-cli/src/helpers/app_paths.rs‎

Lines changed: 92 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
// SPDX-License-Identifier: MIT
44

55
use 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

1514
use tauri_utils::{
1615
config::parse::{ConfigFormat, folder_has_configuration_file, is_configuration_file},
@@ -32,23 +31,31 @@ static FRONTEND_DIR: OnceLock<PathBuf> = OnceLock::new();
3231
static TAURI_DIR: OnceLock<PathBuf> = OnceLock::new();
3332

3433
pub 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+
5259
fn 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+
}

‎crates/tauri-cli/src/interface/rust.rs‎

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
collections::HashMap,
77
ffi::OsStr,
88
fs::FileType,
9-
io::{BufRead, Write},
9+
io::BufRead,
1010
iter::once,
1111
path::{Path, PathBuf},
1212
process::Command,
@@ -344,32 +344,32 @@ impl IgnoreMatcher {
344344
fn build_ignore_matcher(dir: &Path) -> IgnoreMatcher {
345345
let mut matchers = Vec::new();
346346

347+
let custom_ignore_file = std::env::var_os("TAURI_CLI_WATCHER_IGNORE_FILENAME");
348+
let mut overrides = ignore::overrides::OverrideBuilder::new(dir);
349+
overrides.add(".taurignore").unwrap();
350+
if let Some(ignore_file) = &custom_ignore_file {
351+
let _ = overrides.add(&ignore_file.to_string_lossy());
352+
}
353+
347354
// ignore crate doesn't expose an API to build `ignore::gitignore::GitIgnore`
348355
// with custom ignore file names so we have to walk the directory and collect
349356
// our custom ignore files and add it using `ignore::gitignore::GitIgnoreBuilder::add`
350357
for entry in ignore::WalkBuilder::new(dir)
351358
.require_git(false)
352359
.ignore(false)
353-
.overrides(
354-
ignore::overrides::OverrideBuilder::new(dir)
355-
.add(".taurignore")
356-
.unwrap()
357-
.build()
358-
.unwrap(),
359-
)
360+
.overrides(overrides.build().unwrap())
360361
.build()
361362
.flatten()
362363
{
363364
let path = entry.path();
364-
if path.file_name() == Some(OsStr::new(".taurignore")) {
365+
let file_name = path.file_name();
366+
if file_name == Some(OsStr::new(".taurignore"))
367+
|| (file_name.is_some() && file_name == custom_ignore_file.as_deref())
368+
{
365369
let mut ignore_builder = GitignoreBuilder::new(path.parent().unwrap());
366370

367371
ignore_builder.add(path);
368372

369-
if let Some(ignore_file) = std::env::var_os("TAURI_CLI_WATCHER_IGNORE_FILENAME") {
370-
ignore_builder.add(dir.join(ignore_file));
371-
}
372-
373373
for line in crate::dev::TAURI_CLI_BUILTIN_WATCHER_IGNORE_FILE
374374
.lines()
375375
.map_while(Result::ok)
@@ -385,22 +385,16 @@ fn build_ignore_matcher(dir: &Path) -> IgnoreMatcher {
385385
}
386386

387387
fn lookup<F: FnMut(FileType, PathBuf)>(dir: &Path, mut f: F) {
388-
let mut default_gitignore = std::env::temp_dir();
389-
default_gitignore.push(".tauri");
390-
let _ = std::fs::create_dir_all(&default_gitignore);
391-
default_gitignore.push(".gitignore");
392-
if !default_gitignore.exists() {
393-
if let Ok(mut file) = std::fs::File::create(default_gitignore.clone()) {
394-
let _ = file.write_all(crate::dev::TAURI_CLI_BUILTIN_WATCHER_IGNORE_FILE);
395-
}
396-
}
397-
398388
let mut builder = ignore::WalkBuilder::new(dir);
399389
builder.add_custom_ignore_filename(".taurignore");
400-
let _ = builder.add_ignore(default_gitignore);
401390
if let Some(ignore_file) = std::env::var_os("TAURI_CLI_WATCHER_IGNORE_FILENAME") {
402-
builder.add_ignore(ignore_file);
391+
builder.add_custom_ignore_filename(ignore_file);
403392
}
393+
crate::helpers::app_paths::skip_ignored_entries(
394+
&mut builder,
395+
dir,
396+
crate::dev::TAURI_CLI_BUILTIN_WATCHER_IGNORE_FILE,
397+
);
404398
builder.require_git(false).ignore(false).max_depth(Some(1));
405399

406400
for entry in builder.build().flatten() {
@@ -1769,6 +1763,32 @@ mod tests {
17691763
)
17701764
}
17711765

1766+
#[test]
1767+
fn lookup_skips_builtin_ignored_entries() {
1768+
let temp_dir = tempfile::tempdir().unwrap();
1769+
let dir = temp_dir.path();
1770+
for path in ["node_modules", "target", "gen", "src"] {
1771+
fs::create_dir_all(dir.join(path)).unwrap();
1772+
}
1773+
fs::write(dir.join("Cargo.lock"), "").unwrap();
1774+
fs::write(dir.join("Cargo.toml"), "").unwrap();
1775+
1776+
let mut paths = Vec::new();
1777+
lookup(dir, |_, path| {
1778+
paths.push(path.strip_prefix(dir).unwrap().to_path_buf())
1779+
});
1780+
paths.sort();
1781+
1782+
assert_eq!(
1783+
paths,
1784+
vec![
1785+
PathBuf::from(""),
1786+
PathBuf::from("Cargo.toml"),
1787+
PathBuf::from("src")
1788+
]
1789+
);
1790+
}
1791+
17721792
#[test]
17731793
fn parse_cargo_option() {
17741794
let args = [

0 commit comments

Comments
 (0)