1
+ import * as fs from 'fs-extra' ;
2
+ import * as path from 'path' ;
1
3
import { commands , QuickInputButtons , TaskExecution , TaskRevealKind , Terminal , Uri , workspace } from 'vscode' ;
2
4
import {
3
5
CreateEnvironmentOptions ,
@@ -516,22 +518,25 @@ export async function createTerminalCommand(
516
518
const pw = await pickProject ( api . getPythonProjects ( ) ) ;
517
519
if ( pw ) {
518
520
const env = await api . getEnvironment ( pw . uri ) ;
521
+ const cwd = await findParentIfFile ( pw . uri . fsPath ) ;
519
522
if ( env ) {
520
- return await tm . create ( env , { cwd : pw . uri } ) ;
523
+ return await tm . create ( env , { cwd } ) ;
521
524
}
522
525
}
523
526
} else if ( context instanceof Uri ) {
524
527
const uri = context as Uri ;
525
528
const env = await api . getEnvironment ( uri ) ;
526
529
const pw = api . getPythonProject ( uri ) ;
527
530
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 } ) ;
529
533
}
530
534
} else if ( context instanceof ProjectItem ) {
531
535
const view = context as ProjectItem ;
532
536
const env = await api . getEnvironment ( view . project . uri ) ;
537
+ const cwd = await findParentIfFile ( view . project . uri . fsPath ) ;
533
538
if ( env ) {
534
- const terminal = await tm . create ( env , { cwd : view . project . uri } ) ;
539
+ const terminal = await tm . create ( env , { cwd } ) ;
535
540
terminal . show ( ) ;
536
541
return terminal ;
537
542
}
@@ -546,13 +551,23 @@ export async function createTerminalCommand(
546
551
const view = context as PythonEnvTreeItem ;
547
552
const pw = await pickProject ( api . getPythonProjects ( ) ) ;
548
553
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 } ) ;
550
556
terminal . show ( ) ;
551
557
return terminal ;
552
558
}
553
559
}
554
560
}
555
561
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
+
556
571
export async function runInTerminalCommand (
557
572
item : unknown ,
558
573
api : PythonEnvironmentApi ,
0 commit comments