Guard not auto-running when files are saved.

1,027 views
Skip to first unread message

Andrew

unread,
May 3, 2012, 1:01:36 PM5/3/12
to guar...@googlegroups.com
I'm relatively new to using Guard. I used it a while back to run my rspec tests while working on a ruby gem on my Mac, but it's been a while since I've touched that project. Now I'm working on a PHP project on Ubuntu and using Guard to run my PHPUnit tests. I installed the guard gem, then the guard-phpunit gem, ran guard init in my tests directory, then guard start and everything is working great, except that I get a prompt after every time the test suite runs. I expected Guard to auto-run when one of the files changes, but it is not. My tests are failing because of PHP errors in my code, but when I change one of the files, I want Guard to run again without me having to go to the terminal and press enter.

Do I have something configured wrong or did I assume incorrectly how Guard is supposed to work?

--Andrew

Andrew

unread,
May 3, 2012, 2:07:10 PM5/3/12
to guar...@googlegroups.com
Here is the contents of my Guardfile which lives in my tests directory:

guard 'phpunit', :cli => '--colors' do

  watch(%r{^.+Test\.php$})

end

I assume this means that all the files that end in Test.php within all subdirectories will be watched. Is that a correct assumption?

Michael Kessler

unread,
May 3, 2012, 3:38:58 PM5/3/12
to guar...@googlegroups.com
Hi Andrew

Your assumption is correct, the watcher expression should catch all changes from files ending with Test.php
The prompt you're seeing is the readline command prompt to give Guard some commands like running a specific guard, pause, etc. and it should not interfere the file watcher. When a file change is triggered, the command prompt goes away.

I'd add some debug statements to see if it's problem with the phpunit guard or with the file listener:

guard :phpunit do
watch(%r{^.+Test\.php$}) { |m| puts m.inspect }
end

Michael
Message has been deleted

Michael Kessler

unread,
May 4, 2012, 7:11:55 AM5/4/12
to guar...@googlegroups.com
> To reiterate, if a file named EmailTest.php is in a subdirectory (not top level) this expression (%r{^.+Test\.php$}) should watch it? How does it know to look in subdirectories, and how deep? Is it just some magic happening behind the scenes?

Yes, it would match:

'dir/dir/fileTest.php' =~ %r{^.+Test\.php}
=> 0

On Linux rb-notify is used to get events about file changes relative to the project root where the Guardfile is located or the directory you specify with the --watchdir option on start.

> Nothing happens when I add that 'puts' statement because it doesn't run itself (isn't watching files?) without me typing enter from the command prompt.

Please verify that notify events are fired correctly with inotifywait, see http://linux.die.net/man/1/inotifywait for more info.

Andrew

unread,
May 4, 2012, 2:27:03 PM5/4/12
to guar...@googlegroups.com
I tried 'gem install rb-notify' but got a message saying there is no gem called rb-notify with suggestions (rb-notifu, rb-inotify, rnotify, rcr-notify, dnotify).

There is also no inotifywait executable installed. By the way, I am running Ubuntu Lucid 64. Did I miss a step? Was I supposed to install some additional Ubuntu packages? I didn't see anything like that listed in the readme. I saw some things about Growl notifications, but I don't care about Growl notifications at this point.

--Andrew

Michael Kessler

unread,
May 4, 2012, 4:54:51 PM5/4/12
to guar...@googlegroups.com
Andrew,

> I tried 'gem install rb-notify' but got a message saying there is no gem called rb-notify with suggestions (rb-notifu, rb-inotify, rnotify, rcr-notify, notify).

You do not need to install a gem, since they are currently vendored within Guard. This will be dropped when the new Listen gem will be integrated…

> There is also no inotifywait executable installed. By the way, I am running Ubuntu Lucid 64. Did I miss a step? Was I supposed to install some additional Ubuntu packages? I didn't see anything like that listed in the readme. I saw some things about Growl notifications, but I don't care about Growl notifications at this point.

I never used Guard on a Linux box by myself, but inotify was included since 2.6.13. I don't know if this is a kernel module that can be disabled or not. You can install the notify-tools http://packages.ubuntu.com/de/lucid/inotify-tools to have the inotifywait executable.

Michael

Andrew

unread,
May 4, 2012, 7:04:00 PM5/4/12
to guar...@googlegroups.com
The other thing I probably should have mentioned from the beginning is that this Ubuntu box is running inside a VirtualBox VM which is being controlled by Vagrant. The project directory is being mounted using NFS. So when I save a file on my Mac, I'm not sure if the normal file change events get triggered through the NFS file sharing. I don't know anything about NFS so I don't know if that could have anything to do with this.

Michael Kessler

unread,
May 6, 2012, 8:08:49 AM5/6/12
to guar...@googlegroups.com
This is a known issue. You can disable rb-inotify by making sure you don't have the gem in your bundle and run Guard with `bundle exec guard --no-vendor`.

Rick Schmitty

unread,
Aug 27, 2012, 1:39:11 PM8/27/12
to guar...@googlegroups.com
Any update on this issue?  I'm having similar problems with VMware Workstation.  Running with --no-vendor doesn't seem to help.

Additionally I get this in output


DEPRECATION: Starting with Guard v1.1 the 'no_vendor' option is removed because the monitoring
gems are now part of a new gem called Listen. (https://github.com/guard/listen)

Thibaud Guillaume-Gentil

unread,
Aug 27, 2012, 2:40:48 PM8/27/12
to guar...@googlegroups.com
You can get the same feature with `bundle exec guard --force-polling` now. Here in the README: https://github.com/guard/guard#-p--force-polling-option

Rick Schmitty

unread,
Aug 28, 2012, 11:29:02 AM8/28/12
to guar...@googlegroups.com
Thanks, that works, however now my tests are running twice.

When I start up with bundlex exec guard -p the tests will run through once, but when I save a file that is being watched, the tests then run twice
rvm 1.15.7 (master) by Wayne E. Seguin <waynee...@gmail.com>, Michal Papis <mpa...@gmail.com> [https://rvm.io/]
ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux]
Rails 3.2.8

.rspec, Gemfile, Guardfile: http://pastebin.com/raw.php?i=aS8jSHaP

Thibaud Guillaume-Gentil

unread,
Aug 28, 2012, 3:36:31 PM8/28/12
to guar...@googlegroups.com
What editor are you using? From where are you saving the file (outside of the vm) ?

More info here: https://github.com/guard/guard/wiki/Analysis-of-inotify-events-for-different-editors

Rick Schmitty

unread,
Aug 29, 2012, 7:44:20 AM8/29/12
to guar...@googlegroups.com
I'm using eclipse (specifically aptana 3) however I also tried it with sublime2 both had the same inotify output

[proj1] (master) $ inotifywatch test.txt
Establishing watches...
Finished establishing watches, now collecting statistics.
^Ctotal  access  close_nowrite  open  filename
12     4       4              4     test.txt

[proj1] (master) $ inotifywatch test.txt
Establishing watches...
Finished establishing watches, now collecting statistics.
^Ctotal  access  close_nowrite  open  filename
12     4       4              4     test.txt

I also did notepad just to compare

Finished establishing watches, now collecting statistics.
^Ctotal  access  close_nowrite  open  filename
15     5       5              5     test.txt


All 3 editors resulted in 2 tests run after save.  I'm saving files from Windows 7 x64, they are in my projects folder in my user directory.  This is mapped through shared folders under CentOS as /mnt/hgfs if that matters

Rick Schmitty

unread,
Aug 29, 2012, 7:53:38 AM8/29/12
to guar...@googlegroups.com
Hmm not sure if I'm running inotifywatch correctly.  I tried on a new blank file and after each save I would add 1 character.  Here is what notepad looked like

[proj1] (master) $ inotifywatch test2.txt

Establishing watches...
Finished establishing watches, now collecting statistics.
^Ctotal  close_nowrite  open  filename
4      2              2     test2.txt
[proj1] (master) $ inotifywatch test2.txt

Establishing watches...
Finished establishing watches, now collecting statistics.
^Ctotal  access  close_nowrite  open  filename
6      2       2              2     test2.txt
[proj1] (master) $ inotifywatch test2.txt

Establishing watches...
Finished establishing watches, now collecting statistics.
^Ctotal  access  close_nowrite  open  filename
12     4       4              4     test2.txt


and here is with eclipse

[proj1] (master) $ inotifywatch test2.txt

Establishing watches...
Finished establishing watches, now collecting statistics.
^Ctotal  close_nowrite  open  filename
6      3              3     test2.txt
[proj1] (master) $ inotifywatch test2.txt

Establishing watches...
Finished establishing watches, now collecting statistics.
^Ctotal  access  close_nowrite  open  filename
9      3       3              3     test2.txt
[proj1] (master) $ inotifywatch test2.txt

Establishing watches...
Finished establishing watches, now collecting statistics.
^Ctotal  access  close_nowrite  open  filename
9      3       3              3     test2.txt
[proj1] (master) $ inotifywatch test2.txt

Establishing watches...
Finished establishing watches, now collecting statistics.
^Ctotal  access  close_nowrite  open  filename
6      2       2              2     test2.txt

Thibaud Guillaume-Gentil

unread,
Aug 30, 2012, 2:48:19 AM8/30/12
to guar...@googlegroups.com
You can try to ignore swap files by adding that in your Guardfile:

# Ignore Vim swap files
ignore /~$/
ignore /^(?:.*[\\\/])?\.[^\\\/]+\.sw[p-z]$/

More info here: https://github.com/guard/guard/issues/297

Rick Schmitty

unread,
Aug 30, 2012, 8:21:03 AM8/30/12
to guar...@googlegroups.com
Thanks Thibaud for your continued help. 

I'll have to try debugging some more, doesn't appear any swap files were created.  I tried adding those filters and it still ran twice.  Thinking eclipse might use different naming for temp/swap files I tried the watch(%r{^(.+)$}) { |m| `notify-send #{m[1]} modified` } mentioned in that thread.

The only notification I got was 2 for the file I was editing, for example I would get 2 notifications that app/views/static_pages/home.html.erb was modified.  I did not see any other notifications.

Should I reopen that ticket on github?  I seem to be in the same boat as japgolly

Thibaud Guillaume-Gentil

unread,
Aug 30, 2012, 9:23:23 AM8/30/12
to guar...@googlegroups.com
You're welcome, yeah I think reopening this issue is a good idea. Thanks!
Reply all
Reply to author
Forward
0 new messages