From 26b7063d661f76aa5c4797b356ab0c236030edc2 Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Fri, 13 Jun 2025 14:59:50 -0700 Subject: [PATCH] fix: adjust cwd for terminal creation & envs creation if project type is file --- src/features/envCommands.ts | 23 +++++++++++++++++++---- src/managers/builtin/venvManager.ts | 4 +++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/features/envCommands.ts b/src/features/envCommands.ts index 55f0acee..912c1e53 100644 --- a/src/features/envCommands.ts +++ b/src/features/envCommands.ts @@ -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, @@ -539,8 +541,9 @@ 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) { @@ -548,13 +551,15 @@ export async function createTerminalCommand( 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; } @@ -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 { + 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, diff --git a/src/managers/builtin/venvManager.ts b/src/managers/builtin/venvManager.ts index a2e84c4a..122d5e8c 100644 --- a/src/managers/builtin/venvManager.ts +++ b/src/managers/builtin/venvManager.ts @@ -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 { @@ -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) {