diff --git a/lib/vagrant/util/platform.rb b/lib/vagrant/util/platform.rb index 944ca42ea..0c388ecb2 100644 --- a/lib/vagrant/util/platform.rb +++ b/lib/vagrant/util/platform.rb @@ -341,6 +341,25 @@ module Vagrant wsl? && !path.to_s.downcase.start_with?("/mnt/") end + # This takes any path and converts it from a Windows path to a + # WSL style path. + # + # @param [String path] Windows style path + # @return [String] WSL style path + def windows_to_wsl_path(path) + if wsl? + cmd = ["wslpath", "-u", "-a", path.to_s] + else + cmd = ["wsl", "--", "/bin/wslpath", "-u", "-a", path.gsub("\\", "/")] + end + + begin + process = Subprocess.execute(*cmd) + return process.stdout.chomp + rescue Errors::CommandUnavailableWindows + end + end + # Compute the path to rootfs of currently active WSL. # # @return [String] A path to rootfs of a current WSL instance. @@ -473,11 +492,7 @@ module Vagrant def format_windows_path(path, *args) path = cygwin_path(path) if cygwin? path = msys_path(path) if msys? - path = wsl_to_windows_path(path) if wsl? - if windows? || wsl? - path = windows_unc_path(path) if !args.include?(:disable_unc) - end - + path = windows_to_wsl_path(path) if wsl? path end diff --git a/test/unit/vagrant/util/platform_test.rb b/test/unit/vagrant/util/platform_test.rb index 7149a66e3..48e24b25e 100644 --- a/test/unit/vagrant/util/platform_test.rb +++ b/test/unit/vagrant/util/platform_test.rb @@ -534,4 +534,28 @@ EOF end end end + + describe "#windows_to_wsl_path" do + let(:path) { 'c:\my_test_path' } + let(:wsl_path) { "/mnt/c/expected_path" } + let(:process_result) { double("process", stdout: wsl_path)} + + before do + allow(Vagrant::Util::Subprocess).to receive(:execute).and_return(process_result) + end + + after { expect(subject.windows_to_wsl_path(path)).to eq(wsl_path) } + + it "invokes wslpath directly" do + allow(subject).to receive(:wsl?).and_return(true) + expect(Vagrant::Util::Subprocess).to receive(:execute).with("wslpath", "-u", "-a", path) + end + + it "invokes wslpath through wsl.exe" do + allow(subject).to receive(:wsl?).and_return(false) + expect(Vagrant::Util::Subprocess).to receive(:execute). + with("wsl", "--", "/bin/wslpath", "-u", "-a", "c:/my_test_path") + end + end + end