16

I have ubuntu virtual machine that I'm accessing through Vagrant. On my host (Mac OSX), there is a folder with couple of files, that I share to virtual machine. In that virtual machine, I want to use guard to watch for files changes and perform some action if any of those files changes.

I set up guard properly, and when changing shared file from within virtual machine, it works ok and runs appropriate scripts. But, if I try to change shared file from my host machine, this file change event doesn't propagate and guard doesn't react.

This is how my vagrant shared folder looks like (pretty usual stuff)

local_config.vm.share_folder "app", "/var/www/app/current", "../app"

I even tried using NFS sharing (:nfs => true) but it didn't help.

Is there any way to make file changes events propagate from host to virtual machine? Or is this something of Vagrant/VirtualBox nature?

UPDATE:

After some more trials, I installed ZenTest gem, which contains autotest tool allowing similar functionality regarding file change events.

When running autotest in virtual machine and changing files from my host machine, these changes are propagated and autotest reacts.

Based on this, it seems that the file change event propagation is an issue of guard, not vagrant or virtual box.

I haven't look into implementation differences between guard and autotest though.

Now I know it is possible to catch file change events from host in virtual machine. Does anyone have any idea on how to achieve this using guard? I like guard more because of its DSL and generic usability.

rdamborsky
  • 273
  • 2
  • 8

4 Answers4

11

The root cause is that VirtualBox does not cascade the file change events on the host to the VM. Guard (on Linux) is expecting to receive notifications via inotify events. Instead, you can have guard poll for events with guard -p, but this can lead to your CPU maxing out, so you can throttle the polling back down with guard -p -l 10.

From guard help start:

  -l, [--latency=Overwrite Listen's default latency]
  -p, [--force-polling=Force usage of the Listen polling listener]

http://www.softr.li/blog/2012/07/21/running-guard-over-vagrant

4

I know this is an older question, but here is a more up-to-date answer:

Guard -o/--listen-on option documentation

Pasted here for quick reference:

-o/--listen-on option

Use Listen's network functionality to receive file change events from the
network. This is most useful for virtual machines (e.g. Vagrant) which have
problems firing native filesystem events on the guest OS.

Suggested use:

On the host OS, you need to listen to filesystem events and forward them to
your VM using the listen script:

    $ listen -f 127.0.0.1:4000

Remember to configure your VM to forward the appropriate ports, e.g.
in Vagrantfile:

    config.vm.network :forwarded_port, guest: 4000, host: 4000

Then, on your guest OS, listen to the network events but ensure you
specify the host path:

    $ bundle exec guard -o '10.0.2.2:4000' -w '/projects/myproject'
Adam Eberlin
  • 765
  • 7
  • 11
1

If anyone runs into this issue and guard is still not working...

I ended up using watchr. It is an alternative to guard. Events propagation from host to guest machine works ok in watchr. It is also more flexible than autotest.

rdamborsky
  • 273
  • 2
  • 8
  • `Events propagation from host to guest machine works ok in watchr.` How? Does it use polling? If VirtualBox is not propagating the events, how can it know when a file has changed? – Nateowami Jun 05 '18 at 15:42
0

If you have this problem with hugo in Vagrant, you can start the server with a poll option:

hugo server --poll 2s

Docs:

--poll string            set this to a poll interval, e.g --poll 700ms, to use a 
                         poll based approach to watch for file system changes
stackprotector
  • 445
  • 1
  • 3
  • 20