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

Open
#1,001 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
python

Research direction

Start at invoke/context.py around ctx.cwd, then run the provided task reproduction on Windows with a temporary directory whose path contains spaces. Trace how the cwd is turned into the shell command and verify that ctx.run("whoami") succeeds inside ctx.cd(d) without the path error.

Written by the indexing model from the issue text.

Description

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?

Dominant language
Python
Stars
4.8k
Forks
412
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pyinvoke/invoke

All issues in pyinvoke/invoke

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.