1+ import * as fs from 'fs-extra' ;
2+ import * as path from 'path' ;
13import { commands , QuickInputButtons , TaskExecution , TaskRevealKind , Terminal , Uri , workspace } from 'vscode' ;
24import {
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+
556571export async function runInTerminalCommand (
557572 item : unknown ,
558573 api : PythonEnvironmentApi ,
0 commit comments