Skip to content

Commit 174650e

Browse files
anthonykim1eleanorjboyd
authored andcommitted
Attach & for shell integration enabled pwsh case (microsoft#652)
Resolves microsoft#649
1 parent b57eff1 commit 174650e

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

src/features/terminal/runInTerminal.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Terminal, TerminalShellExecution } from 'vscode';
22
import { PythonEnvironment, PythonTerminalExecutionOptions } from '../../api';
3-
import { onDidEndTerminalShellExecution } from '../../common/window.apis';
43
import { createDeferred } from '../../common/utils/deferred';
5-
import { quoteArgs } from '../execution/execUtils';
6-
import { identifyTerminalShell } from '../common/shellDetector';
4+
import { onDidEndTerminalShellExecution } from '../../common/window.apis';
75
import { ShellConstants } from '../common/shellConstants';
6+
import { identifyTerminalShell } from '../common/shellDetector';
7+
import { quoteArgs } from '../execution/execUtils';
88

99
export 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

Comments
 (0)