diff --git a/lib/vagrant/action/builtin/mixin_synced_folders.rb b/lib/vagrant/action/builtin/mixin_synced_folders.rb index 67692d61a..11948cf50 100644 --- a/lib/vagrant/action/builtin/mixin_synced_folders.rb +++ b/lib/vagrant/action/builtin/mixin_synced_folders.rb @@ -62,7 +62,21 @@ module Vagrant # # @param [Machine] machine The machine that the folders belong to # @param [Hash] folders The result from a {#synced_folders} call. - def save_synced_folders(machine, folders) + def save_synced_folders(machine, folders, **opts) + if opts[:merge] + existing = cached_synced_folders(machine) + if existing + folders.each do |impl, fs| + existing[impl] ||= {} + fs.each do |id, data| + existing[impl][id] = data + end + end + + folders = existing + end + end + machine.data_dir.join("synced_folders").open("w") do |f| f.write(JSON.dump(folders)) end diff --git a/lib/vagrant/action/builtin/synced_folders.rb b/lib/vagrant/action/builtin/synced_folders.rb index db2077ad0..2980a3c25 100644 --- a/lib/vagrant/action/builtin/synced_folders.rb +++ b/lib/vagrant/action/builtin/synced_folders.rb @@ -18,7 +18,12 @@ module Vagrant end def call(env) - folders = synced_folders(env[:machine], env[:synced_folders_config]) + opts = { + cached: !!env[:synced_folders_cached], + } + + folders = synced_folders( + env[:machine], env[:synced_folders_config], **opts) folders.each do |impl_name, fs| @logger.info("Synced Folder Implementation: #{impl_name}") @@ -82,6 +87,9 @@ module Vagrant @logger.info("Invoking synced folder enable: #{impl_name}") impl.enable(env[:machine], fs, impl_opts(impl_name, env)) end + + # Save the synced folders + save_synced_folders(env[:machine], folders, merge: true) end end end diff --git a/test/unit/vagrant/action/builtin/mixin_synced_folders_test.rb b/test/unit/vagrant/action/builtin/mixin_synced_folders_test.rb index 802d6c01f..0122ccc97 100644 --- a/test/unit/vagrant/action/builtin/mixin_synced_folders_test.rb +++ b/test/unit/vagrant/action/builtin/mixin_synced_folders_test.rb @@ -158,5 +158,32 @@ describe Vagrant::Action::Builtin::MixinSyncedFolders do }) expect(result[:nfs]).to eq({ "nfs" => old_folders["nfs"] }) end + + it "should be able to save and retrieve cached versions" do + folders["foo"] = { type: "default" } + result = subject.synced_folders(machine) + subject.save_synced_folders(machine, result) + + # Clear the folders and set some more + folders.clear + folders["bar"] = { type: "default" } + folders["baz"] = { type: "nfs" } + result = subject.synced_folders(machine) + subject.save_synced_folders(machine, result, merge: true) + + # Clear one last time + folders.clear + + # Read them all back + result = subject.synced_folders(machine, nil, cached: true) + expect(result.length).to eq(2) + expect(result[:default]).to eq({ + "foo" => { type: "default" }, + "bar" => { type: "default" }, + }) + expect(result[:nfs]).to eq({ + "baz" => { type: "nfs" } + }) + end end end diff --git a/test/unit/vagrant/action/builtin/synced_folders_test.rb b/test/unit/vagrant/action/builtin/synced_folders_test.rb index 263c642f3..d7969474c 100644 --- a/test/unit/vagrant/action/builtin/synced_folders_test.rb +++ b/test/unit/vagrant/action/builtin/synced_folders_test.rb @@ -44,6 +44,7 @@ describe Vagrant::Action::Builtin::SyncedFolders do env[:root_path] = Pathname.new(Dir.mktmpdir) subject.stub(:plugins => plugins) subject.stub(:synced_folders => synced_folders) + allow(subject).to receive(:save_synced_folders) end it "should create on the host if specified" do @@ -189,7 +190,7 @@ describe Vagrant::Action::Builtin::SyncedFolders do env[:synced_folders_config] = new_config expect(subject).to receive(:synced_folders). - with(machine, new_config). + with(machine, new_config, cached: false). and_return(synced_folders) subject.call(env)