diff --git a/src/features/terminal/runInTerminal.ts b/src/features/terminal/runInTerminal.ts index b650b9e3..24ac7eb3 100644 --- a/src/features/terminal/runInTerminal.ts +++ b/src/features/terminal/runInTerminal.ts @@ -1,10 +1,10 @@ import { Terminal, TerminalShellExecution } from 'vscode'; import { PythonEnvironment, PythonTerminalExecutionOptions } from '../../api'; -import { onDidEndTerminalShellExecution } from '../../common/window.apis'; import { createDeferred } from '../../common/utils/deferred'; -import { quoteArgs } from '../execution/execUtils'; -import { identifyTerminalShell } from '../common/shellDetector'; +import { onDidEndTerminalShellExecution } from '../../common/window.apis'; import { ShellConstants } from '../common/shellConstants'; +import { identifyTerminalShell } from '../common/shellDetector'; +import { quoteArgs } from '../execution/execUtils'; export async function runInTerminal( environment: PythonEnvironment, @@ -15,11 +15,10 @@ export async function runInTerminal( terminal.show(); } - const executable = - environment.execInfo?.activatedRun?.executable ?? environment.execInfo?.run.executable ?? 'python'; + let executable = environment.execInfo?.activatedRun?.executable ?? environment.execInfo?.run.executable ?? 'python'; const args = environment.execInfo?.activatedRun?.args ?? environment.execInfo?.run.args ?? []; const allArgs = [...args, ...(options.args ?? [])]; - + const shellType = identifyTerminalShell(terminal); if (terminal.shellIntegration) { let execution: TerminalShellExecution | undefined; const deferred = createDeferred(); @@ -29,10 +28,21 @@ export async function runInTerminal( deferred.resolve(); } }); + + const shouldSurroundWithQuotes = + executable.includes(' ') && !executable.startsWith('"') && !executable.endsWith('"'); + // Handle case where executable contains white-spaces. + if (shouldSurroundWithQuotes) { + executable = `"${executable}"`; + } + + if (shellType === ShellConstants.PWSH && !executable.startsWith('&')) { + // PowerShell requires commands to be prefixed with '&' to run them. + executable = `& ${executable}`; + } execution = terminal.shellIntegration.executeCommand(executable, allArgs); await deferred.promise; } else { - const shellType = identifyTerminalShell(terminal); let text = quoteArgs([executable, ...allArgs]).join(' '); if (shellType === ShellConstants.PWSH && !text.startsWith('&')) { // PowerShell requires commands to be prefixed with '&' to run them.