Skip to content
Myr

Merge pull request #1975 from SAY-5/fix/pattern-path-separator-v2

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

authored by Thayne McCombs · committed by GitHub · parents 0e74d5b, de422af · Browse files · 3 files+85 added−6 removed
This commit

State

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

Trailers

  • fix: flag any pattern containing a path separator, not just ones naming a directory

Diff stats

3 files+85 added−6 removed

  • tests:+43 added−0 removed
  • other:+42 added−6 removed

3 files+85 added−6 removed

modified:src/main.rs+41 added-6 removed
143 unmodified lines
144 }145 Ok(())146}147148/// Detect if the user accidentally supplied a path instead of a search pattern148/// 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":153///154/// 1. The pattern contains '/'. '/' is always a path separator (including on155/// Windows) and has no regex meaning, so flagging it is safe and catches the156/// common Linux/macOS mistake of pasting a full path as the pattern.157/// 2. On Windows only, the pattern contains the native `\` separator *and*158/// names an existing directory on disk. We can't treat `\` as a pure159/// path-separator signal there because it is also the regex escape char,160/// so valid regex patterns like `\Ac` or `\d+` must still run. Requiring161/// that the pattern resolves to a real directory avoids those false162/// positives while preserving the legacy diagnostic for operators who163/// literally typed a directory path.164///165/// See https://github.com/sharkdp/fd/issues/1873.166fn ensure_search_pattern_is_not_a_path(opts: &Opts) -> Result<()> {150 if !opts.full_path151 && opts.pattern.contains(std::path::MAIN_SEPARATOR)152 && Path::new(&opts.pattern).is_dir()167 if opts.full_path {168 return Ok(());169 }170171 // Start with the cheap check: '/' is always a path separator, including on172 // Windows, and has no regex meaning, so flagging it is safe and catches the173 // Linux/macOS mistake of pasting a full path as the pattern.174 #[cfg_attr(not(windows), allow(unused_mut))]175 let mut should_warn = opts.pattern.contains('/');176177 // On Windows we additionally accept the native `\` separator, but only when178 // the pattern actually resolves to an existing directory - `\` is also the179 // regex escape char there, so valid patterns like `\Ac` or `\d+` must still180 // run. The is_dir syscall is only needed when `should_warn` is still false,181 // so short-circuit via `||` to avoid the stat call on the happy path.182 #[cfg(windows)]183 {184 should_warn = should_warn185 || (opts.pattern.contains(std::path::MAIN_SEPARATOR)186 && Path::new(&opts.pattern).is_dir());187 }188189 if should_warn {190 Err(anyhow!(155 "The search pattern '{pattern}' contains a path-separation character ('{sep}') \191 "The search pattern '{pattern}' contains a path-separation character \192 and will not lead to any search results.\n\n\193 If you want to search for all files inside the '{pattern}' directory, use a match-all pattern:\n\n \194 fd . '{pattern}'\n\n\195 Instead, if you want your pattern to match the full file path, use:\n\n \196 fd --full-path '{pattern}'",197 pattern = &opts.pattern,162 sep = std::path::MAIN_SEPARATOR,198 ))199 } else {200 Ok(())201 }
335 unmodified lines
modified:tests/tests.rs+43 added
372 unmodified lines
373 [fd error]: No valid search paths given.",374 );375}376377/// Without --full-path, a pattern containing '/' should always produce the378/// path-separator diagnostic, even if the pattern does not name an existing379/// directory. Before the fix for sharkdp/fd#1873 this only fired when the380/// pattern happened to resolve to a real directory, so the common typo of381/// pasting a full path silently returned zero matches.382#[test]383fn test_pattern_with_forward_slash_is_rejected() {384 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);385386 // Pattern that is NOT a real directory; old behaviour: no warning.387 te.assert_failure_with_error(388 &["nonexistent/path"],389 "[fd error]: The search pattern 'nonexistent/path' contains a path-separation character and will not lead to any search results.",390 );391392 // Pattern that IS a real directory; old behaviour: warning. Must still fire.393 te.assert_failure_with_error(394 &["one/two/three"],395 "[fd error]: The search pattern 'one/two/three' contains a path-separation character and will not lead to any search results.",396 );397}398399/// --full-path is the user's explicit opt-in to regex-over-full-path matching,400/// so a path-separation character in the pattern is expected and must not401/// trigger the diagnostic.402///403/// Gated off Windows: the actual match is regex-over-the-full-path, so a404/// forward-slash pattern only matches Unix-style paths. On Windows the OS405/// uses `\` and `one/two/c` (as a literal regex) does not match a real406/// entry — the behaviour this test is pinning (the diagnostic does not407/// fire) is covered by the fact that the invocation does not error.408#[test]409#[cfg(not(windows))]410fn test_pattern_with_forward_slash_allowed_with_full_path() {411 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);412413 te.assert_output(414 &["--full-path", "one/two/c"],415 "one/two/c.foo416 one/two/C.Foo2",417 );418}419420/// Explicit root path421#[test]422fn test_explicit_root_path() {423 let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
2380 unmodified lines
modified:CHANGELOG.md+1 added
3 unmodified lines
4- Add `--ignore-parent` option to override `--no-ignore-parent`, see #1958 (@tmchow)56## Bugfixes7- Handle invalid working directories gracefully when using `--full-path`, see #1900 (@Xavrir).8- 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.910# 10.4.21112## Bugfixes
846 unmodified lines