diff --git a/lib/vagrant/config/loader.rb b/lib/vagrant/config/loader.rb index 2bf011231..9a3bc3c67 100644 --- a/lib/vagrant/config/loader.rb +++ b/lib/vagrant/config/loader.rb @@ -101,6 +101,18 @@ module Vagrant warnings = [] errors = [] + if !@sources[:root].nil? && @sources[:root].eql?(@sources[:home]) + # Vagrants home dir is set to the same dir as its project directory + # so we don't want to load and merge the same Vagrantfile config + # and execute its settings/procs twice + # + # Note: This protection won't work if there are two separate but + # identical Vagrantfiles in the home and project dir + @logger.info("Duplicate Vagrantfile config objects detected in :root and :home.") + @sources.delete(:home) + @logger.info("Removed :home config from being loaded") + end + order.each do |key| next if !@sources.key?(key) diff --git a/test/unit/vagrant/config/loader_test.rb b/test/unit/vagrant/config/loader_test.rb index 4073b85db..1f2d7e2f7 100644 --- a/test/unit/vagrant/config/loader_test.rb +++ b/test/unit/vagrant/config/loader_test.rb @@ -23,7 +23,7 @@ describe Vagrant::Config::Loader do end def self.merge(old, new) - old.merge(new) + old.merge(new) {|key, oldval, newval| oldval.concat(newval)} end end end @@ -177,6 +177,25 @@ describe Vagrant::Config::Loader do end end + it "should discard duplicate configs if :home and :root are the same" do + proc = Proc.new do |config| + config[:foo] = ["yep"] + end + + order = [:root, :home] + + instance.set(:root, [[current_version, proc]]) + instance.set(:home, [[current_version, proc]]) + + result, warnings, errors = instance.load(order) + + # Verify the config result + expect(result[:foo]).to eq(["yep"]) + expect(result[:foo].size).to eq(1) + expect(warnings).to eq([]) + expect(errors).to eq([]) + end + it "should only load configuration files once" do $_config_data = 0