From 9268ff8e8db179a484810cfbc40f3a8dba74ccaf Mon Sep 17 00:00:00 2001 From: Ninan John Date: Wed, 5 Jun 2019 21:03:52 +0530 Subject: [PATCH] ripgrep: fix bug when CWD has directory named `-` Specifically, when searching stdin, if the current directory has a directory named `-`, then the `--with-filename` flag would automatically be turned on. This is because `--with-filename` is automatically enabled when ripgrep is given a single path that is a directory. When ripgrep is given empty arguments, and if it is searching stdin, then its default path list is just simple `["-"]`. The `is_dir` check passes, and `--with-filename` gets enabled. This commit fixes the problem by checking whether the path is `-` first. If so, then we assume it isn't a directory. This is fine, since if it is a directory and one asks to search it explicitly, then ripgrep will interpret `-` as stdin anyway (which is arguably a bug on its own, but probably not one worth fixing). Fixes #1223, Closes #1292 --- src/args.rs | 3 ++- tests/regression.rs | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/args.rs b/src/args.rs index 09f924dc..63526889 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1594,10 +1594,11 @@ impl ArgMatches { if self.is_present("no-filename") { false } else { + let path_stdin = Path::new("-"); self.is_present("with-filename") || self.is_present("vimgrep") || paths.len() > 1 - || paths.get(0).map_or(false, |p| p.is_dir()) + || paths.get(0).map_or(false, |p| p != path_stdin && p.is_dir()) } } } diff --git a/tests/regression.rs b/tests/regression.rs index 88f2194d..e8c915ae 100644 --- a/tests/regression.rs +++ b/tests/regression.rs @@ -706,6 +706,18 @@ rgtest!(r1203_reverse_suffix_literal, |dir: Dir, _: TestCommand| { eqnice!("153.230000\n", cmd.arg(r"\d\d\d000").arg("test").stdout()); }); +// See: https://github.com/BurntSushi/ripgrep/issues/1223 +rgtest!(r1223_no_dir_check_for_default_path, |dir: Dir, mut cmd: TestCommand| { + dir.create_dir("-"); + dir.create("a.json", "{}"); + dir.create("a.txt", "some text"); + + eqnice!( + "a.json\na.txt\n", + sort_lines(&cmd.arg("a").pipe(b"a.json\na.txt")) + ); +}); + // See: https://github.com/BurntSushi/ripgrep/issues/1259 rgtest!(r1259_drop_last_byte_nonl, |dir: Dir, mut cmd: TestCommand| { dir.create("patterns-nonl", "[foo]");