11import { Terminal , TerminalShellExecution } from 'vscode' ;
22import { PythonEnvironment , PythonTerminalExecutionOptions } from '../../api' ;
3- import { onDidEndTerminalShellExecution } from '../../common/window.apis' ;
43import { createDeferred } from '../../common/utils/deferred' ;
5- import { quoteArgs } from '../execution/execUtils' ;
6- import { identifyTerminalShell } from '../common/shellDetector' ;
4+ import { onDidEndTerminalShellExecution } from '../../common/window.apis' ;
75import { ShellConstants } from '../common/shellConstants' ;
6+ import { identifyTerminalShell } from '../common/shellDetector' ;
7+ import { quoteArgs } from '../execution/execUtils' ;
88
99export async function runInTerminal (
1010 environment : PythonEnvironment ,
@@ -15,11 +15,10 @@ export async function runInTerminal(
1515 terminal . show ( ) ;
1616 }
1717
18- const executable =
19- environment . execInfo ?. activatedRun ?. executable ?? environment . execInfo ?. run . executable ?? 'python' ;
18+ let executable = environment . execInfo ?. activatedRun ?. executable ?? environment . execInfo ?. run . executable ?? 'python' ;
2019 const args = environment . execInfo ?. activatedRun ?. args ?? environment . execInfo ?. run . args ?? [ ] ;
2120 const allArgs = [ ...args , ...( options . args ?? [ ] ) ] ;
22-
21+ const shellType = identifyTerminalShell ( terminal ) ;
2322 if ( terminal . shellIntegration ) {
2423 let execution : TerminalShellExecution | undefined ;
2524 const deferred = createDeferred < void > ( ) ;
@@ -29,10 +28,21 @@ export async function runInTerminal(
2928 deferred . resolve ( ) ;
3029 }
3130 } ) ;
31+
32+ const shouldSurroundWithQuotes =
33+ executable . includes ( ' ' ) && ! executable . startsWith ( '"' ) && ! executable . endsWith ( '"' ) ;
34+ // Handle case where executable contains white-spaces.
35+ if ( shouldSurroundWithQuotes ) {
36+ executable = `"${ executable } "` ;
37+ }
38+
39+ if ( shellType === ShellConstants . PWSH && ! executable . startsWith ( '&' ) ) {
40+ // PowerShell requires commands to be prefixed with '&' to run them.
41+ executable = `& ${ executable } ` ;
42+ }
3243 execution = terminal . shellIntegration . executeCommand ( executable , allArgs ) ;
3344 await deferred . promise ;
3445 } else {
35- const shellType = identifyTerminalShell ( terminal ) ;
3646 let text = quoteArgs ( [ executable , ...allArgs ] ) . join ( ' ' ) ;
3747 if ( shellType === ShellConstants . PWSH && ! text . startsWith ( '&' ) ) {
3848 // PowerShell requires commands to be prefixed with '&' to run them.
0 commit comments