Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/common/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ export namespace VenvManagerStrings {
export const venvErrorNoBasePython = l10n.t('No base Python found');
export const venvErrorNoPython3 = l10n.t('Did not find any base Python 3');

export const venvVirtualEnvActive = l10n.t(
'VIRTUAL_ENV is set for this VS Code session. Selection saved for new terminals only.',
);

export const venvName = l10n.t('Enter a name for the virtual environment');
export const venvNameErrorEmpty = l10n.t('Name cannot be empty');
export const venvNameErrorExists = l10n.t('A folder with the same name already exists');
Expand Down
27 changes: 21 additions & 6 deletions src/managers/builtin/venvManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import { PYTHON_EXTENSION_ID } from '../../common/constants';
import { VenvManagerStrings } from '../../common/localize';
import { traceError, traceWarn } from '../../common/logging';
import { createDeferred, Deferred } from '../../common/utils/deferred';
import { showErrorMessage, withProgress } from '../../common/window.apis';
import { normalizePath } from '../../common/utils/pathUtils';
import { showErrorMessage, showInformationMessage, withProgress } from '../../common/window.apis';
import { findParentIfFile } from '../../features/envCommands';
import { NativePythonFinder } from '../common/nativePythonFinder';
import { getLatest, shortVersion, sortEnvironments } from '../common/utils';
Expand Down Expand Up @@ -383,6 +384,16 @@ export class VenvManager implements EnvironmentManager {
return;
}

// Notify user if VIRTUAL_ENV is set and they're trying to select a different environment
if (process.env.VIRTUAL_ENV && environment) {
const virtualEnvPath = process.env.VIRTUAL_ENV;
const selectedPath = environment.sysPrefix;
// Only show notification if they selected a different environment
if (virtualEnvPath !== selectedPath) {
showInformationMessage(VenvManagerStrings.venvVirtualEnvActive);
}
}

const before = this.fsPathToEnv.get(pw.uri.fsPath);
if (environment) {
this.fsPathToEnv.set(pw.uri.fsPath, environment);
Expand Down Expand Up @@ -541,7 +552,7 @@ export class VenvManager implements EnvironmentManager {
this.fsPathToEnv.clear();

const sorted = sortEnvironments(this.collection);
const projectPaths = this.api.getPythonProjects().map((p) => path.normalize(p.uri.fsPath));
const projectPaths = this.api.getPythonProjects().map((p) => normalizePath(p.uri.fsPath));
const events: (() => void)[] = [];
// Iterates through all workspace projects
for (const p of projectPaths) {
Expand Down Expand Up @@ -580,7 +591,7 @@ export class VenvManager implements EnvironmentManager {
// 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.
const found = sorted.find((e) => {
const t = this.api.getPythonProject(e.environmentPath)?.uri.fsPath;
return t && path.normalize(t) === p;
return t && normalizePath(t) === p;
});
if (found) {
this.fsPathToEnv.set(p, found);
Expand All @@ -595,11 +606,15 @@ export class VenvManager implements EnvironmentManager {
* Finds a PythonEnvironment in the given collection (or all environments) that matches the provided file system path. O(e) where e = environments.len
*/
private findEnvironmentByPath(fsPath: string, collection?: PythonEnvironment[]): PythonEnvironment | undefined {
const normalized = path.normalize(fsPath);
const normalized = normalizePath(fsPath);
const envs = collection ?? this.collection;
return envs.find((e) => {
const n = path.normalize(e.environmentPath.fsPath);
return n === normalized || path.dirname(n) === normalized || path.dirname(path.dirname(n)) === normalized;
const n = normalizePath(e.environmentPath.fsPath);
return (
n === normalized ||
normalizePath(path.dirname(e.environmentPath.fsPath)) === normalized ||
normalizePath(path.dirname(path.dirname(e.environmentPath.fsPath))) === normalized
);
});
}

Expand Down
Loading