Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions src/features/terminal/runInTerminal.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<void>();
Expand All @@ -29,10 +28,21 @@ export async function runInTerminal(
deferred.resolve();
}
});

const shouldSurroundWithQuotes =
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrapping executable in double quotes after checking if there is white space.
Also checking they are not already wrapped in double quotes.

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.
Expand Down