Skip to content

ctx.run inside ctx.cd fails when path contains spaces on Windows #1001

Open

Description

@clarkb7

example

@task
def repro(ctx):
    import tempfile
    d = tempfile.mkdtemp(prefix="with space")
    with ctx.cd(d):
        ctx.run("whoami")

output

The system cannot find the path specified.

debug output

The system cannot find the path specified.
invoke.program.run: Received a possibly-skippable exception: <UnexpectedExit: cmd='cd C:\\Users\\BRANDE~1.CLA\\AppData\\Local\\Temp\\with\\ spacekk8c0niv && whoami' exited=1>

it looks like ctx.cwd improperly escapes the whitespace for Windows by adding a backslash. If using the cd command this way the path should be enclosed in double quotes. Why do you use the cd command instead of the cwd parameter to subprocess.Popen?

Activity

  1. jamesmyatt commented on Oct 25, 2024

    @jamesmyatt

    The escaping of the spaces in cd is definitely wrong for Windows. Needs to be enclosed in " or escaped with ^ or ``` depending on whether it's using CMD or Powershell. e.g. https://www.howtogeek.com/694949/how-to-escape-spaces-in-file-paths-on-the-windows-command-line/.

  2. coilysiren commented on Oct 30, 2024

    @coilysiren

    :/ I'm new to working on windows - is there a workaround for this?

  3. jamesmyatt commented on Nov 6, 2024

    @jamesmyatt

    @coilysiren , I just did it directly `ctx.run(f'cd "{wd}" && {cmd}')

  4. jamesmyatt commented on Nov 6, 2024

    @jamesmyatt

    @bitprophet , @jefftriplett , I think that using mslex: https://mslex.readthedocs.io/en/latest/ and shlex: https://docs.python.org/3/library/shlex.html (possibly via oslex: https://github.com/petamas/oslex) to do the quoting would be the most robust way to do this quoting correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions