Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix changing directories between drives on Windows #883

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion invoke/context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import platform
import re
from contextlib import contextmanager
from itertools import cycle
Expand Down Expand Up @@ -248,7 +249,16 @@ def _prefix_commands(self, command):
prefixes = list(self.command_prefixes)
current_directory = self.cwd
if current_directory:
prefixes.insert(0, "cd {}".format(current_directory))
cd_command = "cd"
if platform.system() == "Windows":
# On Windows when switching between different drives like C:\
# or E:\ the /d flag must be specified.
# To avoid the command failing when a drive must be changed we
# always add the flag. It would be too convoluted and
# complicated to add it only in case the drive changes, also
# the flag has no ill side effects.
cd_command += " /d"
prefixes.insert(0, "{} {}".format(cd_command, current_directory))

return " && ".join(prefixes + [command])

Expand Down
15 changes: 15 additions & 0 deletions tests/context.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import pickle
import platform
import re
import sys

Expand Down Expand Up @@ -225,6 +226,20 @@ def __str__(self):
cmd = "cd foo && whoami"
assert runner.run.call_args[0][0] == cmd

@patch(local_path)
def cd_on_windows_must_append_flag(self, Local):
if platform.system() != "Windows":
skip()

runner = Local.return_value
c = Context()
with c.cd("foo"):
c.run("whoami")

cmd = "cd /d foo && whoami"
assert runner.run.called, "run() never called runner.run()!"
assert runner.run.call_args[0][0] == cmd

class prefix:
def setup(self):
self.escaped_prompt = re.escape(Config().sudo.prompt)
Expand Down