From 85f1e05e2a9bf7a7940bb4498472b809ba43e01f Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 18 Nov 2015 15:48:59 -0800 Subject: [PATCH] core: prune machine if non-existent CWD from index [GH-4742] --- lib/vagrant/machine_index.rb | 2 +- lib/vagrant/plugin/v2/command.rb | 13 +++++++-- test/unit/vagrant/plugin/v2/command_test.rb | 29 +++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/lib/vagrant/machine_index.rb b/lib/vagrant/machine_index.rb index 6e8fe0e3a..1af574b33 100644 --- a/lib/vagrant/machine_index.rb +++ b/lib/vagrant/machine_index.rb @@ -78,7 +78,7 @@ module Vagrant @machines.delete(entry.id) unlocked_save - # Release acccess on this machine + # Release access on this machine unlocked_release(entry.id) end end diff --git a/lib/vagrant/plugin/v2/command.rb b/lib/vagrant/plugin/v2/command.rb index eeb580a29..f0b6c4415 100644 --- a/lib/vagrant/plugin/v2/command.rb +++ b/lib/vagrant/plugin/v2/command.rb @@ -133,8 +133,17 @@ module Vagrant # machine in that environment. We silence warnings here because # Vagrantfiles often have constants, so people would otherwise # constantly (heh) get "already initialized constant" warnings. - env = entry.vagrant_env( - @env.home_path, ui_class: @env.ui_class) + begin + env = entry.vagrant_env( + @env.home_path, ui_class: @env.ui_class) + rescue Vagrant::Errors::EnvironmentNonExistentCWD + # This means that this environment working directory + # no longer exists, so delete this entry. + entry = @env.machine_index.get(name.to_s) + @env.machine_index.delete(entry) if entry + raise + end + next env.machine(entry.name.to_sym, entry.provider.to_sym) end diff --git a/test/unit/vagrant/plugin/v2/command_test.rb b/test/unit/vagrant/plugin/v2/command_test.rb index 7b42f2345..fc6cc5271 100644 --- a/test/unit/vagrant/plugin/v2/command_test.rb +++ b/test/unit/vagrant/plugin/v2/command_test.rb @@ -263,6 +263,35 @@ describe Vagrant::Plugin::V2::Command do expect(results.length).to eq(1) expect(results[0].id).to eq(other_machine.id) end + + it "should yield machines from another environment" do + iso_env = isolated_environment + iso_env.vagrantfile("") + other_env = iso_env.create_vagrant_env( + home_path: environment.home_path) + other_machine = other_env.machine( + other_env.machine_names[0], other_env.default_provider) + + # Set an ID on it so that it is "created" in the index + other_machine.id = "foo" + + # Grab the uuid so we know what it is + index_uuid = other_machine.index_uuid + + # Remove the working directory + FileUtils.rm_rf(iso_env.workdir) + + # Make sure we don't have a root path, to test + environment.stub(root_path: nil) + + # Run the command + expect { + subject.with_target_vms(index_uuid) { |*args| } + }.to raise_error(Vagrant::Errors::EnvironmentNonExistentCWD) + + # Verify that it no longer exists in the index + expect(other_env.machine_index.get(index_uuid)).to be_nil + end end describe "splitting the main and subcommand args" do