Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,29 @@ fn set_working_dir(opts: &Opts) -> Result<()> {

/// Detect if the user accidentally supplied a path instead of a search pattern
fn ensure_search_pattern_is_not_a_path(opts: &Opts) -> Result<()> {
if !opts.full_path
&& opts.pattern.contains(std::path::MAIN_SEPARATOR)
&& Path::new(&opts.pattern).is_dir()
{
Err(anyhow!(
if !opts.full_path && opts.pattern.contains(std::path::MAIN_SEPARATOR) {
let pattern = &opts.pattern;
let sep = std::path::MAIN_SEPARATOR;

let mut msg = format!(
"The search pattern '{pattern}' contains a path-separation character ('{sep}') \
and will not lead to any search results.\n\n\
If you want to search for all files inside the '{pattern}' directory, use a match-all pattern:\n\n \
fd . '{pattern}'\n\n\
Instead, if you want your pattern to match the full file path, use:\n\n \
fd --full-path '{pattern}'",
pattern = &opts.pattern,
sep = std::path::MAIN_SEPARATOR,
))
and will not lead to any search results."
);

if Path::new(pattern).is_dir() {
msg += &format!(
"\n\nIf you want to search for all files inside the '{pattern}' directory, \
use a match-all pattern:\n\n \
fd . '{pattern}'"
);
}

msg += &format!(
"\n\nIf you want your pattern to match the full file path, use:\n\n \
fd --full-path '{pattern}'"
);

Err(anyhow!(msg))
} else {
Ok(())
}
Expand Down
17 changes: 17 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2578,6 +2578,23 @@ fn test_opposing(flag: &str, opposing_flags: &[&str]) {
);
}

/// Print error if search pattern contains a path separator, even if the
/// pattern is not an existing directory (see #1873)
#[test]
#[cfg(unix)]
fn test_error_if_pattern_contains_path_separator() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);

// Pattern that IS an existing directory should still error
te.assert_failure(&["one/two"]);

// Pattern with path separator that is NOT an existing directory should also error
te.assert_failure(&["nonexistent/path"]);

// --full-path should suppress the error
te.assert_output(&["--full-path", "one/two"], "one/two/\none/two/c.foo\none/two/C.Foo2\none/two/three/\none/two/three/d.foo\none/two/three/directory_foo/");
}

/// Print error if search pattern starts with a dot and --hidden is not set
/// (Unix only, hidden files on Windows work differently)
#[test]
Expand Down
Loading