mirror of
https://github.com/BurntSushi/ripgrep.git
synced 2025-07-26 01:31:57 -07:00
cli: print warning if nothing was searched
This was once part of ripgrep, but at some point, was unintentionally removed. The value of this warning is that since ripgrep tries to be "smart" by default, it can be surprising if it doesn't search certain things. This warning covers the case when ripgrep searches *nothing*, which happens somewhat more frequently than you might expect. e.g., If you're searching within an ignore directory. Note that for now, we only print this message when the user has not supplied any explicit paths. It's not clear that we want to print this otherwise, and in particular, it seems that the message shows up too eagerly. e.g., 'rg foo does-not-exist' will both print an error about 'does-not-exist' not existing, *and* the message about no files being searched, which seems annoying in this case. We can always refine this logic later. Fixes #1404, Closes #1762
This commit is contained in:
committed by
Andrew Gallant
parent
0f502a9439
commit
e6cac8b119
@@ -83,12 +83,14 @@ fn search(args: &Args) -> Result<bool> {
|
||||
let mut stats = args.stats()?;
|
||||
let mut searcher = args.search_worker(args.stdout())?;
|
||||
let mut matched = false;
|
||||
let mut searched = false;
|
||||
|
||||
for result in args.walker()? {
|
||||
let subject = match subject_builder.build_from_result(result) {
|
||||
Some(subject) => subject,
|
||||
None => continue,
|
||||
};
|
||||
searched = true;
|
||||
let search_result = match searcher.search(&subject) {
|
||||
Ok(search_result) => search_result,
|
||||
Err(err) => {
|
||||
@@ -108,6 +110,9 @@ fn search(args: &Args) -> Result<bool> {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if args.using_default_path() && !searched {
|
||||
eprint_nothing_searched();
|
||||
}
|
||||
if let Some(ref stats) = stats {
|
||||
let elapsed = Instant::now().duration_since(started_at);
|
||||
// We don't care if we couldn't print this successfully.
|
||||
@@ -129,11 +134,13 @@ fn search_parallel(args: &Args) -> Result<bool> {
|
||||
let bufwtr = args.buffer_writer()?;
|
||||
let stats = args.stats()?.map(Mutex::new);
|
||||
let matched = AtomicBool::new(false);
|
||||
let searched = AtomicBool::new(false);
|
||||
let mut searcher_err = None;
|
||||
args.walker_parallel()?.run(|| {
|
||||
let bufwtr = &bufwtr;
|
||||
let stats = &stats;
|
||||
let matched = &matched;
|
||||
let searched = &searched;
|
||||
let subject_builder = &subject_builder;
|
||||
let mut searcher = match args.search_worker(bufwtr.buffer()) {
|
||||
Ok(searcher) => searcher,
|
||||
@@ -148,6 +155,7 @@ fn search_parallel(args: &Args) -> Result<bool> {
|
||||
Some(subject) => subject,
|
||||
None => return WalkState::Continue,
|
||||
};
|
||||
searched.store(true, SeqCst);
|
||||
searcher.printer().get_mut().clear();
|
||||
let search_result = match searcher.search(&subject) {
|
||||
Ok(search_result) => search_result,
|
||||
@@ -181,6 +189,9 @@ fn search_parallel(args: &Args) -> Result<bool> {
|
||||
if let Some(err) = searcher_err.take() {
|
||||
return Err(err);
|
||||
}
|
||||
if args.using_default_path() && !searched.load(SeqCst) {
|
||||
eprint_nothing_searched();
|
||||
}
|
||||
if let Some(ref locked_stats) = stats {
|
||||
let elapsed = Instant::now().duration_since(started_at);
|
||||
let stats = locked_stats.lock().unwrap();
|
||||
@@ -191,6 +202,14 @@ fn search_parallel(args: &Args) -> Result<bool> {
|
||||
Ok(matched.load(SeqCst))
|
||||
}
|
||||
|
||||
fn eprint_nothing_searched() {
|
||||
err_message!(
|
||||
"No files were searched, which means ripgrep probably \
|
||||
applied a filter you didn't expect.\n\
|
||||
Running with --debug will show why files are being skipped."
|
||||
);
|
||||
}
|
||||
|
||||
/// The top-level entry point for listing files without searching them. This
|
||||
/// recursively steps through the file list (current directory by default) and
|
||||
/// prints each path sequentially using a single thread.
|
||||
|
Reference in New Issue
Block a user