From 47ada51fec15a776756351ab10bf8ad9f1d055a7 Mon Sep 17 00:00:00 2001 From: Zeev Rotshtein Date: Wed, 6 Mar 2019 03:27:35 +0200 Subject: [PATCH] Pass VIRTUAL_ENV to linter's environment when in a virtual env --- src/client/common/process/pythonToolService.ts | 4 ++-- src/client/linters/baseLinter.ts | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/client/common/process/pythonToolService.ts b/src/client/common/process/pythonToolService.ts index d4b2ccaaa8bb..c162741f762d 100644 --- a/src/client/common/process/pythonToolService.ts +++ b/src/client/common/process/pythonToolService.ts @@ -12,7 +12,7 @@ export class PythonToolExecutionService implements IPythonToolExecutionService { constructor(@inject(IServiceContainer) private serviceContainer: IServiceContainer) { } public async execObservable(executionInfo: ExecutionInfo, options: SpawnOptions, resource: Uri): Promise> { if (options.env) { - throw new Error('Environment variables are not supported'); + Object.assign(env, process.env); } if (executionInfo.moduleName && executionInfo.moduleName.length > 0) { const pythonExecutionService = await this.serviceContainer.get(IPythonExecutionFactory).create({ resource }); @@ -24,7 +24,7 @@ export class PythonToolExecutionService implements IPythonToolExecutionService { } public async exec(executionInfo: ExecutionInfo, options: SpawnOptions, resource: Uri): Promise> { if (options.env) { - throw new Error('Environment variables are not supported'); + Object.assign(env, process.env); } if (executionInfo.moduleName && executionInfo.moduleName.length > 0) { const pythonExecutionService = await this.serviceContainer.get(IPythonExecutionFactory).create({ resource }); diff --git a/src/client/linters/baseLinter.ts b/src/client/linters/baseLinter.ts index 2d6df190bff2..54339b43d086 100644 --- a/src/client/linters/baseLinter.ts +++ b/src/client/linters/baseLinter.ts @@ -15,6 +15,7 @@ import { ILinter, ILinterInfo, ILinterManager, ILintMessage, LinterId, LintMessageSeverity } from './types'; +import { IInterpreterService, InterpreterType } from '../interpreter/contracts'; // tslint:disable-next-line:no-require-imports no-var-requires no-any const namedRegexp = require('named-js-regexp'); @@ -72,6 +73,7 @@ export abstract class BaseLinter implements ILinter { private _pythonSettings!: IPythonSettings; private _info: ILinterInfo; private workspace: IWorkspaceService; + private interpreterService: IInterpreterService; protected get pythonSettings(): IPythonSettings { return this._pythonSettings; @@ -85,6 +87,7 @@ export abstract class BaseLinter implements ILinter { this.errorHandler = new ErrorHandler(this.info.product, outputChannel, serviceContainer); this.configService = serviceContainer.get(IConfigurationService); this.workspace = serviceContainer.get(IWorkspaceService); + this.interpreterService = serviceContainer.get(IInterpreterService); } public get info(): ILinterInfo { @@ -138,7 +141,12 @@ export abstract class BaseLinter implements ILinter { const cwd = this.getWorkspaceRootPath(document); const pythonToolsExecutionService = this.serviceContainer.get(IPythonToolExecutionService); try { - const result = await pythonToolsExecutionService.exec(executionInfo, { cwd, token: cancellation, mergeStdOutErr: false }, document.uri); + let myEnv = {}; + const activeInterpreter = await this.interpreterService.getActiveInterpreter(document.uri); + if (activeInterpreter.type !== InterpreterType.Unknown) { + myEnv = { VIRTUAL_ENV: activeInterpreter.sysPrefix }; // For some reason activeInterpreter.envPath doesn't work + } + const result = await pythonToolsExecutionService.exec(executionInfo, { cwd, token: cancellation, mergeStdOutErr: false, env: myEnv }, document.uri); this.displayLinterResultHeader(result.stdout); return await this.parseMessages(result.stdout, document, cancellation, regEx); } catch (error) {