Skip to content

Commit 76dce84

Browse files
Copilotanthonykim1
andcommitted
Fix getEnvironment to use active editor scope when undefined
Co-authored-by: anthonykim1 <62267334+anthonykim1@users.noreply.github.com>
1 parent be7eed4 commit 76dce84

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

package-lock.json

Lines changed: 2 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/features/pythonApi.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ import { runInBackground } from './execution/runInBackground';
4949
import { EnvVarManager } from './execution/envVariableManager';
5050
import { checkUri } from '../common/utils/pathUtils';
5151
import { waitForAllEnvManagers, waitForEnvManager, waitForEnvManagerId } from './common/managerReady';
52+
import { activeTextEditor } from '../common/window.apis';
53+
import { getWorkspaceFolders } from '../common/workspace.apis';
5254

5355
class 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

Comments
 (0)