Skip to content

Commit ef9388c

Browse files
author
Kartik Raj
committed
More changes
1 parent a1137df commit ef9388c

File tree

7 files changed

+37
-9
lines changed

7 files changed

+37
-9
lines changed

src/client/activation/activationManager.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { IFileSystem } from '../common/platform/types';
1212
import { IDisposable, IInterpreterPathService, Resource } from '../common/types';
1313
import { Deferred } from '../common/utils/async';
1414
import { IInterpreterAutoSelectionService } from '../interpreter/autoSelection/types';
15+
import { IActivatedEnvironmentLaunch } from '../interpreter/contracts';
1516
import { traceDecoratorError } from '../logging';
1617
import { sendActivationTelemetry } from '../telemetry/envFileTelemetry';
1718
import { IExtensionActivationManager, IExtensionActivationService, IExtensionSingleActivationService } from './types';
@@ -37,6 +38,7 @@ export class ExtensionActivationManager implements IExtensionActivationManager {
3738
@inject(IFileSystem) private readonly fileSystem: IFileSystem,
3839
@inject(IActiveResourceService) private readonly activeResourceService: IActiveResourceService,
3940
@inject(IInterpreterPathService) private readonly interpreterPathService: IInterpreterPathService,
41+
@inject(IActivatedEnvironmentLaunch) private readonly activatedEnvLaunch: IActivatedEnvironmentLaunch,
4042
) {}
4143

4244
private filterServices() {
@@ -91,6 +93,7 @@ export class ExtensionActivationManager implements IExtensionActivationManager {
9193

9294
if (this.workspaceService.isTrusted) {
9395
// Do not interact with interpreters in a untrusted workspace.
96+
await this.activatedEnvLaunch.selectIfLaunchedViaActivatedEnv();
9497
await this.autoSelection.autoSelectInterpreter(resource);
9598
await this.interpreterPathService.copyOldInterpreterStorageValuesToNew(resource);
9699
}

src/client/common/persistentState.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import { inject, injectable, named } from 'inversify';
77
import { Memento } from 'vscode';
88
import { IExtensionSingleActivationService } from '../activation/types';
9-
import { traceError } from '../logging';
9+
import { traceError, traceVerbose, traceWarn } from '../logging';
1010
import { ICommandManager } from './application/types';
1111
import { Commands } from './constants';
1212
import {
@@ -41,13 +41,21 @@ export class PersistentState<T> implements IPersistentState<T> {
4141
}
4242
}
4343

44-
public async updateValue(newValue: T): Promise<void> {
44+
public async updateValue(newValue: T, retryOnce = true): Promise<void> {
4545
try {
4646
if (this.expiryDurationMs) {
4747
await this.storage.update(this.key, { data: newValue, expiry: Date.now() + this.expiryDurationMs });
4848
} else {
4949
await this.storage.update(this.key, newValue);
5050
}
51+
if (retryOnce && JSON.stringify(this.value) != JSON.stringify(newValue)) {
52+
traceVerbose('Storage update failed for key', this.key, ' retrying');
53+
await this.updateValue(undefined as any, false);
54+
await this.updateValue(newValue, false);
55+
if (JSON.stringify(this.value) != JSON.stringify(newValue)) {
56+
traceWarn('Retry failed, storage update failed for key', this.key);
57+
}
58+
}
5159
} catch (ex) {
5260
traceError('Error while updating storage for key:', this.key, ex);
5361
}

src/client/common/process/pythonExecutionFactory.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { inject, injectable } from 'inversify';
44

55
import { IEnvironmentActivationService } from '../../interpreter/activation/types';
6-
import { IComponentAdapter } from '../../interpreter/contracts';
6+
import { IActivatedEnvironmentLaunch, IComponentAdapter } from '../../interpreter/contracts';
77
import { IServiceContainer } from '../../ioc/types';
88
import { sendTelemetryEvent } from '../../telemetry';
99
import { EventName } from '../../telemetry/constants';
@@ -52,6 +52,10 @@ export class PythonExecutionFactory implements IPythonExecutionFactory {
5252
public async create(options: ExecutionFactoryCreationOptions): Promise<IPythonExecutionService> {
5353
let { pythonPath } = options;
5454
if (!pythonPath || pythonPath === 'python') {
55+
const activatedEnvLaunch = this.serviceContainer.get<IActivatedEnvironmentLaunch>(
56+
IActivatedEnvironmentLaunch,
57+
);
58+
await activatedEnvLaunch.selectIfLaunchedViaActivatedEnv();
5559
// If python path wasn't passed in, we need to auto select it and then read it
5660
// from the configuration.
5761
const interpreterPath = this.interpreterPathExpHelper.get(options.resource);

src/client/common/utils/localize.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,9 @@ export namespace Interpreters {
192192
'We noticed you\'re using a conda environment. If you are experiencing issues with this environment in the integrated terminal, we recommend that you let the Python extension change "terminal.integrated.inheritEnv" to false in your user settings.',
193193
);
194194
export const activatedCondaEnvLaunch = l10n.t(
195-
'Interpreters.activatedCondaEnvLaunch',
196-
'We noticed VSCode was launched from an activated conda environment, would you like to select it?',
195+
'We noticed VS Code was launched from an activated conda environment, would you like to select it?',
197196
);
198197
export const environmentPromptMessage = l10n.t(
199-
'Interpreters.environmentPromptMessage',
200198
'We noticed a new environment has been created. Do you want to select it for the workspace folder?',
201199
);
202200
export const entireWorkspace = l10n.t('Select at workspace level');

src/client/interpreter/interpreterService.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ export class InterpreterService implements Disposable, IInterpreterService {
9494
}
9595

9696
public async refresh(resource?: Uri): Promise<void> {
97-
const activatedEnvLaunch = this.serviceContainer.get<IActivatedEnvironmentLaunch>(IActivatedEnvironmentLaunch);
98-
await activatedEnvLaunch.selectIfLaunchedViaActivatedEnv();
9997
const interpreterDisplay = this.serviceContainer.get<IInterpreterDisplay>(IInterpreterDisplay);
10098
await interpreterDisplay.refresh(resource);
10199
this.ensureEnvironmentContainsPython(this.configService.getSettings(resource).pythonPath).ignoreErrors();
@@ -182,6 +180,12 @@ export class InterpreterService implements Disposable, IInterpreterService {
182180
}
183181

184182
public async getActiveInterpreter(resource?: Uri): Promise<PythonEnvironment | undefined> {
183+
const activatedEnvLaunch = this.serviceContainer.get<IActivatedEnvironmentLaunch>(IActivatedEnvironmentLaunch);
184+
await activatedEnvLaunch.selectIfLaunchedViaActivatedEnv();
185+
// Config service also updates itself on interpreter config change,
186+
// so yielding control here to make sure it goes first and updates
187+
// itself before we can query it.
188+
await sleep(1);
185189
let path = this.configService.getSettings(resource).pythonPath;
186190
if (pathUtils.basename(path) === path) {
187191
// Value can be `python`, `python3`, `python3.9` etc.

src/client/interpreter/virtualEnvs/activatedEnvLaunch.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { ConfigurationTarget } from 'vscode';
66
import { IExtensionSingleActivationService } from '../../activation/types';
77
import { IApplicationShell, IWorkspaceService } from '../../common/application/types';
88
import { IProcessServiceFactory } from '../../common/process/types';
9+
import { sleep } from '../../common/utils/async';
10+
import { cache } from '../../common/utils/decorators';
911
import { Common, Interpreters } from '../../common/utils/localize';
1012
import { traceError, traceWarn } from '../../logging';
1113
import { Conda } from '../../pythonEnvironments/common/environmentManagers/conda';
@@ -82,13 +84,15 @@ export class ActivatedEnvironmentLaunch implements IExtensionSingleActivationSer
8284
}
8385
}
8486

87+
@cache(-1, true)
8588
public async selectIfLaunchedViaActivatedEnv(): Promise<void> {
89+
this.wasTriggered = true;
8690
const prefix = await this.getPrefixOfActivatedEnv();
8791
if (!prefix) {
8892
return;
8993
}
90-
this.wasTriggered = true;
9194
await this.setPrefixAsInterpeter(prefix);
95+
await sleep(1);
9296
}
9397

9498
private async setPrefixAsInterpeter(prefix: string) {

src/test/activation/activationManager.unit.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { FileSystem } from '../../client/common/platform/fileSystem';
1818
import { IFileSystem } from '../../client/common/platform/types';
1919
import { IDisposable, IInterpreterPathService } from '../../client/common/types';
2020
import { IInterpreterAutoSelectionService } from '../../client/interpreter/autoSelection/types';
21+
import { IActivatedEnvironmentLaunch } from '../../client/interpreter/contracts';
2122
import * as EnvFileTelemetry from '../../client/telemetry/envFileTelemetry';
2223
import { sleep } from '../core';
2324

@@ -43,8 +44,11 @@ suite('Activation Manager', () => {
4344
let activeResourceService: IActiveResourceService;
4445
let documentManager: typemoq.IMock<IDocumentManager>;
4546
let interpreterPathService: typemoq.IMock<IInterpreterPathService>;
47+
let activatedEnvLaunch: typemoq.IMock<IActivatedEnvironmentLaunch>;
4648
let fileSystem: IFileSystem;
4749
setup(() => {
50+
activatedEnvLaunch = typemoq.Mock.ofType<IActivatedEnvironmentLaunch>();
51+
activatedEnvLaunch.setup((a) => a.selectIfLaunchedViaActivatedEnv()).returns(() => Promise.resolve());
4852
interpreterPathService = typemoq.Mock.ofType<IInterpreterPathService>();
4953
interpreterPathService
5054
.setup((i) => i.copyOldInterpreterStorageValuesToNew(typemoq.It.isAny()))
@@ -70,6 +74,7 @@ suite('Activation Manager', () => {
7074
instance(fileSystem),
7175
instance(activeResourceService),
7276
interpreterPathService.object,
77+
activatedEnvLaunch.object,
7378
);
7479

7580
sinon.stub(EnvFileTelemetry, 'sendActivationTelemetry').resolves();
@@ -102,6 +107,7 @@ suite('Activation Manager', () => {
102107
instance(fileSystem),
103108
instance(activeResourceService),
104109
interpreterPathService.object,
110+
activatedEnvLaunch.object,
105111
);
106112
await managerTest.activateWorkspace(resource);
107113

@@ -132,6 +138,7 @@ suite('Activation Manager', () => {
132138
instance(fileSystem),
133139
instance(activeResourceService),
134140
interpreterPathService.object,
141+
activatedEnvLaunch.object,
135142
);
136143
await managerTest.activateWorkspace(resource);
137144

0 commit comments

Comments
 (0)