@@ -49,6 +49,8 @@ import { runInBackground } from './execution/runInBackground';
4949import { EnvVarManager } from './execution/envVariableManager' ;
5050import { checkUri } from '../common/utils/pathUtils' ;
5151import { waitForAllEnvManagers , waitForEnvManager , waitForEnvManagerId } from './common/managerReady' ;
52+ import { activeTextEditor } from '../common/window.apis' ;
53+ import { getWorkspaceFolders } from '../common/workspace.apis' ;
5254
5355class PythonEnvironmentApiImpl implements PythonEnvironmentApi {
5456 private readonly _onDidChangeEnvironments = new EventEmitter < DidChangeEnvironmentsEventArgs > ( ) ;
@@ -214,7 +216,24 @@ class PythonEnvironmentApiImpl implements PythonEnvironmentApi {
214216 return this . envManagers . setEnvironment ( currentScope , environment ) ;
215217 }
216218 async getEnvironment ( scope : GetEnvironmentScope ) : Promise < PythonEnvironment | undefined > {
217- const currentScope = checkUri ( scope ) as GetEnvironmentScope ;
219+ let currentScope = checkUri ( scope ) as GetEnvironmentScope ;
220+
221+ // When scope is undefined, try to determine the appropriate scope from context
222+ if ( currentScope === undefined ) {
223+ // First, check if there's an active text editor with a valid document
224+ const activeDoc = activeTextEditor ( ) ?. document ;
225+ if ( activeDoc && ! activeDoc . isUntitled && activeDoc . uri . scheme === 'file' ) {
226+ currentScope = activeDoc . uri ;
227+ } else {
228+ // If no active editor, check if there's a single workspace folder
229+ const workspaceFolders = getWorkspaceFolders ( ) ;
230+ if ( workspaceFolders && workspaceFolders . length === 1 ) {
231+ currentScope = workspaceFolders [ 0 ] . uri ;
232+ }
233+ // Otherwise currentScope remains undefined, which will return the global environment
234+ }
235+ }
236+
218237 await waitForEnvManager ( currentScope ? [ currentScope ] : undefined ) ;
219238 return this . envManagers . getEnvironment ( currentScope ) ;
220239 }
0 commit comments