Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kartik Raj committed Jan 12, 2023
1 parent 9461f4a commit 30f7096
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 9 deletions.
35 changes: 33 additions & 2 deletions src/client/interpreter/activation/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { IPlatformService } from '../../common/platform/types';
import * as internalScripts from '../../common/process/internal/scripts';
import { ExecutionResult, IProcessServiceFactory } from '../../common/process/types';
import { ITerminalHelper, TerminalShellType } from '../../common/terminal/types';
import { ICurrentProcess, IDisposable, Resource } from '../../common/types';
import { ICurrentProcess, IDisposable, IExtensionContext, Resource } from '../../common/types';
import { sleep } from '../../common/utils/async';
import { InMemoryCache } from '../../common/utils/cacheUtils';
import { OSType } from '../../common/utils/platform';
Expand Down Expand Up @@ -102,6 +102,7 @@ export class EnvironmentActivationServiceCache {
@injectable()
export class EnvironmentActivationService implements IEnvironmentActivationService, IDisposable {
private readonly disposables: IDisposable[] = [];
private previousEnvVars = process.env;
private readonly activatedEnvVariablesCache = new EnvironmentActivationServiceCache();
constructor(
@inject(ITerminalHelper) private readonly helper: ITerminalHelper,
Expand All @@ -111,6 +112,7 @@ export class EnvironmentActivationService implements IEnvironmentActivationServi
@inject(IWorkspaceService) private workspace: IWorkspaceService,
@inject(IInterpreterService) private interpreterService: IInterpreterService,
@inject(IEnvironmentVariablesProvider) private readonly envVarsService: IEnvironmentVariablesProvider,
@inject(IExtensionContext) private context: IExtensionContext,
) {
this.envVarsService.onDidEnvironmentVariablesChange(
() => this.activatedEnvVariablesCache.clear(),
Expand All @@ -119,10 +121,15 @@ export class EnvironmentActivationService implements IEnvironmentActivationServi
);

this.interpreterService.onDidChangeInterpreter(
() => this.activatedEnvVariablesCache.clear(),
async (resource) => {
this.activatedEnvVariablesCache.clear();
await this.initializeEnvironmentCollection(resource);
},
this,
this.disposables,
);

this.initializeEnvironmentCollection(undefined).ignoreErrors();
}

public dispose(): void {
Expand Down Expand Up @@ -322,4 +329,28 @@ export class EnvironmentActivationService implements IEnvironmentActivationServi
const js = output.substring(output.indexOf('{')).trim();
return parse(js);
}

private async initializeEnvironmentCollection(resource: Resource) {
const env = await this.getActivatedEnvironmentVariables(resource);
if (!env) {
this.context.environmentVariableCollection.clear();
this.previousEnvVars = process.env;
return;
}
const previousEnv = this.previousEnvVars;
Object.keys(previousEnv).forEach((key) => {
// If the previous env var is not in the current env, then explicitly add it so it can cleared later.
if (!(key in env)) {
env[key] = '';
}
});
this.previousEnvVars = env;
for (const key in env) {
const value = env[key];
const prevValue = previousEnv[key];
if (value !== undefined && prevValue !== value) {
this.context.environmentVariableCollection.replace(key, value);
}
}
}
}
2 changes: 1 addition & 1 deletion src/client/interpreter/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export interface IInterpreterService {
readonly refreshPromise: Promise<void> | undefined;
readonly onDidChangeInterpreters: Event<PythonEnvironmentsChangedEvent>;
onDidChangeInterpreterConfiguration: Event<Uri | undefined>;
onDidChangeInterpreter: Event<void>;
onDidChangeInterpreter: Event<Uri | undefined>;
onDidChangeInterpreterInformation: Event<PythonEnvironment>;
/**
* Note this API does not trigger the refresh but only works with the current refresh if any. Information
Expand Down
6 changes: 3 additions & 3 deletions src/client/interpreter/interpreterService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class InterpreterService implements Disposable, IInterpreterService {
return this.pyenvs.getRefreshPromise();
}

public get onDidChangeInterpreter(): Event<void> {
public get onDidChangeInterpreter(): Event<Uri | undefined> {
return this.didChangeInterpreterEmitter.event;
}

Expand All @@ -79,7 +79,7 @@ export class InterpreterService implements Disposable, IInterpreterService {

private readonly interpreterPathService: IInterpreterPathService;

private readonly didChangeInterpreterEmitter = new EventEmitter<void>();
private readonly didChangeInterpreterEmitter = new EventEmitter<Uri | undefined>();

private readonly didChangeInterpreterInformation = new EventEmitter<PythonEnvironment>();

Expand Down Expand Up @@ -219,7 +219,7 @@ export class InterpreterService implements Disposable, IInterpreterService {
this.didChangeInterpreterConfigurationEmitter.fire(resource);
if (this._pythonPathSetting === '' || this._pythonPathSetting !== pySettings.pythonPath) {
this._pythonPathSetting = pySettings.pythonPath;
this.didChangeInterpreterEmitter.fire();
this.didChangeInterpreterEmitter.fire(resource);
reportActiveInterpreterChanged({
path: pySettings.pythonPath,
resource: this.serviceContainer.get<IWorkspaceService>(IWorkspaceService).getWorkspaceFolder(resource),
Expand Down
2 changes: 1 addition & 1 deletion src/client/jupyter/jupyterIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type PythonApiForJupyterExtension = {
/**
* IInterpreterService
*/
onDidChangeInterpreter: Event<void>;
onDidChangeInterpreter: Event<Uri | undefined>;
/**
* IInterpreterService
*/
Expand Down
5 changes: 3 additions & 2 deletions src/test/linters/lint.provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
IPersistentStateFactory,
IPythonSettings,
Product,
Resource,
WORKSPACE_MEMENTO,
} from '../../client/common/types';
import { createDeferred } from '../../client/common/utils/async';
Expand Down Expand Up @@ -171,12 +172,12 @@ suite('Linting - Provider', () => {
});

test('Lint on change interpreters', async () => {
const e = new vscode.EventEmitter<void>();
const e = new vscode.EventEmitter<Resource>();
interpreterService.setup((x) => x.onDidChangeInterpreter).returns(() => e.event);

const linterProvider = new LinterProvider(serviceContainer);
await linterProvider.activate();
e.fire();
e.fire(undefined);
engine.verify((x) => x.lintOpenPythonFiles(), TypeMoq.Times.once());
});

Expand Down

0 comments on commit 30f7096

Please sign in to comment.