Skip to content

Commit 26b7063

Browse files
committed
fix: adjust cwd for terminal creation & envs creation if project type is file
1 parent f622704 commit 26b7063

2 files changed

Lines changed: 22 additions & 5 deletions

File tree

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,
@@ -539,22 +541,25 @@ export async function createTerminalCommand(
539541
const pw = await pickProject(api.getPythonProjects());
540542
if (pw) {
541543
const env = await api.getEnvironment(pw.uri);
544+
const cwd = await findParentIfFile(pw.uri.fsPath);
542545
if (env) {
543-
return await tm.create(env, { cwd: pw.uri });
546+
return await tm.create(env, { cwd });
544547
}
545548
}
546549
} else if (context instanceof Uri) {
547550
const uri = context as Uri;
548551
const env = await api.getEnvironment(uri);
549552
const pw = api.getPythonProject(uri);
550553
if (env && pw) {
551-
return await tm.create(env, { cwd: pw.uri });
554+
const cwd = await findParentIfFile(pw.uri.fsPath);
555+
return await tm.create(env, { cwd });
552556
}
553557
} else if (context instanceof ProjectItem) {
554558
const view = context as ProjectItem;
555559
const env = await api.getEnvironment(view.project.uri);
560+
const cwd = await findParentIfFile(view.project.uri.fsPath);
556561
if (env) {
557-
const terminal = await tm.create(env, { cwd: view.project.uri });
562+
const terminal = await tm.create(env, { cwd });
558563
terminal.show();
559564
return terminal;
560565
}
@@ -569,13 +574,23 @@ export async function createTerminalCommand(
569574
const view = context as PythonEnvTreeItem;
570575
const pw = await pickProject(api.getPythonProjects());
571576
if (pw) {
572-
const terminal = await tm.create(view.environment, { cwd: pw.uri });
577+
const cwd = await findParentIfFile(pw.uri.fsPath);
578+
const terminal = await tm.create(view.environment, { cwd });
573579
terminal.show();
574580
return terminal;
575581
}
576582
}
577583
}
578584

585+
export async function findParentIfFile(cwd: string): Promise<string> {
586+
const stat = await fs.stat(cwd);
587+
if (stat.isFile()) {
588+
// If the project is a file, use the directory of the file as the cwd
589+
return path.dirname(cwd);
590+
}
591+
return cwd;
592+
}
593+
579594
export async function runInTerminalCommand(
580595
item: unknown,
581596
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)