Guard --listen-on with vagrant

guard, rspec, ruby-on-rails, vagrant

Solution

UPDATE 2:

Listen 3.x no longer includes TCP functionality (see https://github.com/guard/listen/issues/258), so you'll need to lock to 2.x, e.g. in your `Gemfile`:

gem 'listen', '~> 2.9'

And then follow the instructions below:

UPDATE 1:

For `guard` >= v2.7.0 to work, you need `listen` >= v2.9.0 and the magic `-r` option (since full paths don't match on host and guest):

listen -r -f 10.11.12.1:4000 # on the host (note "-r" option)
guard -o 10.11.12.1:4000 # on the guest (paths relative, so no prob)

Notes:

since `guard` v2.7.0, `-w` is only for listening for multiple directories. Instead, run listen in the directory you're watching

I'd only consider Vargrant/ssh forwarding only if you're behind a firewall, or you can't use the given ports for some reason - instead, use the VM assigned IPs and network (e.g. 10.11.12.1)

for debugging things in `guard`, check out: https://github.com/guard/guard/wiki/Understanding-Guard (worth taking a look)

running a listen TCP server on 127.0.0.1 makes little sense (unless e.g. you must use a complex ssh port forwarding setup)

Problem

I am trying to use guard's `--listen-on` option with vagrant as outlined here, but I am unable to get it to work. If I add `config.vm.network :forwarded_port, guest: 4000, host: 4000` to my `Vagrantfile` and subsequently try to start listen with `listen -f 127.0.0.1:4000`, I get an error: `Broadcaster.initialize: Address already in use - bind(2) for "127.0.0.1" port 4000`. If I try to start listen and then start vagrant, vagrant complains similarly: Vagrant cannot forward the specified ports on this VM, since they would collide with some other application that is already listening on these ports. The forwarded port to 4000 is already in use on the host machine. So I tried some other things while omitting the port 4000 forwarding in the `Vagrantfile`: If I omit the port 4000 forwarding in my `Vagrantfile`, then I can successfully start listen with `listen -f 127.0.0.1:4000`. But then when I run `guard -o "10.0.2.2:4000" -w "/home/me/my_project/"` in my vagrant guest, guard does not do anything when a file changes. Adding a `-v` flag to the `listen` call reveals that changes are being picked up correctly on the host. I also tried `listen -f 10.11.12.1:4000` on the host combined with `guard -o "10.11.12.1:4000" -w "/home/me/my_project/"` on the guest with the same results of guard not do anything when a file changes. Combining `listen -f 127.0.0.1:4000` with `guard -o "10.11.12.1:4000" -w "/home/me/my_project/"` results in guard being unable to connect. I've also tried port forwarding with ssh: ``` listen -vf 127.0.0.1:4000 # host ssh -R 4000:localhost:4000 vagrant@10.11.12.13 # connect guard -o "127.0.0.1:4000" -w "/home/me/my_project" # guest ``` Everything seems to go okay with port forwarding, but again guard never does anything when files are changed. Both the host and guest are ubuntu 14.04. The network config in my `Vagrantfile` is as follows: ``` config.vm.network 'forwarded_port', guest: 80, host: 3000 config.vm.network 'private_network', ip: '10.11.12.13' ``` What is the proper way to make this work?

Original source