Skip to content

Commit 0157b2a

Browse files
authored
fix: adjust cwd for terminal creation & envs creation if project type is file (#539)
fixes #540
1 parent 683ab83 commit 0157b2a

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/features/envCommands.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as fs from 'fs-extra';
2+
import * as path from 'path';
13
import { commands, QuickInputButtons, TaskExecution, TaskRevealKind, Terminal, Uri, workspace } from 'vscode';
24
import {
35
CreateEnvironmentOptions,
@@ -516,22 +518,25 @@ export async function createTerminalCommand(
516518
const pw = await pickProject(api.getPythonProjects());
517519
if (pw) {
518520
const env = await api.getEnvironment(pw.uri);
521+
const cwd = await findParentIfFile(pw.uri.fsPath);
519522
if (env) {
520-
return await tm.create(env, { cwd: pw.uri });
523+
return await tm.create(env, { cwd });
521524
}
522525
}
523526
} else if (context instanceof Uri) {
524527
const uri = context as Uri;
525528
const env = await api.getEnvironment(uri);
526529
const pw = api.getPythonProject(uri);
527530
if (env && pw) {
528-
return await tm.create(env, { cwd: pw.uri });
531+
const cwd = await findParentIfFile(pw.uri.fsPath);
532+
return await tm.create(env, { cwd });
529533
}
530534
} else if (context instanceof ProjectItem) {
531535
const view = context as ProjectItem;
532536
const env = await api.getEnvironment(view.project.uri);
537+
const cwd = await findParentIfFile(view.project.uri.fsPath);
533538
if (env) {
534-
const terminal = await tm.create(env, { cwd: view.project.uri });
539+
const terminal = await tm.create(env, { cwd });
535540
terminal.show();
536541
return terminal;
537542
}
@@ -546,13 +551,23 @@ export async function createTerminalCommand(
546551
const view = context as PythonEnvTreeItem;
547552
const pw = await pickProject(api.getPythonProjects());
548553
if (pw) {
549-
const terminal = await tm.create(view.environment, { cwd: pw.uri });
554+
const cwd = await findParentIfFile(pw.uri.fsPath);
555+
const terminal = await tm.create(view.environment, { cwd });
550556
terminal.show();
551557
return terminal;
552558
}
553559
}
554560
}
555561

562+
export async function findParentIfFile(cwd: string): Promise<string> {
563+
const stat = await fs.stat(cwd);
564+
if (stat.isFile()) {
565+
// If the project is a file, use the directory of the file as the cwd
566+
return path.dirname(cwd);
567+
}
568+
return cwd;
569+
}
570+
556571
export async function runInTerminalCommand(
557572
item: unknown,
558573
api: PythonEnvironmentApi,

src/managers/builtin/venvManager.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { PYTHON_EXTENSION_ID } from '../../common/constants';
2222
import { VenvManagerStrings } from '../../common/localize';
2323
import { createDeferred, Deferred } from '../../common/utils/deferred';
2424
import { showErrorMessage, withProgress } from '../../common/window.apis';
25+
import { findParentIfFile } from '../../features/envCommands';
2526
import { NativePythonFinder } from '../common/nativePythonFinder';
2627
import { getLatest, shortVersion, sortEnvironments } from '../common/utils';
2728
import {
@@ -125,7 +126,8 @@ export class VenvManager implements EnvironmentManager {
125126
return;
126127
}
127128

128-
const venvRoot: Uri = uri;
129+
const venvRoot: Uri = Uri.file(await findParentIfFile(uri.fsPath));
130+
129131
const globals = await this.baseManager.getEnvironments('global');
130132
let environment: PythonEnvironment | undefined = undefined;
131133
if (options?.quickCreate) {

0 commit comments

Comments
 (0)