Skip to content

fix: always warn when pattern contains path separator, not only for existing dirs#1902

Open
kadaliao wants to merge 2 commits intosharkdp:masterfrom
kadaliao:fix/path-separator-warning
Open

fix: always warn when pattern contains path separator, not only for existing dirs#1902
kadaliao wants to merge 2 commits intosharkdp:masterfrom
kadaliao:fix/path-separator-warning

Conversation

@kadaliao
Copy link

@kadaliao kadaliao commented Mar 7, 2026

Problem

Fixes #1873.

ensure_search_pattern_is_not_a_path only showed the path-separator warning when the pattern happened to be an existing directory (Path::new(&opts.pattern).is_dir()). Patterns containing / that do not correspond to an existing directory silently produced no results.

Reproducer (before fix):

mkdir -p dir1/dir2
fd dir1/dir2   # ✅ warns (existing dir)
fd r1/dir2     # ❌ silent, no results (non-existing path)
fd some/nonexistent/pattern  # ❌ silent, no results

Fix

Remove the is_dir() guard so the warning fires whenever the pattern contains a path separator and --full-path is not set.

Windows caveat: \ is simultaneously a path separator and a valid regex escape character (\d, \w, etc.) on Windows. To avoid false-positive warnings on legitimate regex patterns, the fix only checks for / on Windows.

let pattern_has_separator = if cfg!(windows) {
    opts.pattern.contains('/')
} else {
    opts.pattern.contains(std::path::MAIN_SEPARATOR)
};

Tests

Added 4 unit tests for ensure_search_pattern_is_not_a_path:

  • Pattern with separator, non-existent path → now errors ✅
  • Pattern with separator, existing dir → still errors ✅
  • Pattern without separator → Ok(())
  • --full-path flag suppresses warning → Ok(())

All 242 tests pass (cargo test).

kadaliao added 2 commits March 7, 2026 09:51
…xisting dirs

Previously, ensure_search_pattern_is_not_a_path() only showed the warning
when the pattern was an existing directory (is_dir() guard). Patterns like
'some/nonexistent/path' would silently produce no results.

Remove the is_dir() guard so the warning fires whenever the pattern contains
a path separator (and --full-path is not set).

Windows caveat: backslash is both a path separator and a valid regex escape
character on Windows, so we only check for forward slashes there.

Fixes sharkdp#1873
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] The 'pattern contains path separator' error is only shown when pattern is an existing directory

1 participant