Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warning if nothing was searched #1762

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
19 changes: 19 additions & 0 deletions crates/core/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -108,6 +110,9 @@ fn search(args: &Args) -> Result<bool> {
break;
}
}
if !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.
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -178,6 +186,9 @@ fn search_parallel(args: &Args) -> Result<bool> {
}
})
});
if !searched.load(SeqCst) {
eprint_nothing_searched();
}
if let Some(err) = searcher_err.take() {
return Err(err);
}
Expand All @@ -191,6 +202,14 @@ fn search_parallel(args: &Args) -> Result<bool> {
Ok(matched.load(SeqCst))
}

fn eprint_nothing_searched() {
eprintln!(
"No files were searched, which means ripgrep probably \
applied a filter you didn't expect. \
Try running again with --debug."
);
}

/// 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.
Expand Down
18 changes: 18 additions & 0 deletions tests/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,24 @@ rgtest!(f1466_no_ignore_files, |dir: Dir, mut cmd: TestCommand| {
eqnice!("foo\n", cmd.arg("-u").stdout());
});

// See: https://github.com/BurntSushi/ripgrep/issues/1404
rgtest!(f1404_nothing_searched_warning, |dir: Dir, mut cmd: TestCommand| {
dir.create(".ignore", "ignored-dir/**");
dir.create_dir("ignored-dir");
dir.create("ignored-dir/foo", "needle");

// Test that, if ripgrep searches only ignored folders/files, warning is displayed
cmd.arg("needle");
cmd.assert_err();

let output = cmd.cmd().output().unwrap();
let stderr = String::from_utf8_lossy(&output.stderr);
let expected = "\
No files were searched, which means ripgrep probably applied a filter you didn't expect. Try running again with --debug.
";
eqnice!(expected, stderr);
});

rgtest!(no_context_sep, |dir: Dir, mut cmd: TestCommand| {
dir.create("test", "foo\nctx\nbar\nctx\nfoo\nctx");
cmd.args(&["-A1", "--no-context-separator", "foo", "test"]);
Expand Down
4 changes: 3 additions & 1 deletion tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ rgtest!(r428_color_context_path, |dir: Dir, mut cmd: TestCommand| {
});

// See: https://github.com/BurntSushi/ripgrep/issues/428
rgtest!(r428_unrecognized_style, |_: Dir, mut cmd: TestCommand| {
rgtest!(r428_unrecognized_style, |dir: Dir, mut cmd: TestCommand| {
dir.create("file.txt", "Sherlock");

cmd.arg("--colors=match:style:").arg("Sherlock");
cmd.assert_err();

Expand Down