Merge pull request #2036 from greymoth-jp/fix/and-pattern-path-separator
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Check --and patterns for path separators too (match #1975)
authored by Thayne McCombs · committed by GitHub · parents 126be98, 6aac84e · 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
Provenance
- Commit
61c5399- Tree
1ebaf7b- Author
Thayne McCombs <astrothayne@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
126be98- Parent 2
6aac84e
3 files changed+45 added−6 removed
modified:src/main.rs+19 added-6 removed
146 unmodified lines147 148 /// 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 lines169 if opts.full_path {170 return Ok(());171 }172 173+ // 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+}181+182+/// Apply the path-separator diagnostic to a single pattern. See183+/// [`ensure_search_pattern_is_not_a_path`] for the rationale of each case.184+fn 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('/');190 191 // 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 still1 unmodified line195 // 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 }201 202 if should_warn {203 Err(anyhow!(2 unmodified lines206 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 linesmodified:tests/tests.rs+25 added
415 unmodified lines416 one/two/C.Foo2",417 );418 }419 420+/// `--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]425+fn test_and_pattern_with_forward_slash_is_rejected() {426+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);427+428+ // 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+ );433+434+ // `--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+}444+445 /// Explicit root path446 #[test]447 fn test_explicit_root_path() {448 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);2430 unmodified linesmodified:CHANGELOG.md+1 added
5 unmodified lines6 7 ## 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).12 13 # 10.4.214 849 unmodified lines