Skip to content
Myr

fix: flag path separators in --and patterns, not just the primary pattern

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The path-separator diagnostic (#1873, made unconditional for the primary pattern in #1975) only inspected opts.pattern. --and patterns (opts.exprs) are chained with the primary pattern and matched against the file name in exactly the same way, so a path separator in any of them is the same silent "no results" footgun. Factor the per-pattern check into a helper and apply it to the primary pattern plus every --and pattern, mirroring how ensure_use_hidden_option_for_leading_dot_pattern already iterates all patterns. --full-path still suppresses the diagnostic. Adds a regression test. See #1873.

authored by greymoth-jp · committed by greymoth-jp · parent a8a77d2 · Browse files · 3 files+45 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+45 added−6 removed

  • tests:+25 added−0 removed
  • other:+20 added−6 removed

3 files+45 added−6 removed

modified:src/main.rs+19 added-6 removed
146 unmodified lines
147148/// Detect if the user accidentally supplied a path instead of a search pattern.149///150/// Without `--full-path`, fd matches patterns against file names, so any pattern151/// containing a path separator can never match. Two cases are worth a friendly152/// error rather than silent "no results":151/// containing a path separator can never match. This applies to the primary152/// positional pattern *and* to every `--and` pattern, since all of them are153/// chained together and matched against the file name (see `run`). Two cases per154/// pattern are worth a friendly error rather than silent "no results":155///156/// 1. The pattern contains '/'. '/' is always a path separator (including on157/// Windows) and has no regex meaning, so flagging it is safe and catches the158/// common Linux/macOS mistake of pasting a full path as the pattern.
10 unmodified lines
169 if opts.full_path {170 return Ok(());171 }172173 // Check the primary pattern and every `--and` pattern. They are all matched174 // against the file name (see `run`), so a path separator in any of them is175 // the same silent "no results" footgun.176 for pattern in std::iter::once(&opts.pattern).chain(opts.exprs.iter().flatten()) {177 ensure_single_search_pattern_is_not_a_path(pattern)?;178 }179 Ok(())180}181182/// Apply the path-separator diagnostic to a single pattern. See183/// [`ensure_search_pattern_is_not_a_path`] for the rationale of each case.184fn ensure_single_search_pattern_is_not_a_path(pattern: &str) -> Result<()> {185 // Start with the cheap check: '/' is always a path separator, including on186 // Windows, and has no regex meaning, so flagging it is safe and catches the187 // Linux/macOS mistake of pasting a full path as the pattern.188 #[cfg_attr(not(windows), allow(unused_mut))]175 let mut should_warn = opts.pattern.contains('/');189 let mut should_warn = pattern.contains('/');190191 // On Windows we additionally accept the native `\` separator, but only when192 // the pattern actually resolves to an existing directory - `\` is also the193 // regex escape char there, so valid patterns like `\Ac` or `\d+` must still
1 unmodified line
195 // so short-circuit via `||` to avoid the stat call on the happy path.196 #[cfg(windows)]197 {198 should_warn = should_warn185 || (opts.pattern.contains(std::path::MAIN_SEPARATOR)186 && Path::new(&opts.pattern).is_dir());199 || (pattern.contains(std::path::MAIN_SEPARATOR) && Path::new(pattern).is_dir());200 }201202 if should_warn {203 Err(anyhow!(
2 unmodified lines
206 If you want to search for all files inside the '{pattern}' directory, use a match-all pattern:\n\n \207 fd . '{pattern}'\n\n\208 Instead, if you want your pattern to match the full file path, use:\n\n \209 fd --full-path '{pattern}'",197 pattern = &opts.pattern,210 pattern = pattern,211 ))212 } else {213 Ok(())214 }
340 unmodified lines
modified:tests/tests.rs+25 added
415 unmodified lines
416 one/two/C.Foo2",417 );418}419420/// `--and` patterns are matched against the file name exactly like the primary421/// pattern, so a path separator in any of them is the same silent "no results"422/// footgun and must trigger the same diagnostic. Regression for the sibling of423/// #1873 left unchecked by #1975 (only the positional pattern was validated).424#[test]425fn test_and_pattern_with_forward_slash_is_rejected() {426 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);427428 // Clean primary pattern, but a `--and` pattern carrying a path separator.429 te.assert_failure_with_error(430 &["foo", "--and", "nonexistent/path"],431 "[fd error]: The search pattern 'nonexistent/path' contains a path-separation character and will not lead to any search results.",432 );433434 // `--full-path` is the explicit opt-in and must still suppress the435 // diagnostic for `--and` patterns too (the invocation runs and matches436 // instead of erroring on the path separator).437 #[cfg(not(windows))]438 te.assert_output(439 &["--full-path", "one/two/c", "--and", "two/c"],440 "one/two/c.foo441 one/two/C.Foo2",442 );443}444445/// Explicit root path446#[test]447fn test_explicit_root_path() {448 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
2430 unmodified lines
modified:CHANGELOG.md+1 added
5 unmodified lines
67## Bugfixes8- Handle invalid working directories gracefully when using `--full-path`, see #1900 (@Xavrir).9- Fire the "search pattern contains a path separator" diagnostic for any pattern containing `/`, not just patterns that happen to name an existing directory. Preserves the legacy Windows behaviour that also flags native `\` separators when the pattern resolves to a real directory. See #1873.10- Also fire the "search pattern contains a path separator" diagnostic for `--and` patterns, not only the primary positional pattern. `--and` patterns are matched against the file name just like the primary pattern, so a path separator in them silently returned zero results. See #1873.11- Fix bug where passing "-" as a directory argument didn't actually search that directory, see #849 (@Sean-Kenneth-Doherty).1213# 10.4.214
849 unmodified lines