Skip to content

Commit cc45070

Browse files
authored
Add notification for active virtual environment selection (#1179)
Notify users when attempting to select a different virtual environment while one is already active. This is the case where VS Code was launched from an activated environment (i.e, VIRTUAL_ENV is set) . This enhances user awareness and prevents confusion regarding the current environment in use.
1 parent bc250ea commit cc45070

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/common/localize.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ export namespace VenvManagerStrings {
9696
export const venvErrorNoBasePython = l10n.t('No base Python found');
9797
export const venvErrorNoPython3 = l10n.t('Did not find any base Python 3');
9898

99+
export const venvVirtualEnvActive = l10n.t(
100+
'VIRTUAL_ENV is set for this VS Code session. Selection saved for new terminals only.',
101+
);
102+
99103
export const venvName = l10n.t('Enter a name for the virtual environment');
100104
export const venvNameErrorEmpty = l10n.t('Name cannot be empty');
101105
export const venvNameErrorExists = l10n.t('A folder with the same name already exists');

src/managers/builtin/venvManager.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import { PYTHON_EXTENSION_ID } from '../../common/constants';
3232
import { VenvManagerStrings } from '../../common/localize';
3333
import { traceError, traceWarn } from '../../common/logging';
3434
import { createDeferred, Deferred } from '../../common/utils/deferred';
35-
import { showErrorMessage, withProgress } from '../../common/window.apis';
35+
import { normalizePath } from '../../common/utils/pathUtils';
36+
import { showErrorMessage, showInformationMessage, withProgress } from '../../common/window.apis';
3637
import { findParentIfFile } from '../../features/envCommands';
3738
import { NativePythonFinder } from '../common/nativePythonFinder';
3839
import { getLatest, shortVersion, sortEnvironments } from '../common/utils';
@@ -383,6 +384,16 @@ export class VenvManager implements EnvironmentManager {
383384
return;
384385
}
385386

387+
// Notify user if VIRTUAL_ENV is set and they're trying to select a different environment
388+
if (process.env.VIRTUAL_ENV && environment) {
389+
const virtualEnvPath = process.env.VIRTUAL_ENV;
390+
const selectedPath = environment.sysPrefix;
391+
// Only show notification if they selected a different environment
392+
if (virtualEnvPath !== selectedPath) {
393+
showInformationMessage(VenvManagerStrings.venvVirtualEnvActive);
394+
}
395+
}
396+
386397
const before = this.fsPathToEnv.get(pw.uri.fsPath);
387398
if (environment) {
388399
this.fsPathToEnv.set(pw.uri.fsPath, environment);
@@ -541,7 +552,7 @@ export class VenvManager implements EnvironmentManager {
541552
this.fsPathToEnv.clear();
542553

543554
const sorted = sortEnvironments(this.collection);
544-
const projectPaths = this.api.getPythonProjects().map((p) => path.normalize(p.uri.fsPath));
555+
const projectPaths = this.api.getPythonProjects().map((p) => normalizePath(p.uri.fsPath));
545556
const events: (() => void)[] = [];
546557
// Iterates through all workspace projects
547558
for (const p of projectPaths) {
@@ -580,7 +591,7 @@ export class VenvManager implements EnvironmentManager {
580591
// Search through all known environments (e) and check if any are associated with the current project path. If so, add that environment and path in the map.
581592
const found = sorted.find((e) => {
582593
const t = this.api.getPythonProject(e.environmentPath)?.uri.fsPath;
583-
return t && path.normalize(t) === p;
594+
return t && normalizePath(t) === p;
584595
});
585596
if (found) {
586597
this.fsPathToEnv.set(p, found);
@@ -595,11 +606,15 @@ export class VenvManager implements EnvironmentManager {
595606
* Finds a PythonEnvironment in the given collection (or all environments) that matches the provided file system path. O(e) where e = environments.len
596607
*/
597608
private findEnvironmentByPath(fsPath: string, collection?: PythonEnvironment[]): PythonEnvironment | undefined {
598-
const normalized = path.normalize(fsPath);
609+
const normalized = normalizePath(fsPath);
599610
const envs = collection ?? this.collection;
600611
return envs.find((e) => {
601-
const n = path.normalize(e.environmentPath.fsPath);
602-
return n === normalized || path.dirname(n) === normalized || path.dirname(path.dirname(n)) === normalized;
612+
const n = normalizePath(e.environmentPath.fsPath);
613+
return (
614+
n === normalized ||
615+
normalizePath(path.dirname(e.environmentPath.fsPath)) === normalized ||
616+
normalizePath(path.dirname(path.dirname(e.environmentPath.fsPath))) === normalized
617+
);
603618
});
604619
}
605620

0 commit comments

Comments
 (0)