Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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());
}
20 changes: 20 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,24 @@ fn ensure_search_pattern_is_not_a_path(opts: &Opts) -> Result<()> {
}
}

/// With `--glob` and `--full-path`, patterns match the entire absolute path (from `/`).
/// Warn when the pattern does not start with `/` or `*`, 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;
}
let Some(first) = opts.pattern.chars().next() else {
return;
};
if first == '/' || first == '*' {

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 is only correct on unix systems. On windows an absolute path could start with a letter (for example it could start with "c:\").

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 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
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
Loading