Skip to content

Commit 8b85d0b

Browse files
authored
Merge branch 'main' into quote-executable-task
2 parents fd32610 + 6e0b196 commit 8b85d0b

4 files changed

Lines changed: 42 additions & 14 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vscode-python-envs",
33
"displayName": "Python Environments",
44
"description": "Provides a unified python environment experience",
5-
"version": "1.1.0",
5+
"version": "1.3.0",
66
"publisher": "ms-python",
77
"preview": true,
88
"engines": {

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.

src/managers/builtin/venvManager.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
} from '../../api';
3131
import { PYTHON_EXTENSION_ID } from '../../common/constants';
3232
import { VenvManagerStrings } from '../../common/localize';
33-
import { traceError } from '../../common/logging';
33+
import { traceError, traceWarn } from '../../common/logging';
3434
import { createDeferred, Deferred } from '../../common/utils/deferred';
3535
import { showErrorMessage, withProgress } from '../../common/window.apis';
3636
import { findParentIfFile } from '../../features/envCommands';
@@ -186,12 +186,30 @@ export class VenvManager implements EnvironmentManager {
186186

187187
// Add .gitignore to the .venv folder
188188
try {
189-
const venvDir = environment.environmentPath.fsPath;
190-
const gitignorePath = path.join(venvDir, '.gitignore');
189+
// determine if env path is python binary or environment folder
190+
let envPath = environment.environmentPath.fsPath;
191+
try {
192+
const stat = await fs.stat(envPath);
193+
if (!stat.isDirectory()) {
194+
// If the env path is a file (likely the python binary), use parent-parent as the env path
195+
// following format of .venv/bin/python or .venv\Scripts\python.exe
196+
envPath = Uri.file(path.dirname(path.dirname(envPath))).fsPath;
197+
}
198+
} catch (err) {
199+
// If stat fails, fallback to original envPath
200+
traceWarn(
201+
`Failed to stat environment path: ${envPath}. Error: ${
202+
err instanceof Error ? err.message : String(err)
203+
}, continuing to attempt to create .gitignore.`,
204+
);
205+
}
206+
const gitignorePath = path.join(envPath, '.gitignore');
191207
await fs.writeFile(gitignorePath, '*\n', { flag: 'w' });
192208
} catch (err) {
193209
traceError(
194-
`Failed to create .gitignore in venv: ${err instanceof Error ? err.message : String(err)}`,
210+
`Failed to create .gitignore in venv: ${
211+
err instanceof Error ? err.message : String(err)
212+
}, continuing.`,
195213
);
196214
}
197215

0 commit comments

Comments
 (0)