Skip to content

Commit e4e2fd2

Browse files
author
Kartik Raj
authored
Added prompt to flip "inheritEnv" setting to false to fix conda activation issue (#7633)
* Added functionality * News * Temp * Added tests * Add missing test * Fixes * Intialize prompt in background
1 parent 6de4b2f commit e4e2fd2

File tree

9 files changed

+478
-2
lines changed

9 files changed

+478
-2
lines changed

news/2 Fixes/7607.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added prompt to flip "inheritEnv" setting to false to fix conda activation issue

package.nls.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
"Experiments.inGroup": "User belongs to experiment group '{0}'",
122122
"Interpreters.RefreshingInterpreters": "Refreshing Python Interpreters",
123123
"Interpreters.LoadingInterpreters": "Loading Python Interpreters",
124+
"Interpreters.condaInheritEnvMessage": "We noticed you're using a conda environment. If you are experiencing issues with this environment in the integrated terminal, we suggest the \"terminal.integrated.inheritEnv\" setting to be changed to false. Would you like to update this setting?",
124125
"Logging.CurrentWorkingDirectory": "cwd:",
125126
"Common.doNotShowAgain": "Do not show again",
126127
"Common.reload": "Reload",
@@ -367,6 +368,6 @@
367368
"DataScience.untitledNotebookMessage": "Your changes will be lost if you don't save them.",
368369
"DataScience.untitledNotebookYes": "Save",
369370
"DataScience.untitledNotebookNo": "Cancel",
370-
"DataScience.noInterpreter" : "No python selected",
371-
"DataScience.notebookNotFound" : "python -m jupyter notebook --version is not running"
371+
"DataScience.noInterpreter": "No python selected",
372+
"DataScience.notebookNotFound": "python -m jupyter notebook --version is not running"
372373
}

src/client/common/utils/localize.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export namespace Experiments {
6060
export namespace Interpreters {
6161
export const loading = localize('Interpreters.LoadingInterpreters', 'Loading Python Interpreters');
6262
export const refreshing = localize('Interpreters.RefreshingInterpreters', 'Refreshing Python Interpreters');
63+
export const condaInheritEnvMessage = localize('Interpreters.condaInheritEnvMessage', 'We noticed you\'re using a conda environment. If you are experiencing issues with this environment in the integrated terminal, we suggest the \"terminal.integrated.inheritEnv\" setting to be changed to false. Would you like to update this setting?');
6364
export const environmentPromptMessage = localize('Interpreters.environmentPromptMessage', 'We noticed a new virtual environment has been created. Do you want to select it for the workspace folder?');
6465
export const selectInterpreterTip = localize('Interpreters.selectInterpreterTip', 'Tip: you can change the Python interpreter used by the Python extension by clicking on the Python version in the status bar');
6566
}

src/client/interpreter/serviceRegistry.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ import { WindowsStoreInterpreter } from './locators/services/windowsStoreInterpr
7474
import { WorkspaceVirtualEnvironmentsSearchPathProvider, WorkspaceVirtualEnvService } from './locators/services/workspaceVirtualEnvService';
7575
import { WorkspaceVirtualEnvWatcherService } from './locators/services/workspaceVirtualEnvWatcherService';
7676
import { IPipEnvServiceHelper, IPythonInPathCommandProvider } from './locators/types';
77+
import { CondaInheritEnvPrompt } from './virtualEnvs/condaInheritEnvPrompt';
7778
import { VirtualEnvironmentManager } from './virtualEnvs/index';
7879
import { IVirtualEnvironmentManager } from './virtualEnvs/types';
7980
import { VirtualEnvironmentPrompt } from './virtualEnvs/virtualEnvPrompt';
@@ -135,6 +136,7 @@ export function registerTypes(serviceManager: IServiceManager) {
135136

136137
serviceManager.addSingleton<IEnvironmentActivationService>(IEnvironmentActivationService, EnvironmentActivationService);
137138

139+
serviceManager.addSingleton<IExtensionActivationService>(IExtensionActivationService, CondaInheritEnvPrompt);
138140
serviceManager.addSingleton<WindowsStoreInterpreter>(WindowsStoreInterpreter, WindowsStoreInterpreter);
139141
serviceManager.addSingleton<InterpreterHashProvider>(InterpreterHashProvider, InterpreterHashProvider);
140142
serviceManager.addSingleton<InterpeterHashProviderFactory>(InterpeterHashProviderFactory, InterpeterHashProviderFactory);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import { inject, injectable, optional } from 'inversify';
5+
import { ConfigurationTarget, Uri } from 'vscode';
6+
import { IExtensionActivationService } from '../../activation/types';
7+
import { IApplicationShell, IWorkspaceService } from '../../common/application/types';
8+
import { traceDecorators, traceError } from '../../common/logger';
9+
import { IPersistentStateFactory } from '../../common/types';
10+
import { InteractiveShiftEnterBanner, Interpreters } from '../../common/utils/localize';
11+
import { sendTelemetryEvent } from '../../telemetry';
12+
import { EventName } from '../../telemetry/constants';
13+
import { IInterpreterService, InterpreterType } from '../contracts';
14+
15+
export const condaInheritEnvPromptKey = 'CONDA_INHERIT_ENV_PROMPT_KEY';
16+
17+
@injectable()
18+
export class CondaInheritEnvPrompt implements IExtensionActivationService {
19+
constructor(
20+
@inject(IInterpreterService) private readonly interpreterService: IInterpreterService,
21+
@inject(IWorkspaceService) private readonly workspaceService: IWorkspaceService,
22+
@inject(IApplicationShell) private readonly appShell: IApplicationShell,
23+
@inject(IPersistentStateFactory) private readonly persistentStateFactory: IPersistentStateFactory,
24+
@optional() public hasPromptBeenShownInCurrentSession: boolean = false
25+
) { }
26+
27+
public async activate(resource: Uri): Promise<void> {
28+
this.initializeInBackground(resource).ignoreErrors();
29+
}
30+
31+
@traceDecorators.error('Failed to intialize conda inherit env prompt')
32+
public async initializeInBackground(resource: Uri): Promise<void> {
33+
const show = await this.shouldShowPrompt(resource);
34+
if (!show) {
35+
return;
36+
}
37+
await this.promptAndUpdate();
38+
}
39+
40+
@traceDecorators.error('Failed to display conda inherit env prompt')
41+
public async promptAndUpdate() {
42+
const notificationPromptEnabled = this.persistentStateFactory.createGlobalPersistentState(condaInheritEnvPromptKey, true);
43+
if (!notificationPromptEnabled.value) {
44+
return;
45+
}
46+
const prompts = [InteractiveShiftEnterBanner.bannerLabelYes(), InteractiveShiftEnterBanner.bannerLabelNo()];
47+
const telemetrySelections: ['Yes', 'No'] = ['Yes', 'No'];
48+
const selection = await this.appShell.showInformationMessage(Interpreters.condaInheritEnvMessage(), ...prompts);
49+
sendTelemetryEvent(EventName.CONDA_INHERIT_ENV_PROMPT, undefined, { selection: selection ? telemetrySelections[prompts.indexOf(selection)] : undefined });
50+
if (!selection) {
51+
return;
52+
}
53+
if (selection === prompts[0]) {
54+
await this.workspaceService.getConfiguration('terminal').update('integrated.inheritEnv', false, ConfigurationTarget.Global);
55+
} else if (selection === prompts[1]) {
56+
await notificationPromptEnabled.updateValue(false);
57+
}
58+
}
59+
60+
@traceDecorators.error('Failed to check whether to display prompt for conda inherit env setting')
61+
public async shouldShowPrompt(resource: Uri): Promise<boolean> {
62+
if (this.hasPromptBeenShownInCurrentSession) {
63+
return false;
64+
}
65+
const interpreter = await this.interpreterService.getActiveInterpreter(resource);
66+
if (!interpreter || interpreter.type !== InterpreterType.Conda) {
67+
return false;
68+
}
69+
const setting = this.workspaceService.getConfiguration('terminal', resource).inspect<boolean>('integrated.inheritEnv');
70+
if (!setting) {
71+
traceError('WorkspaceConfiguration.inspect returns `undefined` for setting `terminal.integrated.inheritEnv`');
72+
return false;
73+
}
74+
if (setting.globalValue !== undefined || setting.workspaceValue !== undefined || setting.workspaceFolderValue !== undefined) {
75+
return false;
76+
}
77+
this.hasPromptBeenShownInCurrentSession = true;
78+
return true;
79+
}
80+
}

src/client/telemetry/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export enum EventName {
3030
PYTHON_INTERPRETER_ACTIVATION_FOR_TERMINAL = 'PYTHON_INTERPRETER_ACTIVATION_FOR_TERMINAL',
3131
TERMINAL_SHELL_IDENTIFICATION = 'TERMINAL_SHELL_IDENTIFICATION',
3232
PYTHON_INTERPRETER_ACTIVATE_ENVIRONMENT_PROMPT = 'PYTHON_INTERPRETER_ACTIVATE_ENVIRONMENT_PROMPT',
33+
CONDA_INHERIT_ENV_PROMPT = 'CONDA_INHERIT_ENV_PROMPT',
3334
INSIDERS_RELOAD_PROMPT = 'INSIDERS_RELOAD_PROMPT',
3435
INSIDERS_PROMPT = 'INSIDERS_PROMPT',
3536
OPT_INTO_INSIDERS_AGAIN_PROMPT = 'OPT_INTO_INSIDERS_AGAIN_PROMPT',

src/client/telemetry/index.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,17 @@ export interface IEventNamePropertyMapping {
931931
*/
932932
interpreters?: number;
933933
};
934+
/**
935+
* Telemetry event sent with details when user clicks the prompt with the following message
936+
* `Prompt message` :- 'We noticed you're using a conda environment. If you are experiencing issues with this environment in the integrated terminal, we suggest the "terminal.integrated.inheritEnv" setting to be changed to false. Would you like to update this setting?'
937+
*/
938+
[EventName.CONDA_INHERIT_ENV_PROMPT]: {
939+
/**
940+
* `Yes` When 'Yes' option is selected
941+
* `No` When 'No' option is selected
942+
*/
943+
selection: 'Yes' | 'No' | undefined;
944+
};
934945
/**
935946
* Telemetry event sent with details when user clicks a button in the virtual environment prompt.
936947
* `Prompt message` :- 'We noticed a new virtual environment has been created. Do you want to select it for the workspace folder?'

src/test/interpreters/serviceRegistry.unit.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import { WorkspaceVirtualEnvWatcherService } from '../../client/interpreter/loca
8282
import { IPipEnvServiceHelper, IPythonInPathCommandProvider } from '../../client/interpreter/locators/types';
8383
import { registerTypes } from '../../client/interpreter/serviceRegistry';
8484
import { VirtualEnvironmentManager } from '../../client/interpreter/virtualEnvs';
85+
import { CondaInheritEnvPrompt } from '../../client/interpreter/virtualEnvs/condaInheritEnvPrompt';
8586
import { IVirtualEnvironmentManager } from '../../client/interpreter/virtualEnvs/types';
8687
import { VirtualEnvironmentPrompt } from '../../client/interpreter/virtualEnvs/virtualEnvPrompt';
8788
import { ServiceManager } from '../../client/ioc/serviceManager';
@@ -142,6 +143,7 @@ suite('Interpreters - Service Registry', () => {
142143
[IInterpreterAutoSelectionService, InterpreterAutoSelectionService],
143144

144145
[IEnvironmentActivationService, EnvironmentActivationService],
146+
[IExtensionActivationService, CondaInheritEnvPrompt],
145147

146148
[WindowsStoreInterpreter, WindowsStoreInterpreter],
147149
[InterpreterHashProvider, InterpreterHashProvider],

0 commit comments

Comments
 (0)