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
30 changes: 19 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,28 @@ 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 is_dir = Path::new(&opts.pattern).is_dir();
let mut message = 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}'",
and will not lead to any search results.\n\n",
pattern = &opts.pattern,
sep = std::path::MAIN_SEPARATOR,
))
);
if is_dir {
message += &format!(
"If you want to search for all files inside the '{pattern}' directory, \
use a match-all pattern:\n\n \
fd . '{pattern}'\n\n",
pattern = &opts.pattern,
);
}
message += &format!(
"If you want your pattern to match the full file path, use:\n\n \
fd --full-path '{pattern}'",
pattern = &opts.pattern,
);
Err(anyhow!(message))
} else {
Ok(())
}
Expand Down
26 changes: 26 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,32 @@ fn test_multi_file_with_missing() {
);
}

/// Pattern containing path separator should always produce an error,
/// not only when the pattern is an existing directory.
/// Regression test for https://github.com/sharkdp/fd/issues/1873
#[test]
fn test_path_separator_in_pattern_non_directory() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);

// A pattern with '/' that is NOT an existing directory should still error
te.assert_failure_with_error(
&["foo/bar"],
"[fd error]: The search pattern 'foo/bar' contains a path-separation character ('/') and will not lead to any search results.",
);
}

#[test]
fn test_path_separator_in_pattern_existing_directory() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);

// A pattern with '/' that IS an existing directory should also error
// and include the directory-specific hint
te.assert_failure_with_error(
&["one/two"],
"[fd error]: The search pattern 'one/two' contains a path-separation character ('/') and will not lead to any search results.",
);
}

/// Explicit root path
#[test]
fn test_explicit_root_path() {
Expand Down