Skip to content

PowerShell command format error after running first command #388

@ok3721

Description

@ok3721

Current Behavior

When the environments extension is enabled, whether in venv or not, running first python script is working, but all subsequent run commands are in wrong format, with no '&' at front, and ' ' in python executable path is not escaped, resulting in error.

# first run
PS C:\> & "C:\Users\Spaced Username\AppData\Local\Microsoft\WindowsApps\python3.11.exe" "c:/Users/Spaced Username/Documents/test.py"
# second run
PS C:\> C:\Users\Spaced Username\AppData\Local\Microsoft\WindowsApps\python3.11.exe "c:/Users/Spaced Username/Documents/test.py"
C:\Users\Spaced: The term 'C:\Users\Spaced' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Expected Behavior

# first run
PS C:\> & "C:\Users\Spaced Username\AppData\Local\Microsoft\WindowsApps\python3.11.exe" "c:/Users/Spaced Username/Documents/test.py"
# second run
PS C:\> & "C:\Users\Spaced Username\AppData\Local\Microsoft\WindowsApps\python3.11.exe" "c:/Users/Spaced Username/Documents/test.py"

Possible Solution

I believe src/features/terminal/runInTerminal.ts is involved

    if (terminal.shellIntegration) {
        let execution: TerminalShellExecution | undefined;
        const deferred = createDeferred<void>();
        const disposable = onDidEndTerminalShellExecution((e) => {
            if (e.execution === execution) {
                disposable.dispose();
                deferred.resolve();
            }
        });
        execution = terminal.shellIntegration.executeCommand(executable, allArgs); // also parse command text here?
        await deferred.promise;
    } else {
        const shellType = identifyTerminalShell(terminal);       
        let text = quoteArgs([executable, ...allArgs]).join(' ');      // correct behavior
        if (shellType === ShellConstants.PWSH && !text.startsWith('&')) {
            // PowerShell requires commands to be prefixed with '&' to run them.
            text = `& ${text}`;
        }
        terminal.sendText(`${text}\n`);
    }

Metadata

Metadata

Assignees

Labels

info-neededIssue requires more information from poster

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions