Skip to content
Merged
Show file tree
Hide file tree
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
23 changes: 19 additions & 4 deletions src/features/envCommands.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import { commands, QuickInputButtons, TaskExecution, TaskRevealKind, Terminal, Uri, workspace } from 'vscode';
import {
CreateEnvironmentOptions,
Expand Down Expand Up @@ -539,22 +541,25 @@ export async function createTerminalCommand(
const pw = await pickProject(api.getPythonProjects());
if (pw) {
const env = await api.getEnvironment(pw.uri);
const cwd = await findParentIfFile(pw.uri.fsPath);
if (env) {
return await tm.create(env, { cwd: pw.uri });
return await tm.create(env, { cwd });
}
}
} else if (context instanceof Uri) {
const uri = context as Uri;
const env = await api.getEnvironment(uri);
const pw = api.getPythonProject(uri);
if (env && pw) {
return await tm.create(env, { cwd: pw.uri });
const cwd = await findParentIfFile(pw.uri.fsPath);
return await tm.create(env, { cwd });
}
} else if (context instanceof ProjectItem) {
const view = context as ProjectItem;
const env = await api.getEnvironment(view.project.uri);
const cwd = await findParentIfFile(view.project.uri.fsPath);
if (env) {
const terminal = await tm.create(env, { cwd: view.project.uri });
const terminal = await tm.create(env, { cwd });
terminal.show();
return terminal;
}
Expand All @@ -569,13 +574,23 @@ export async function createTerminalCommand(
const view = context as PythonEnvTreeItem;
const pw = await pickProject(api.getPythonProjects());
if (pw) {
const terminal = await tm.create(view.environment, { cwd: pw.uri });
const cwd = await findParentIfFile(pw.uri.fsPath);
const terminal = await tm.create(view.environment, { cwd });
terminal.show();
return terminal;
}
}
}

export async function findParentIfFile(cwd: string): Promise<string> {
const stat = await fs.stat(cwd);
if (stat.isFile()) {
// If the project is a file, use the directory of the file as the cwd
return path.dirname(cwd);
}
return cwd;
}

export async function runInTerminalCommand(
item: unknown,
api: PythonEnvironmentApi,
Expand Down
4 changes: 3 additions & 1 deletion src/managers/builtin/venvManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { PYTHON_EXTENSION_ID } from '../../common/constants';
import { VenvManagerStrings } from '../../common/localize';
import { createDeferred, Deferred } from '../../common/utils/deferred';
import { showErrorMessage, withProgress } from '../../common/window.apis';
import { findParentIfFile } from '../../features/envCommands';
import { NativePythonFinder } from '../common/nativePythonFinder';
import { getLatest, shortVersion, sortEnvironments } from '../common/utils';
import {
Expand Down Expand Up @@ -125,7 +126,8 @@ export class VenvManager implements EnvironmentManager {
return;
}

const venvRoot: Uri = uri;
const venvRoot: Uri = Uri.file(await findParentIfFile(uri.fsPath));

const globals = await this.baseManager.getEnvironments('global');
let environment: PythonEnvironment | undefined = undefined;
if (options?.quickCreate) {
Expand Down