ignore: add multi-root parent matcher regression tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Parent ignore matchers are cached by directory across search roots, but each root must keep its own absolute_base for path rewriting when applying parent .gitignore / .rgignore rules. Document that invariant and lock it in with unit and integration tests for the order-dependent multi-root bugs in #3376, #3419, and #3320. Closes #3320, Closes #3376, Ref #3419, Closes #3451
authored by Nipan Das · committed by Andrew Gallant · parent 068a63e · Browse files · 2 files·+190 added−2 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+190 added−2 removed
- tests:+62 added−0 removed
- other:+128 added−2 removed
Provenance
- Commit
653d7f5- Tree
f686ff3- Author
Nipan Das <n.das@castsoftware.com>- Committer
Andrew Gallant <jamslam@gmail.com>- Parent
068a63e
2 files changed+190 added−2 removed
modified:crates/ignore/src/dir.rs+128 added-2 removed
94 unmodified lines95 inner: Arc<IgnoreInner>,96 // Parent matchers are cached independently of the path being walked, but97 // matching them still needs the canonicalized path originally passed to98 // `add_parents`. For example, when walking `/tmp/project/src`, parent99- // matchers use `/tmp/project/src` to rewrite `foo.py` before matching it100- // against ignore files from `/tmp/project` and its ancestors.99+ // matchers use `/tmp/project/src` to rewrite `/tmp/project/src/foo.py`100+ // before matching it against ignore files from `/tmp/project` and its101+ // ancestors.102+ //103+ // For ripgrep itself, this means that `rg pat src tests` must rewrite104+ // `src/foo` relative to `.../src`, and not whatever root was prepared105+ // first.106+ //107+ // See: https://github.com/BurntSushi/ripgrep/pull/3420108+ // See: https://github.com/BurntSushi/ripgrep/issues/3376109+ // See: https://github.com/BurntSushi/ripgrep/issues/3419110+ // See: https://github.com/BurntSushi/ripgrep/issues/3320111 absolute_base: Option<Arc<PathBuf>>,112 }113 114 #[derive(Clone, Debug)]1224 unmodified lines1339 assert!(src.matched("build", true).is_none());1340 assert!(tests.matched("build", true).is_ignore());1341 }1342 1343+ /// Parent matchers are shared across search roots, but path rewriting for1344+ /// absolute parents must use each root's own base path. Otherwise a rule1345+ /// like `src/invalid` is matched against the wrong absolute path when1346+ /// `src` is searched before a sibling root (e.g. `tests`).1347+ ///1348+ /// Paths passed to `matched` use the same relative layout as `Walk` when1349+ /// roots are given as relative directory names.1350+ ///1351+ /// Regression for: https://github.com/BurntSushi/ripgrep/issues/33761352+ /// and https://github.com/BurntSushi/ripgrep/issues/34191353 #[test]1354+ fn multi_root_gitignore_order_independent() {1355+ let td = tmpdir();1356+ let cwd = std::env::current_dir().unwrap();1357+ // Use paths relative to CWD like the CLI walk does for `rg pat src tests`.1358+ let root = td.path().strip_prefix(&cwd).unwrap_or(td.path());1359+ let src_root = root.join("src");1360+ let tests_root = root.join("tests");1361+1362+ mkdirp(td.path().join(".git"));1363+ mkdirp(td.path().join("src"));1364+ mkdirp(td.path().join("tests"));1365+ wfile(td.path().join(".gitignore"), "src/invalid\n");1366+ wfile(td.path().join("src/invalid"), "x");1367+ wfile(td.path().join("src/valid"), "x");1368+ wfile(td.path().join("tests/valid"), "x");1369+1370+ let ig0 = IgnoreBuilder::new().build();1371+1372+ // Historically buggy order: search `src` first, then `tests`.1373+ let (src_parents, err) = ig0.add_parents(&src_root);1374+ assert!(err.is_none());1375+ let (src, err) = src_parents.add_child(&src_root);1376+ assert!(err.is_none());1377+ let (tests_parents, err) = ig0.add_parents(&tests_root);1378+ assert!(err.is_none());1379+ let (tests, err) = tests_parents.add_child(&tests_root);1380+ assert!(err.is_none());1381+1382+ assert!(Arc::ptr_eq(&src_parents.inner, &tests_parents.inner));1383+ // Each root must carry its own absolute_base even though inners are shared.1384+ assert_ne!(1385+ src.absolute_base.as_ref().unwrap().as_path(),1386+ tests.absolute_base.as_ref().unwrap().as_path()1387+ );1388+ assert!(1389+ src.matched(src_root.join("invalid"), false).is_ignore(),1390+ "parent .gitignore must apply for the src root even when \1391+ another root was prepared in the same process"1392+ );1393+ assert!(src.matched(src_root.join("valid"), false).is_none());1394+ assert!(tests.matched(tests_root.join("valid"), false).is_none());1395+1396+ // Reverse order should behave the same way.1397+ let ig0 = IgnoreBuilder::new().build();1398+ let (tests_parents, err) = ig0.add_parents(&tests_root);1399+ assert!(err.is_none());1400+ let (tests, err) = tests_parents.add_child(&tests_root);1401+ assert!(err.is_none());1402+ let (src_parents, err) = ig0.add_parents(&src_root);1403+ assert!(err.is_none());1404+ let (src, err) = src_parents.add_child(&src_root);1405+ assert!(err.is_none());1406+1407+ assert!(src.matched(src_root.join("invalid"), false).is_ignore());1408+ assert!(src.matched(src_root.join("valid"), false).is_none());1409+ assert!(tests.matched(tests_root.join("valid"), false).is_none());1410+ }1411+1412+ /// Same multi-root / order issue for non-git ignore files (e.g. `.rgignore`1413+ /// via custom ignore names).1414+ ///1415+ /// Regression for: https://github.com/BurntSushi/ripgrep/issues/33201416+ #[test]1417+ fn multi_root_custom_ignore_order_independent() {1418+ let td = tmpdir();1419+ let cwd = std::env::current_dir().unwrap();1420+ let root = td.path().strip_prefix(&cwd).unwrap_or(td.path());1421+ let alpha_root = root.join("alpha");1422+ let beta_root = root.join("beta");1423+1424+ mkdirp(td.path().join("alpha"));1425+ mkdirp(td.path().join("beta"));1426+ wfile(td.path().join(".rgignore"), "beta/**/*.svg\n");1427+ wfile(td.path().join("alpha/a.txt"), "x");1428+ wfile(td.path().join("beta/x.svg"), "x");1429+1430+ let ig0 = IgnoreBuilder::new()1431+ .add_custom_ignore_filename(".rgignore")1432+ .ignore(false)1433+ .git_ignore(false)1434+ .git_global(false)1435+ .git_exclude(false)1436+ .build();1437+1438+ let (alpha_parents, err) = ig0.add_parents(&alpha_root);1439+ assert!(err.is_none());1440+ let (alpha, err) = alpha_parents.add_child(&alpha_root);1441+ assert!(err.is_none());1442+ let (beta_parents, err) = ig0.add_parents(&beta_root);1443+ assert!(err.is_none());1444+ let (beta, err) = beta_parents.add_child(&beta_root);1445+ assert!(err.is_none());1446+1447+ assert_ne!(1448+ alpha.absolute_base.as_ref().unwrap().as_path(),1449+ beta.absolute_base.as_ref().unwrap().as_path()1450+ );1451+ assert!(alpha.matched(alpha_root.join("a.txt"), false).is_none());1452+ assert!(1453+ beta.matched(beta_root.join("x.svg"), false).is_ignore(),1454+ "parent .rgignore must apply for the beta root regardless of \1455+ which root was set up first"1456+ );1457+ }1458+1459+ #[test]1460 fn git_info_exclude_in_linked_worktree() {1461 let td = tmpdir();1462 let git_dir = td.path().join(".git");1463 mkdirp(git_dir.join("info"));40 unmodified linesmodified:tests/misc.rs+62 added
740 unmodified lines741 ";742 eqnice!(expected, cmd.stdout());743 });744 745+// Multiple explicit search roots must apply parent .gitignore rules using each746+// root's own path base. Searching `src` before `tests` used to leak the wrong747+// absolute base into cached parent matchers so `src/invalid` was not ignored.748+// See: https://github.com/BurntSushi/ripgrep/issues/3376749+rgtest!(ignore_git_multi_root_order, |dir: Dir, mut cmd: TestCommand| {750+ dir.create_dir(".git");751+ dir.create(".gitignore", "src/invalid\n");752+ dir.create_dir("src");753+ dir.create_dir("tests");754+ dir.create("src/invalid", "this\n");755+ dir.create("src/valid", "this\n");756+ dir.create("tests/valid", "this\n");757+758+ cmd.args(&["--files-with-matches", "this", "src", "tests"]);759+ let got = cmd.stdout();760+ assert!(761+ !got.contains("invalid"),762+ "src/invalid must stay ignored with multiple roots, got:\n{}",763+ got764+ );765+ assert!(got.contains("src/valid"), "missing src/valid in:\n{}", got);766+ assert!(got.contains("tests/valid"), "missing tests/valid in:\n{}", got);767+768+ // Reverse CLI order should behave the same.769+ let mut cmd = dir.command();770+ cmd.args(&["--files-with-matches", "this", "tests", "src"]);771+ let got = cmd.stdout();772+ assert!(773+ !got.contains("invalid"),774+ "src/invalid must stay ignored with roots reversed, got:\n{}",775+ got776+ );777+});778+779+// Same multi-root path-base issue for `.rgignore`.780+// See: https://github.com/BurntSushi/ripgrep/issues/3320781+rgtest!(ignore_rgignore_multi_root_order, |dir: Dir, mut cmd: TestCommand| {782+ dir.create(".rgignore", "beta/**/*.svg\n");783+ dir.create_dir("alpha");784+ dir.create_dir("beta");785+ dir.create("alpha/a.txt", "AWS\n");786+ dir.create("beta/x.svg", "AWS\n");787+788+ cmd.args(&["--files-with-matches", "AWS", "alpha", "beta"]);789+ let got = cmd.stdout();790+ assert!(791+ !got.contains("x.svg"),792+ "beta/x.svg must be ignored with multiple roots, got:\n{}",793+ got794+ );795+ assert!(got.contains("alpha/a.txt"), "missing alpha/a.txt in:\n{}", got);796+797+ let mut cmd = dir.command();798+ cmd.args(&["--files-with-matches", "AWS", "beta", "alpha"]);799+ let got = cmd.stdout();800+ assert!(801+ !got.contains("x.svg"),802+ "beta/x.svg must be ignored with roots reversed, got:\n{}",803+ got804+ );805+});806+807 rgtest!(symlink_nofollow, |dir: Dir, mut cmd: TestCommand| {808 dir.create_dir("foo");809 dir.create_dir("foo/bar");810 dir.link_dir("foo/baz", "foo/bar/baz");388 unmodified lines