diff --git a/lib/vagrant/config/loader.rb b/lib/vagrant/config/loader.rb index 9a3bc3c67..64d4f404a 100644 --- a/lib/vagrant/config/loader.rb +++ b/lib/vagrant/config/loader.rb @@ -122,7 +122,27 @@ module Vagrant # Get the proper version loader for this version and load version_loader = @versions.get(version) - version_config = version_loader.load(proc) + begin + version_config = version_loader.load(proc) + rescue NameError => e + line = "(unknown)" + path = "(unknown)" + if e.backtrace && e.backtrace[0] + backtrace_tokens = e.backtrace[0].split(":") + path = backtrace_tokens[0] + backtrace_tokens.each do |part| + if part =~ /\d+/ + line = part.to_i + break + end + end + end + + raise Errors::VagrantfileNameError, + path: path, + line: line, + message: e.message.sub(/' for .*$/, "'") + end # Store the errors/warnings associated with loading this # configuration. We'll store these for later. diff --git a/lib/vagrant/errors.rb b/lib/vagrant/errors.rb index ee0d8ea25..d93493de9 100644 --- a/lib/vagrant/errors.rb +++ b/lib/vagrant/errors.rb @@ -780,6 +780,10 @@ module Vagrant error_key(:vagrantfile_load_error) end + class VagrantfileNameError < VagrantError + error_key(:vagrantfile_name_error) + end + class VagrantfileSyntaxError < VagrantError error_key(:vagrantfile_syntax_error) end diff --git a/templates/locales/en.yml b/templates/locales/en.yml index 0e673586a..55f3cfb61 100644 --- a/templates/locales/en.yml +++ b/templates/locales/en.yml @@ -1374,6 +1374,14 @@ en: Path: %{path} Line number: %{line} Message: %{exception_class}: %{message} + vagrantfile_name_error: |- + There was an error loading a Vagrantfile. The file being loaded + and the error message are shown below. This is usually caused by + an invalid or undefined variable. + + Path: %{path} + Line number: %{line} + Message: %{message} vagrantfile_syntax_error: |- There is a syntax error in the following Vagrantfile. The syntax error message is reproduced below for convenience: diff --git a/test/unit/vagrant/config/loader_test.rb b/test/unit/vagrant/config/loader_test.rb index 1f2d7e2f7..c5503dd96 100644 --- a/test/unit/vagrant/config/loader_test.rb +++ b/test/unit/vagrant/config/loader_test.rb @@ -87,6 +87,20 @@ describe Vagrant::Config::Loader do expect(warnings).to eq([]) expect(errors).to eq([]) end + + it "should throw a NameError exception if invalid or undefined variable is used" do + vagrantfile = <<-VF + Vagrant.configure("2") do |config| + config.ssh.port = variable + end + VF + + instance.set(:foo, temporary_file(vagrantfile)) + + expect { + instance.load([:foo]) + }.to raise_error(Vagrant::Errors::VagrantfileNameError, /invalid or undefined variable/) + end end describe "finalization" do