Skip to content
Open
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
72 changes: 58 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,69 @@ 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) {
// On Windows, backslash is both a path separator and a regex escape character.
// We need to distinguish between paths (e.g., "C:\path" or "\nonexistent") and
// regex patterns (e.g., "\Ac" where \A is a regex anchor).
// A simple heuristic: if the pattern looks like it could be a path (not just
// a single-character regex escape), show the error.
let looks_like_path = if cfg!(windows) {
// On Windows, check if it's a drive path (C:\) or if the backslash is
// followed by something that looks like a path component (not a single regex escape)
let is_drive_path = opts.pattern.len() >= 3
&& opts
.pattern
.chars()
.next()
.is_some_and(|c| c.is_ascii_alphabetic())
&& opts.pattern.chars().nth(1) == Some(':')
&& opts.pattern.chars().nth(2) == Some(std::path::MAIN_SEPARATOR);
Comment on lines +159 to +166
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let is_drive_path = opts.pattern.len() >= 3
&& opts
.pattern
.chars()
.next()
.is_some_and(|c| c.is_ascii_alphabetic())
&& opts.pattern.chars().nth(1) == Some(':')
&& opts.pattern.chars().nth(2) == Some(std::path::MAIN_SEPARATOR);
let pattern_bytes = opts.pattern.bytes();
let is_drive_path = pattern_bytes.len() >= 3 && pattern_bytes[0].is_ascii_alphabetic()
&& pattern_bytes[1] == b':'
&& pattern_bytes[2].into() == std::path::MAIN_SEPARATOR;

is_drive_path
|| (opts.pattern.matches(std::path::MAIN_SEPARATOR).count() > 0
&& !is_likely_regex_escape(&opts.pattern))
} else {
// On Unix, if it starts with / or contains /, it's likely a path
true
};
Comment on lines +156 to +173
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let looks_like_path = if cfg!(windows) {
// On Windows, check if it's a drive path (C:\) or if the backslash is
// followed by something that looks like a path component (not a single regex escape)
let is_drive_path = opts.pattern.len() >= 3
&& opts
.pattern
.chars()
.next()
.is_some_and(|c| c.is_ascii_alphabetic())
&& opts.pattern.chars().nth(1) == Some(':')
&& opts.pattern.chars().nth(2) == Some(std::path::MAIN_SEPARATOR);
is_drive_path
|| (opts.pattern.matches(std::path::MAIN_SEPARATOR).count() > 0
&& !is_likely_regex_escape(&opts.pattern))
} else {
// On Unix, if it starts with / or contains /, it's likely a path
true
};
#[cfg(windows)]
let looks_like_path = {
// On Windows, check if it's a drive path (C:\) or if the backslash is
// followed by something that looks like a path component (not a single regex escape)
let is_drive_path = opts.pattern.len() >= 3
&& opts
.pattern
.chars()
.next()
.is_some_and(|c| c.is_ascii_alphabetic())
&& opts.pattern.chars().nth(1) == Some(':')
&& opts.pattern.chars().nth(2) == Some(std::path::MAIN_SEPARATOR);
is_drive_path
|| (opts.pattern.matches(std::path::MAIN_SEPARATOR).count() > 0
&& !is_likely_regex_escape(&opts.pattern))
}
// On Unix, if it starts with / or contains /, it's likely a path
#[cfg(not(windows))]
let looks_like_path = true;

So that on non-windows we don't even have to compile the code for the path that we'll never encounter.

It would be nice if we could avoid the if statement below as well, but I'm not sure how to do that in a clean way without repeating the errror. Maybe if we factored the error message out into a const?


if looks_like_path {
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,
))
} else {
Ok(())
}
} else {
Ok(())
}
}

/// Check if a pattern is likely a regex escape sequence rather than a path.
/// This is a heuristic to avoid false positives on Windows where \ is both
/// a path separator and a regex escape character.
fn is_likely_regex_escape(pattern: &str) -> bool {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn is_likely_regex_escape(pattern: &str) -> bool {
#[cfg(windows)]
fn is_likely_regex_escape(pattern: &str) -> bool {

if !cfg!(windows) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get rid of this, and use conditional compilation on the entire function

return false;
}
// Common regex escape sequences: \A, \z, \b, \d, \s, \w, \1, \2, etc.
// If the pattern is very short (like "\Ac") and starts with \ followed by
// a letter or digit, it's likely a regex escape.
if pattern.len() <= 3
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This heuristic will have a lot of false positives. This will prevent you from using escapes in a regex for anything longer than 3 characters on windows.

I'm not really sure what a good path forward is for windows. Honestly, our best option might be to continue using the same heuristic we had before (checking if the path corresponds to a directory). Or maybe just check if the first path component is a directory?

Maybe we could also look for "/" on windows, since that can be a path separator as well.

&& pattern.starts_with('\\')
&& let Some(ch) = pattern.chars().nth(1)
{
return ch.is_ascii_alphanumeric();
}
false
}

fn build_pattern_regex(pattern: &str, opts: &Opts) -> Result<String> {
Ok(if opts.glob && !pattern.is_empty() {
let glob = GlobBuilder::new(pattern).literal_separator(true).build()?;
Expand Down