Skip to content

Commit 4e2c812

Browse files
authored
Merge pull request #1975 from SAY-5/fix/pattern-path-separator-v2
fix: flag any pattern containing a path separator, not just ones naming a directory
2 parents 0e74d5b + de422af commit 4e2c812

3 files changed

Lines changed: 85 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
## Bugfixes
77
- 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.
89

910
# 10.4.2
1011

src/main.rs

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,56 @@ fn set_working_dir(opts: &Opts) -> Result<()> {
145145
Ok(())
146146
}
147147

148-
/// Detect if the user accidentally supplied a path instead of a search pattern
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 pattern
151+
/// containing a path separator can never match. Two cases are worth a friendly
152+
/// error rather than silent "no results":
153+
///
154+
/// 1. The pattern contains '/'. '/' is always a path separator (including on
155+
/// Windows) and has no regex meaning, so flagging it is safe and catches the
156+
/// 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 pure
159+
/// path-separator signal there because it is also the regex escape char,
160+
/// so valid regex patterns like `\Ac` or `\d+` must still run. Requiring
161+
/// that the pattern resolves to a real directory avoids those false
162+
/// positives while preserving the legacy diagnostic for operators who
163+
/// literally typed a directory path.
164+
///
165+
/// See https://github.com/sharkdp/fd/issues/1873.
149166
fn ensure_search_pattern_is_not_a_path(opts: &Opts) -> Result<()> {
150-
if !opts.full_path
151-
&& opts.pattern.contains(std::path::MAIN_SEPARATOR)
152-
&& Path::new(&opts.pattern).is_dir()
167+
if opts.full_path {
168+
return Ok(());
169+
}
170+
171+
// Start with the cheap check: '/' is always a path separator, including on
172+
// Windows, and has no regex meaning, so flagging it is safe and catches the
173+
// 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('/');
176+
177+
// On Windows we additionally accept the native `\` separator, but only when
178+
// the pattern actually resolves to an existing directory - `\` is also the
179+
// regex escape char there, so valid patterns like `\Ac` or `\d+` must still
180+
// 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)]
153183
{
184+
should_warn = should_warn
185+
|| (opts.pattern.contains(std::path::MAIN_SEPARATOR)
186+
&& Path::new(&opts.pattern).is_dir());
187+
}
188+
189+
if should_warn {
154190
Err(anyhow!(
155-
"The search pattern '{pattern}' contains a path-separation character ('{sep}') \
191+
"The search pattern '{pattern}' contains a path-separation character \
156192
and will not lead to any search results.\n\n\
157193
If you want to search for all files inside the '{pattern}' directory, use a match-all pattern:\n\n \
158194
fd . '{pattern}'\n\n\
159195
Instead, if you want your pattern to match the full file path, use:\n\n \
160196
fd --full-path '{pattern}'",
161197
pattern = &opts.pattern,
162-
sep = std::path::MAIN_SEPARATOR,
163198
))
164199
} else {
165200
Ok(())

tests/tests.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,49 @@ fn test_multi_file_with_missing() {
374374
);
375375
}
376376

377+
/// Without --full-path, a pattern containing '/' should always produce the
378+
/// path-separator diagnostic, even if the pattern does not name an existing
379+
/// directory. Before the fix for sharkdp/fd#1873 this only fired when the
380+
/// pattern happened to resolve to a real directory, so the common typo of
381+
/// pasting a full path silently returned zero matches.
382+
#[test]
383+
fn test_pattern_with_forward_slash_is_rejected() {
384+
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
385+
386+
// 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+
);
391+
392+
// 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+
}
398+
399+
/// --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 not
401+
/// trigger the diagnostic.
402+
///
403+
/// Gated off Windows: the actual match is regex-over-the-full-path, so a
404+
/// forward-slash pattern only matches Unix-style paths. On Windows the OS
405+
/// uses `\` and `one/two/c` (as a literal regex) does not match a real
406+
/// entry — the behaviour this test is pinning (the diagnostic does not
407+
/// fire) is covered by the fact that the invocation does not error.
408+
#[test]
409+
#[cfg(not(windows))]
410+
fn test_pattern_with_forward_slash_allowed_with_full_path() {
411+
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);
412+
413+
te.assert_output(
414+
&["--full-path", "one/two/c"],
415+
"one/two/c.foo
416+
one/two/C.Foo2",
417+
);
418+
}
419+
377420
/// Explicit root path
378421
#[test]
379422
fn test_explicit_root_path() {

0 commit comments

Comments
 (0)