Skip to content

Commit

Permalink
Ignore cache when querying for interpreters during auto-selection (#1…
Browse files Browse the repository at this point in the history
…6734)

* Ignore cache when getting envs for autoselection

* Don't call autoSelectInterpreter twice
  • Loading branch information
kimadeline authored Jul 20, 2021
1 parent ae979b1 commit 0d910c6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 42 deletions.
1 change: 0 additions & 1 deletion src/client/activation/activationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ export class ExtensionActivationManager implements IExtensionActivationManager {
Promise.all(this.singleActivationServices.map((item) => item.activate())),
this.activateWorkspace(this.activeResourceService.getActiveResource()),
]);
await this.autoSelection.autoSelectInterpreter(undefined);
}

@traceDecorators.error('Failed to activate a workspace')
Expand Down
2 changes: 1 addition & 1 deletion src/client/interpreter/autoSelection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class InterpreterAutoSelectionService implements IInterpreterAutoSelectio
* As such, we can sort interpreters based on what it returns.
*/
private async autoselectInterpreterWithLocators(resource: Resource): Promise<void> {
const interpreters = await this.interpreterService.getInterpreters(resource);
const interpreters = await this.interpreterService.getInterpreters(resource, { ignoreCache: true });
const workspaceUri = this.interpreterHelper.getActiveWorkspaceUri(resource);

// When auto-selecting an intepreter for a workspace, we either want to return a local one
Expand Down
9 changes: 0 additions & 9 deletions src/test/activation/activationManager.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,25 +456,16 @@ suite('Activation Manager', () => {
.setup((s) => s.activate())
.returns(() => Promise.resolve())
.verifiable(typemoq.Times.once());
autoSelection
.setup((a) => a.autoSelectInterpreter(undefined))
.returns(() => Promise.resolve())
.verifiable(typemoq.Times.once());
when(activeResourceService.getActiveResource()).thenReturn(resource);
await managerTest.activate();
assert.ok(initialize.calledOnce);
assert.ok(activateWorkspace.calledOnce);
singleActivationService.verifyAll();
autoSelection.verifyAll();
});

test('Throws error if execution fails', async () => {
singleActivationService
.setup((s) => s.activate())
.returns(() => Promise.resolve())
.verifiable(typemoq.Times.once());
autoSelection
.setup((a) => a.autoSelectInterpreter(undefined))
.returns(() => Promise.reject(new Error('Kaboom')))
.verifiable(typemoq.Times.once());
when(activeResourceService.getActiveResource()).thenReturn(resource);
Expand Down
77 changes: 46 additions & 31 deletions src/test/interpreters/autoSelection/index.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ import {
IInterpreterAutoSelectionProxyService,
} from '../../../client/interpreter/autoSelection/types';
import { EnvironmentTypeComparer } from '../../../client/interpreter/configuration/environmentTypeComparer';
import { IInterpreterHelper, IInterpreterService, WorkspacePythonPath } from '../../../client/interpreter/contracts';
import {
GetInterpreterOptions,
IInterpreterHelper,
IInterpreterService,
WorkspacePythonPath,
} from '../../../client/interpreter/contracts';
import { InterpreterHelper } from '../../../client/interpreter/helpers';
import { InterpreterService } from '../../../client/interpreter/interpreterService';
import { EnvironmentType, PythonEnvironment } from '../../../client/pythonEnvironments/info';
Expand Down Expand Up @@ -203,25 +208,30 @@ suite('Interpreters - Auto Selection', () => {
envPath: path.join(workspacePath, '.venv'),
version: { major: 3, minor: 10, patch: 0 },
} as PythonEnvironment;

when(interpreterService.getInterpreters(resource)).thenResolve([
{
envType: EnvironmentType.Conda,
envPath: path.join('some', 'conda', 'env'),
version: { major: 3, minor: 7, patch: 2 },
} as PythonEnvironment,
{
envType: EnvironmentType.System,
envPath: path.join('/', 'usr', 'bin'),
version: { major: 3, minor: 9, patch: 1 },
} as PythonEnvironment,
localEnv,
]);
let options: GetInterpreterOptions = {};

when(interpreterService.getInterpreters(resource, anything())).thenCall((_, opts) => {
options = opts;
return Promise.resolve([
{
envType: EnvironmentType.Conda,
envPath: path.join('some', 'conda', 'env'),
version: { major: 3, minor: 7, patch: 2 },
} as PythonEnvironment,
{
envType: EnvironmentType.System,
envPath: path.join('/', 'usr', 'bin'),
version: { major: 3, minor: 9, patch: 1 },
} as PythonEnvironment,
localEnv,
]);
});

await autoSelectionService.autoSelectInterpreter(resource);

expect(eventFired).to.deep.equal(true, 'event not fired');
verify(interpreterService.getInterpreters(resource)).once();
expect(options).to.deep.equal({ ignoreCache: true }, 'getInterpreters options are different');
verify(interpreterService.getInterpreters(resource, anything())).once();
verify(state.updateValue(localEnv)).once();
});

Expand All @@ -231,25 +241,30 @@ suite('Interpreters - Auto Selection', () => {
envPath: path.join('/', 'usr', 'bin'),
version: { major: 3, minor: 9, patch: 1 },
} as PythonEnvironment;

when(interpreterService.getInterpreters(resource)).thenResolve([
{
envType: EnvironmentType.Conda,
envPath: path.join('some', 'conda', 'env'),
version: { major: 3, minor: 7, patch: 2 },
} as PythonEnvironment,
systemEnv,
{
envType: EnvironmentType.Pipenv,
envPath: path.join('some', 'pipenv', 'env'),
version: { major: 3, minor: 10, patch: 0 },
} as PythonEnvironment,
]);
let options: GetInterpreterOptions = {};

when(interpreterService.getInterpreters(resource, anything())).thenCall((_, opts) => {
options = opts;
return Promise.resolve([
{
envType: EnvironmentType.Conda,
envPath: path.join('some', 'conda', 'env'),
version: { major: 3, minor: 7, patch: 2 },
} as PythonEnvironment,
systemEnv,
{
envType: EnvironmentType.Pipenv,
envPath: path.join('some', 'pipenv', 'env'),
version: { major: 3, minor: 10, patch: 0 },
} as PythonEnvironment,
]);
});

await autoSelectionService.autoSelectInterpreter(resource);

expect(eventFired).to.deep.equal(true, 'event not fired');
verify(interpreterService.getInterpreters(resource)).once();
expect(options).to.deep.equal({ ignoreCache: true }, 'getInterpreters options are different');
verify(interpreterService.getInterpreters(resource, anything())).once();
verify(state.updateValue(systemEnv)).once();
});
});
Expand Down

0 comments on commit 0d910c6

Please sign in to comment.