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
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
pub fn print_error(msg: impl Into<String>) {
eprintln!("[fd error]: {}", msg.into());
}

pub fn print_warning(msg: impl Into<String>) {
eprintln!("[fd warning]: {}", msg.into());
}
51 changes: 51 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use regex::bytes::{Regex, RegexBuilder, RegexSetBuilder};

use crate::cli::{ColorWhen, HyperlinkWhen, Opts};
use crate::config::Config;
use crate::error::print_warning;
use crate::exec::CommandSet;
use crate::exit_codes::ExitCode;
use crate::filetypes::FileTypes;
Expand Down Expand Up @@ -86,6 +87,7 @@ fn run() -> Result<ExitCode> {
}

ensure_search_pattern_is_not_a_path(&opts)?;
warn_if_full_path_glob_missing_leading_anchor(&opts);
let pattern = &opts.pattern;
let exprs = &opts.exprs;
let empty = Vec::new();
Expand Down Expand Up @@ -201,6 +203,31 @@ fn ensure_search_pattern_is_not_a_path(opts: &Opts) -> Result<()> {
}
}

/// With `--glob` and `--full-path`, patterns match the entire absolute path.
/// Warn when the pattern does not start with a path root or glob wildcard, which often means no
/// matches (#1650).
fn warn_if_full_path_glob_missing_leading_anchor(opts: &Opts) {
if !opts.glob || !opts.full_path || opts.pattern.is_empty() {
return;
}
if full_path_glob_has_leading_anchor(&opts.pattern) {
return;
}
print_warning(
"With --glob and --full-path, the pattern matches the whole absolute path and \
usually should start with '/' or '**/' (for example: '**/foo.txt').",
);
}

fn full_path_glob_has_leading_anchor(pattern: &str) -> bool {
let bytes = pattern.as_bytes();
matches!(bytes.first(), Some(b'*' | b'/' | b'\\'))
|| matches!(
bytes,
[drive, b':', b'/' | b'\\', ..] if drive.is_ascii_alphabetic()
)
}

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 Expand Up @@ -534,3 +561,27 @@ fn build_regex(pattern_regex: String, config: &Config) -> Result<regex::bytes::R
)
})
}

#[cfg(test)]
mod tests {
use super::full_path_glob_has_leading_anchor;

#[test]
fn full_path_glob_anchor_accepts_absolute_and_wildcard_patterns() {
assert!(full_path_glob_has_leading_anchor("/tmp/file.txt"));
assert!(full_path_glob_has_leading_anchor(r"C:\Users\me\file.txt"));
assert!(full_path_glob_has_leading_anchor("C:/Users/me/file.txt"));
assert!(full_path_glob_has_leading_anchor(
r"\\server\share\file.txt"
));
assert!(full_path_glob_has_leading_anchor("**/file.txt"));
assert!(full_path_glob_has_leading_anchor("*file.txt"));
}

#[test]
fn full_path_glob_anchor_rejects_relative_patterns() {
assert!(!full_path_glob_has_leading_anchor("file.txt"));
assert!(!full_path_glob_has_leading_anchor("dir/file.txt"));
assert!(!full_path_glob_has_leading_anchor(r"C:file.txt"));
}
}
15 changes: 15 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,21 @@ fn test_full_path_glob_searches() {
);
}

#[cfg(not(windows))]
#[test]
fn test_warn_when_full_path_glob_missing_leading_anchor() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);

te.assert_output(&["--glob", "--full-path", "foo.txt"], "");

let output = te.assert_success_and_get_output(".", &["--glob", "--full-path", "foo.txt"]);
let stderr = String::from_utf8_lossy(&output.stderr);
assert!(
stderr.contains("[fd warning]:") && stderr.contains("**/foo.txt"),
"expected full-path glob anchor warning, got: {stderr}"
);
}

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