Skip to content

Running on virtual machines

Robin Daugherty edited this page Nov 23, 2020 · 8 revisions

Editor links

If your app is running in a virtual machine (e.g. Vagrant, Docker, or Windows Subsystem for Linux), the path to your source files from the perspective of your running project will be different than the path seen by your editor.

Better Errors v2.9 and newer

As of v2.9, this can be solved by setting two environment variables:

  • BETTER_ERRORS_VIRTUAL_PATH is any prefix path within the virtual machine. For example, the project might be running in /app in the virtual machine.
  • BETTER_ERRORS_HOST_PATH is the path to the project root on the host machine. For example, this might be /Users/myname/Code/myproject.

Older versions

Before v2.9, Better Errors did not have a way to configure editor paths using environment variables.

However, for Vagrant, you can adjust the path used to generate open-in-editor links using the vagrant-host-path gem, and then hard-coding the editor. This are not recommended since it forces all developers to use the same editor, but it's better than nothing.

Install the vagrant-host-path gem, then add something like the following to your project's config/environments/development.rb, depending on your editor.

if defined?(BetterErrors)
  BetterErrors.editor = proc { |full_path, line|
    full_path = full_path.sub(Rails.root.to_s, ENV["VAGRANT_HOST_PATH"])
    "subl://open?url=file://#{full_path}&line=#{line}"
  }
end

For VS Code:

if defined?(BetterErrors)
  BetterErrors.editor = proc { |full_path, line|
    full_path = full_path.sub(Rails.root.to_s, ENV["VAGRANT_HOST_PATH"])
    "vscode://file#{full_path}:#{line}"
  }
end

Allowing access to your IP address

The following can be added to your config/environments/development.rb. When you connect to the virtual machine to run your project, it will allow the IP address that you are connecting from.

if defined?(BetterErrors) && ENV["SSH_CLIENT"]
  host = ENV["SSH_CLIENT"].match(/\A([^\s]*)/)[1]
  BetterErrors::Middleware.allow_ip! host if host
end