Skip to content
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

## Bugfixes

- Show the "pattern contains path separator" error consistently, even when the pattern is not an existing directory, see #1873 (@bejaratommy)

- Fix Windows hyperlink generation for paths with spaces. (#1872)

- `--print0` combined with `--exec` will now print a `\0` between the output of each entry. Note that if there are multiple instances
Expand Down
35 changes: 21 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,27 @@ 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!(
"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,
))
if !opts.full_path && opts.pattern.contains(std::path::MAIN_SEPARATOR) {
let pattern = &opts.pattern;
let sep = std::path::MAIN_SEPARATOR;

if Path::new(pattern).is_dir() {
Err(anyhow!(
"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}'",
))
} else {
Err(anyhow!(
"The search pattern '{pattern}' contains a path-separation character ('{sep}') \
and will not lead to any search results.\n\n\
If you want your pattern to match the full file path, use:\n\n \
fd --full-path '{pattern}'",
))
}
} 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 @@ -2758,3 +2758,29 @@ fn test_ignore_contain_precedence_over_root_check() {
let expected = "";
te.assert_output(&["--ignore-contain=CACHEDIR.TAG", "."], expected);
}

#[test]
fn test_path_separator_in_pattern_not_a_directory() {
let te = TestEnv::new(&["one"], &["one/a.txt"]);

// A pattern with a path separator that is NOT an existing directory should
// still produce an error, see #1873
te.assert_failure(&["nonexistent/path"]);
}

#[test]
fn test_path_separator_in_pattern_is_a_directory() {
let te = TestEnv::new(&["one/two"], &["one/two/a.txt"]);

// A pattern with a path separator that IS an existing directory should
// also produce an error
te.assert_failure(&["one/two"]);
}

#[test]
fn test_path_separator_in_pattern_full_path() {
let te = TestEnv::new(&["one"], &["one/a.txt"]);

// With --full-path, patterns with path separators should work fine
te.assert_output(&["--full-path", "one/a"], "one/a.txt");
}