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 ,
@@ -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+
579594export async function runInTerminalCommand (
580595 item : unknown ,
581596 api : PythonEnvironmentApi ,
0 commit comments