From bf0486e659b993a13d1a37888578d29d68e9e09c Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Fri, 22 Jul 2016 14:08:18 +0100 Subject: [PATCH 01/10] Vagrantfile-relative VAGRANT_DOTFILE_PATH --- lib/vagrant/environment.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index e98c8f397..a602e133b 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -157,9 +157,13 @@ module Vagrant end # Setup the local data directory. If a configuration path is given, - # then it is expanded relative to the working directory. Otherwise, + # it is expanded relative to the root path or to the working directory + # depending on whether it starts with the "{}" placeholder. Otherwise, # we use the default which is expanded relative to the root path. opts[:local_data_path] ||= ENV["VAGRANT_DOTFILE_PATH"] if !opts[:child] + if opts[:local_data_path] && opts[:local_data_path].start_with?("{}") && !root_path.nil? + opts[:local_data_path] = File.join(root_path, opts[:local_data_path].sub(/^\{\}/, '')) + end opts[:local_data_path] ||= root_path.join(DEFAULT_LOCAL_DATA) if !root_path.nil? if opts[:local_data_path] @local_data_path = Pathname.new(File.expand_path(opts[:local_data_path], @cwd)) From c95267fdda896b14b023c24c5b73f59828f148d6 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Fri, 22 Jul 2016 14:09:55 +0100 Subject: [PATCH 02/10] Update VAGRANT_DOTFILE_PATH documentation --- website/source/docs/other/environmental-variables.html.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/source/docs/other/environmental-variables.html.md b/website/source/docs/other/environmental-variables.html.md index 0f827267d..2ef093288 100644 --- a/website/source/docs/other/environmental-variables.html.md +++ b/website/source/docs/other/environmental-variables.html.md @@ -68,6 +68,8 @@ a scripting environment in order to set the directory that Vagrant sees. `VAGRANT_DOTFILE_PATH` can be set to change the directory where Vagrant stores VM-specific state, such as the VirtualBox VM UUID. By default, this is set to `.vagrant`. If you keep your Vagrantfile in a Dropbox folder in order to share the folder between your desktop and laptop (for example), Vagrant will overwrite the files in this directory with the details of the VM on the most recently-used host. To avoid this, you could set `VAGRANT_DOTFILE_PATH` to `.vagrant-laptop` and `.vagrant-desktop` on the respective machines. (Remember to update your `.gitignore`!) +Non-absolute paths in this environmental variable are interpreted as relative to the current working directory unless prefixed with `{}` (example: `{}/.vagrant-laptop`), in which case they're interpreted as relative to the Vagrantfile. + ## `VAGRANT_HOME` `VAGRANT_HOME` can be set to change the directory where Vagrant stores From 92104fec729aab897ade2b4b21a4c7311876f28c Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Fri, 22 Jul 2016 14:39:44 +0100 Subject: [PATCH 03/10] Use match instead of start_with --- lib/vagrant/environment.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index a602e133b..daf4e6013 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -161,7 +161,7 @@ module Vagrant # depending on whether it starts with the "{}" placeholder. Otherwise, # we use the default which is expanded relative to the root path. opts[:local_data_path] ||= ENV["VAGRANT_DOTFILE_PATH"] if !opts[:child] - if opts[:local_data_path] && opts[:local_data_path].start_with?("{}") && !root_path.nil? + if opts[:local_data_path] && opts[:local_data_path].match("^\{\}") && !root_path.nil? opts[:local_data_path] = File.join(root_path, opts[:local_data_path].sub(/^\{\}/, '')) end opts[:local_data_path] ||= root_path.join(DEFAULT_LOCAL_DATA) if !root_path.nil? From 9ebc028745d672f77d281e86d674dab4c4ae4e2b Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Fri, 22 Jul 2016 14:59:05 +0100 Subject: [PATCH 04/10] Better checks, hopefully --- lib/vagrant/environment.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index daf4e6013..c0865eb5f 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -160,9 +160,13 @@ module Vagrant # it is expanded relative to the root path or to the working directory # depending on whether it starts with the "{}" placeholder. Otherwise, # we use the default which is expanded relative to the root path. - opts[:local_data_path] ||= ENV["VAGRANT_DOTFILE_PATH"] if !opts[:child] - if opts[:local_data_path] && opts[:local_data_path].match("^\{\}") && !root_path.nil? - opts[:local_data_path] = File.join(root_path, opts[:local_data_path].sub(/^\{\}/, '')) + if ENV.has_key?("VAGRANT_DOTFILE_PATH") && !opts[:child] + dotfile_path = ENV["VAGRANT_DOTFILE_PATH"] + if dotfile_path.match(/^\{\}/) && !root_path.nil? + opts[:local_data_path] = File.join(root_path, dotfile_path.sub(/^\{\}/, '')) + else + opts[:local_data_path] = dotfile_path + end end opts[:local_data_path] ||= root_path.join(DEFAULT_LOCAL_DATA) if !root_path.nil? if opts[:local_data_path] From 188a7dab0c904d04f6ae4390f0367922135b01e2 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Sun, 24 Jul 2016 18:40:09 +0100 Subject: [PATCH 05/10] Another try, without string substitution --- lib/vagrant/environment.rb | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index c0865eb5f..1af6d96d0 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -157,21 +157,16 @@ module Vagrant end # Setup the local data directory. If a configuration path is given, - # it is expanded relative to the root path or to the working directory - # depending on whether it starts with the "{}" placeholder. Otherwise, - # we use the default which is expanded relative to the root path. + # it is expanded relative to the root path. Otherwise, we use the + # default (which is also expanded relative to the root path). if ENV.has_key?("VAGRANT_DOTFILE_PATH") && !opts[:child] - dotfile_path = ENV["VAGRANT_DOTFILE_PATH"] - if dotfile_path.match(/^\{\}/) && !root_path.nil? - opts[:local_data_path] = File.join(root_path, dotfile_path.sub(/^\{\}/, '')) - else - opts[:local_data_path] = dotfile_path - end + opts[:local_data_path] ||= root_path.join(ENV["VAGRANT_DOTFILE_PATH"]) if !root_path.nil? end opts[:local_data_path] ||= root_path.join(DEFAULT_LOCAL_DATA) if !root_path.nil? if opts[:local_data_path] @local_data_path = Pathname.new(File.expand_path(opts[:local_data_path], @cwd)) end + @logger.debug("Effective local data path: #{@local_data_path}") # If we have a root path, load the ".vagrantplugins" file. if root_path From 6b49d505d4d578ab6de39a94fbdf997f9cd43a15 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Sun, 24 Jul 2016 18:41:04 +0100 Subject: [PATCH 06/10] Update the docs to match previous change --- website/source/docs/other/environmental-variables.html.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/source/docs/other/environmental-variables.html.md b/website/source/docs/other/environmental-variables.html.md index 2ef093288..fe21e6451 100644 --- a/website/source/docs/other/environmental-variables.html.md +++ b/website/source/docs/other/environmental-variables.html.md @@ -68,7 +68,7 @@ a scripting environment in order to set the directory that Vagrant sees. `VAGRANT_DOTFILE_PATH` can be set to change the directory where Vagrant stores VM-specific state, such as the VirtualBox VM UUID. By default, this is set to `.vagrant`. If you keep your Vagrantfile in a Dropbox folder in order to share the folder between your desktop and laptop (for example), Vagrant will overwrite the files in this directory with the details of the VM on the most recently-used host. To avoid this, you could set `VAGRANT_DOTFILE_PATH` to `.vagrant-laptop` and `.vagrant-desktop` on the respective machines. (Remember to update your `.gitignore`!) -Non-absolute paths in this environmental variable are interpreted as relative to the current working directory unless prefixed with `{}` (example: `{}/.vagrant-laptop`), in which case they're interpreted as relative to the Vagrantfile. +Non-absolute paths in this environmental variable are interpreted as relative to the Vagrantfile's location. ## `VAGRANT_HOME` From 218b697d5e1b1607a3cf88f097d94e882d2706a2 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Sun, 24 Jul 2016 19:01:54 +0100 Subject: [PATCH 07/10] This looks cleaner --- lib/vagrant/environment.rb | 9 ++++++--- .../source/docs/other/environmental-variables.html.md | 2 -- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/vagrant/environment.rb b/lib/vagrant/environment.rb index 1af6d96d0..5f936c0fe 100644 --- a/lib/vagrant/environment.rb +++ b/lib/vagrant/environment.rb @@ -159,10 +159,13 @@ module Vagrant # Setup the local data directory. If a configuration path is given, # it is expanded relative to the root path. Otherwise, we use the # default (which is also expanded relative to the root path). - if ENV.has_key?("VAGRANT_DOTFILE_PATH") && !opts[:child] - opts[:local_data_path] ||= root_path.join(ENV["VAGRANT_DOTFILE_PATH"]) if !root_path.nil? + if !root_path.nil? + if !(ENV["VAGRANT_DOTFILE_PATH"] or "").empty? && !opts[:child] + opts[:local_data_path] ||= root_path.join(ENV["VAGRANT_DOTFILE_PATH"]) + else + opts[:local_data_path] ||= root_path.join(DEFAULT_LOCAL_DATA) + end end - opts[:local_data_path] ||= root_path.join(DEFAULT_LOCAL_DATA) if !root_path.nil? if opts[:local_data_path] @local_data_path = Pathname.new(File.expand_path(opts[:local_data_path], @cwd)) end diff --git a/website/source/docs/other/environmental-variables.html.md b/website/source/docs/other/environmental-variables.html.md index fe21e6451..0f827267d 100644 --- a/website/source/docs/other/environmental-variables.html.md +++ b/website/source/docs/other/environmental-variables.html.md @@ -68,8 +68,6 @@ a scripting environment in order to set the directory that Vagrant sees. `VAGRANT_DOTFILE_PATH` can be set to change the directory where Vagrant stores VM-specific state, such as the VirtualBox VM UUID. By default, this is set to `.vagrant`. If you keep your Vagrantfile in a Dropbox folder in order to share the folder between your desktop and laptop (for example), Vagrant will overwrite the files in this directory with the details of the VM on the most recently-used host. To avoid this, you could set `VAGRANT_DOTFILE_PATH` to `.vagrant-laptop` and `.vagrant-desktop` on the respective machines. (Remember to update your `.gitignore`!) -Non-absolute paths in this environmental variable are interpreted as relative to the Vagrantfile's location. - ## `VAGRANT_HOME` `VAGRANT_HOME` can be set to change the directory where Vagrant stores From 211523d052f1a399a912e35ea5d1e4a3ee13ff98 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Thu, 28 Jul 2016 01:01:29 +0100 Subject: [PATCH 08/10] Added tests for VAGRANT_DOTFILE_PATH --- test/unit/vagrant/environment_test.rb | 68 +++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index ba3466c9b..afcaccaf2 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -921,6 +921,74 @@ VF end end + context "with environmental variable VAGRANT_DOTFILE_PATH set to the empty string" do + it "is set to the default, from the work directory" do + with_temp_env("VAGRANT_DOTFILE_PATH" => "") do + instance = env.create_vagrant_env + expect(instance.cwd).to eq(env.workdir) + expect(instance.local_data_path.to_s).to eq(File.join(env.workdir, ".vagrant")) + end + end + + it "is set to the default, from a sub-directory of the work directory" do + Dir.mktmpdir("sub-directory", env.workdir) do |temp_dir| + with_temp_env("VAGRANT_DOTFILE_PATH" => "") do + instance = env.create_vagrant_env(cwd: temp_dir) + expect(instance.cwd.to_s).to eq(temp_dir) + expect(instance.local_data_path.to_s).to eq(File.join(env.workdir, ".vagrant")) + end + end + end + end + + context "with environmental variable VAGRANT_DOTFILE_PATH set to an absolute path" do + it "is set to VAGRANT_DOTFILE_PATH from the work directory" do + Dir.mktmpdir("sub-directory", env.workdir) do |temp_dir| + dotfile_path = File.join(temp_dir, ".vagrant-custom") + + with_temp_env("VAGRANT_DOTFILE_PATH" => dotfile_path) do + instance = env.create_vagrant_env + expect(instance.cwd).to eq(env.workdir) + expect(instance.local_data_path.to_s).to eq(dotfile_path) + end + end + end + + it "is set to VAGRANT_DOTFILE_PATH from a sub-directory of the work directory" do + Dir.mktmpdir("sub-directory", env.workdir) do |temp_dir| + dotfile_path = File.join(temp_dir, ".vagrant-custom") + + with_temp_env("VAGRANT_DOTFILE_PATH" => dotfile_path) do + instance = env.create_vagrant_env(cwd: temp_dir) + expect(instance.cwd.to_s).to eq(temp_dir) + expect(instance.local_data_path.to_s).to eq(dotfile_path) + end + end + end + end + + context "with environmental variable VAGRANT_DOTFILE_PATH set to a relative path" do + it "is set relative to the the work directory, from the work directory" do + Dir.mktmpdir("sub-directory", env.workdir) do |temp_dir| + with_temp_env("VAGRANT_DOTFILE_PATH" => ".vagrant-custom") do + instance = env.create_vagrant_env + expect(instance.cwd).to eq(env.workdir) + expect(instance.local_data_path.to_s).to eq(File.join(env.workdir, ".vagrant-custom")) + end + end + end + + it "is set relative to the the work directory, from a sub-directory of the work directory" do + Dir.mktmpdir("sub-directory", env.workdir) do |temp_dir| + with_temp_env("VAGRANT_DOTFILE_PATH" => ".vagrant-custom") do + instance = env.create_vagrant_env(cwd: temp_dir) + expect(instance.cwd.to_s).to eq(temp_dir) + expect(instance.local_data_path.to_s).to eq(File.join(env.workdir, ".vagrant-custom")) + end + end + end + end + describe "upgrading V1 dotfiles" do let(:v1_dotfile_tempfile) do Tempfile.new("vagrant-upgrade-dotfile").tap do |f| From 1b0a28bd4a50cbbcf56b89b348df1ff56d4169da Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Thu, 28 Jul 2016 01:09:14 +0100 Subject: [PATCH 09/10] One extra smoke test --- test/unit/vagrant/environment_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index afcaccaf2..29bdbbb5e 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -987,6 +987,16 @@ VF end end end + + it "is set relative to the empty string when there is no valid work directory" do + Dir.mktmpdir("out-of-tree-directory") do |temp_dir| + with_temp_env("VAGRANT_DOTFILE_PATH" => ".vagrant-custom") do + instance = env.create_vagrant_env(cwd: temp_dir) + expect(instance.cwd.to_s).to eq(temp_dir) + expect(instance.local_data_path.to_s).to eq("") + end + end + end end describe "upgrading V1 dotfiles" do From 22a22e8bb6101492979bb907cc4f9c09ee7db0b8 Mon Sep 17 00:00:00 2001 From: Carlos Rodrigues Date: Thu, 28 Jul 2016 01:18:48 +0100 Subject: [PATCH 10/10] Missed a spot --- test/unit/vagrant/environment_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/vagrant/environment_test.rb b/test/unit/vagrant/environment_test.rb index 29bdbbb5e..f2c9cf9bc 100644 --- a/test/unit/vagrant/environment_test.rb +++ b/test/unit/vagrant/environment_test.rb @@ -988,7 +988,7 @@ VF end end - it "is set relative to the empty string when there is no valid work directory" do + it "is set to the empty string when there is no valid work directory" do Dir.mktmpdir("out-of-tree-directory") do |temp_dir| with_temp_env("VAGRANT_DOTFILE_PATH" => ".vagrant-custom") do instance = env.create_vagrant_env(cwd: temp_dir)