ggreer / the_silver_searcher

A code-searching tool similar to ack, but faster.
http://geoff.greer.fm/ag/
Apache License 2.0
26.12k stars 1.42k forks source link

ag -l executed from cron shows no results #753

Open richq opened 9 years ago

richq commented 9 years ago

If I write a script with ag in it to find which files have "foo" in them:

#!/bin/sh
ag -l foo ~/src/the_silver_searcher

and run that from a shell /home/richq/checkag.sh > /tmp/checkag.log 2>&1 then I get results as expected in the /tmp/checkag.log file:

/home/richq/src/the_silver_searcher/src/options.c
/home/richq/src/the_silver_searcher/tests/empty_match.t

etc. But if I run this exact line from a cron job it doesn't output any results. This used to work - a script I have in my crontab stopped working when I updated ag recently from 0.29 to the latest release.

I git-bisected the problem down to commit df5d456 Fix --count functionality to work with --nofilename. Indeed if I add --vimgrep to the ag command line it spits out results again from the cron job. I'm running Arch Linux, bash shell, cron is cronie 1.5.0. To reproduce the problem I had this line in my crontab to run the test script every minute and monitor the results:

* * * * *       /home/richq/checkag.sh > /tmp/checkag.log 2>&1

As I say, ag -l works fine from the command line, and that cron-line works too when run interactively.

Debugging a bit, I found that the reason for this is that in src/options.c, around line 316 it checks S_ISFIFO on stdin, and apparently when run from cron this returns 1, it is a pipe. This sets opts.search_stream to 1 even though we aren't really searching a stream, just that stdin is not there as the shell isn't interactive. Then around line 669 it sets opts.print_path = PATH_PRINT_NOTHING since it is in search_stream mode, which later on filters out the path printing as seen in the "regression" bisection. I am not sure what the fix is for this though, probably the pipe/file/stdin detection needs tweaking (technical term, heh).

fiddlerwoaroof commented 8 years ago

I'm running into this problem too. Perhaps a solution would just be to provide an argument to force not search_stream mode or something.

richq commented 8 years ago

@fiddlerwoaroof that flag is effectively --vimgrep, which is what I use for now when running ag from a crontab.

cben commented 5 years ago

In ag 2.1.0 (on Fedora), as well as built from current master (2.2.0+), adding . also works to disable stdin pipe auto-detection: foo | ag [--OPTS] PATTERN .

cben commented 5 years ago

Peeking at the code, this was supposed got fixed by #58. This issue is 3 years newer than that fix, but I just tested master, so I think this can be closed.
Only when you don't specify any paths to search, ISPIPE check determines stdin vs current directory.

wolfgangwalther commented 3 years ago

I just experienced a similar problem - adding a path did not fix it, but using --vimgrep worked.

I can reproduce it by using entr -r, which closes stdin as well:

# the first ag -l is just to give entr something to work with
# the second ag -l is important

# shows no files
ag -l | entr -r ag -l

# shows no files
ag -l | entr -r ag -l .

# this works
ag -l | entr -r ag -l --vimgrep

ag --version

ag version 2.2.0

Features:
  +jit +lzma +zlib

I don't think this is fixed.