diff --git a/CHANGELOG.md b/CHANGELOG.md index 66a4a5e62..bf71dd86f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.8.7 (unreleased) - Fix regression with remote paths from chef-solo. [GH-431] + - Fix issue where Vagrant crashes if `.vagrant` file becomes invalid. [GH-496] ## 0.8.6 (August 28, 2011) diff --git a/lib/vagrant/data_store.rb b/lib/vagrant/data_store.rb index 400c070e8..e98b5796d 100644 --- a/lib/vagrant/data_store.rb +++ b/lib/vagrant/data_store.rb @@ -23,7 +23,12 @@ module Vagrant if File.exist?(file_path) File.open(file_path, "r") do |f| - merge!(JSON.parse(f.read)) + begin + merge!(JSON.parse(f.read)) + rescue JSON::ParserError + # Ignore if the data is invalid in the file. + # TODO: Log here. + end end end end diff --git a/test/unit/vagrant/data_store_test.rb b/test/unit/vagrant/data_store_test.rb index 51c06b82b..424244749 100644 --- a/test/unit/vagrant/data_store_test.rb +++ b/test/unit/vagrant/data_store_test.rb @@ -23,6 +23,14 @@ class DataStoreTest < Test::Unit::TestCase } end + should "initialize just fine if the db file contains invalid data" do + file = tmp_path.join("data_store_empty_test") + File.open(file, "w") { |f| f.write("") } + + instance = @klass.new(file) + assert instance.length == 0 + end + should "be a hash with indifferent access" do assert @instance.is_a?(Vagrant::Util::HashWithIndifferentAccess) end