ag output differs depending on whether it's invoked from a subshell or not
Problem:
Expected behavior:
When output goes to a pipe, ag output should be the same whether it's invoked from a subshell or not
Actual behavior:
When invoked from a subshell, ag output to a pipe is in the format $matching_line, but when invoked from a "top-level" shell, ag output to a pipe is in the format $filename:$lineno:$matching_line
This looks liks a variant of this bug
Example:
Top-level:
bagel.pls:3:File1=http://ice2.somafm.com/bagel-128-mp3
bagel.pls:6:File2=http://ice6.somafm.com/bagel-128-mp3
bagel.pls:9:File3=http://ice4.somafm.com/bagel-128-mp3
bagel.pls:12:File4=http://ice1.somafm.com/bagel-128-mp3Subshell:
File1=http://ice2.somafm.com/bagel-128-mp3
File2=http://ice6.somafm.com/bagel-128-mp3
File3=http://ice4.somafm.com/bagel-128-mp3
File4=http://ice1.somafm.com/bagel-128-mp3Debugging procedure
Let's use dtrace to pop the hood and take a look at ag output as it's being written out by the ag process and into the pipe. We're going to invoke dtrace in one shell, and a test command in another. The first shell will show us the output from ag as reported by dtrace. The second shell will show us the test command output.
We'll run the same ag command in both shells: in the first test, ag won't be invoked from a subshell. In the second test, it will.
Since in both tests, ag is reading from a file, dtrace should show us filenames and line numbers in both cases.
First, invoke ag from toplevel:
$ sudo dtrace -n 'syscall::write*:entry /execname == "ag"/ {trace(copyinstr(arg1))}'
dtrace: description 'syscall::write*:entry ' matched 4 probes
CPU ID FUNCTION:NAME
0 961 write_nocancel:entry bagel.pls:3:File1=http://ice2.somafm.com/bagel-128-mp3
bagel.pls:6:File2=http://ice6.somafm.com/bagel-128-mp3
bagel.pls:9:File3=http://ice4.somafm.com/bagel-128-mp3
bagel.pls:12:File4=http://ice1.somafm.com/bagel-128-mp3
^C
-------------------------
$ ag mp3 bagel*|cut -d: -f1|sort -u
bagels.plsNow let's invoke ag from a subshell. ag's output as reported by dtrace should be identical.
$ sudo dtrace -n 'syscall::write*:entry /execname == "ag"/ {trace(copyinstr(arg1))}'
dtrace: description 'syscall::write*:entry ' matched 4 probes
CPU ID FUNCTION:NAME
4 961 write_nocancel:entry File1=http://ice2.somafm.com/bagel-128-mp3
File2=http://ice6.somafm.com/bagel-128-mp3
File3=http://ice4.somafm.com/bagel-128-mp3
File4=http://ice1.somafm.com/bagel-128-mp3
^C
-------------------------
$ ls bagel*|grep -v "$(ag mp3 bagel*|cut -d: -f1|sort -u)"
bagel.pls
bagel130.pls
bagel32.pls
bagel64.plsThe output format as reported by dtrace has changed: ag is just printing matching lines without prefixing them with $filename:$lineno: as would be expected.
Input files available here: bagels.zip
Thank you for providing us with ag!
Source: ggreer/the_silver_searcher