Skip to content

Commit 1499a8f

Browse files
authored
Revert "Move .env file and environment variable parser into the extension (#6773)" (#6794)
This reverts commit e095693.
1 parent a016401 commit 1499a8f

File tree

8 files changed

+53
-67
lines changed

8 files changed

+53
-67
lines changed

news/3 Code Health/6770.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/client/debugger/debugAdapter/DebugClients/LocalDebugClient.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ import * as path from 'path';
33
import { DebugSession, OutputEvent } from 'vscode-debugadapter';
44
import { DebugProtocol } from 'vscode-debugprotocol';
55
import { open } from '../../../common/open';
6+
import { IS_WINDOWS } from '../../../common/platform/constants';
7+
import { PathUtils } from '../../../common/platform/pathUtils';
8+
import { CurrentProcess } from '../../../common/process/currentProcess';
69
import { noop } from '../../../common/utils/misc';
10+
import { EnvironmentVariablesService } from '../../../common/variables/environment';
711
import { IServiceContainer } from '../../../ioc/types';
812
import { LaunchRequestArguments } from '../../types';
913
import { IDebugServer } from '../Common/Contracts';
1014
import { BaseDebugServer } from '../DebugServers/BaseDebugServer';
1115
import { LocalDebugServerV2 } from '../DebugServers/LocalDebugServerV2';
1216
import { ILocalDebugLauncherScriptProvider } from '../types';
1317
import { DebugClient, DebugType } from './DebugClient';
18+
import { DebugClientHelper } from './helper';
1419

1520
enum DebugServerStatus {
1621
Unknown = 1,
@@ -62,6 +67,11 @@ export class LocalDebugClient extends DebugClient<LaunchRequestArguments> {
6267
}
6368
// tslint:disable-next-line:max-func-body-length member-ordering no-any
6469
public async LaunchApplicationToDebug(dbgServer: IDebugServer): Promise<any> {
70+
const pathUtils = new PathUtils(IS_WINDOWS);
71+
const currentProcess = new CurrentProcess();
72+
const environmentVariablesService = new EnvironmentVariablesService(pathUtils);
73+
const helper = new DebugClientHelper(environmentVariablesService, pathUtils, currentProcess);
74+
const environmentVariables = await helper.getEnvironmentVariables(this.args);
6575
// tslint:disable-next-line:max-func-body-length cyclomatic-complexity no-any
6676
return new Promise<any>((resolve, reject) => {
6777
const fileDir = this.args && this.args.program ? path.dirname(this.args.program) : '';
@@ -74,16 +84,15 @@ export class LocalDebugClient extends DebugClient<LaunchRequestArguments> {
7484
pythonPath = this.args.pythonPath;
7585
}
7686
const args = this.buildLaunchArguments(processCwd, dbgServer.port);
77-
const envVars = this.args.env ? { ...this.args.env } : {};
7887
switch (this.args.console) {
7988
case 'externalTerminal':
8089
case 'integratedTerminal': {
8190
const isSudo = Array.isArray(this.args.debugOptions) && this.args.debugOptions.some(opt => opt === 'Sudo');
82-
this.launchExternalTerminal(isSudo, processCwd, pythonPath, args, envVars).then(resolve).catch(reject);
91+
this.launchExternalTerminal(isSudo, processCwd, pythonPath, args, environmentVariables).then(resolve).catch(reject);
8392
break;
8493
}
8594
default: {
86-
this.pyProc = spawn(pythonPath, args, { cwd: processCwd, env: envVars });
95+
this.pyProc = spawn(pythonPath, args, { cwd: processCwd, env: environmentVariables });
8796
this.handleProcessOutput(this.pyProc!, reject);
8897

8998
// Here we wait for the application to connect to the socket server.

src/client/debugger/extension/configuration/resolvers/helper.ts renamed to src/client/debugger/debugAdapter/DebugClients/helper.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
2-
// Licensed under the MIT License.
1+
import { ICurrentProcess, IPathUtils } from '../../../common/types';
2+
import { EnvironmentVariables, IEnvironmentVariablesService } from '../../../common/variables/types';
3+
import { LaunchRequestArguments } from '../../types';
34

4-
'use strict';
5-
6-
import { inject, injectable } from 'inversify';
7-
import { ICurrentProcess, IPathUtils } from '../../../../common/types';
8-
import { EnvironmentVariables, IEnvironmentVariablesService } from '../../../../common/variables/types';
9-
import { LaunchRequestArguments } from '../../../types';
10-
11-
export const IDebugEnvironmentVariablesService = Symbol('IDebugEnvironmentVariablesService');
12-
export interface IDebugEnvironmentVariablesService {
13-
getEnvironmentVariables(args: LaunchRequestArguments): Promise<EnvironmentVariables>;
14-
}
15-
16-
@injectable()
17-
export class DebugEnvironmentVariablesHelper implements IDebugEnvironmentVariablesService {
18-
constructor(
19-
@inject(IEnvironmentVariablesService) private envParser: IEnvironmentVariablesService,
20-
@inject(IPathUtils) private pathUtils: IPathUtils,
21-
@inject(ICurrentProcess) private process: ICurrentProcess
22-
) { }
5+
export class DebugClientHelper {
6+
constructor(private envParser: IEnvironmentVariablesService, private pathUtils: IPathUtils,
7+
private process: ICurrentProcess) { }
238
public async getEnvironmentVariables(args: LaunchRequestArguments): Promise<EnvironmentVariables> {
249
const pathVariableName = this.pathUtils.getPathVariableName();
2510

src/client/debugger/extension/configuration/resolvers/launch.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { IConfigurationService } from '../../../../common/types';
1313
import { DebuggerTypeName } from '../../../constants';
1414
import { DebugOptions, LaunchRequestArguments } from '../../../types';
1515
import { BaseConfigurationResolver } from './base';
16-
import { IDebugEnvironmentVariablesService } from './helper';
1716

1817
@injectable()
1918
export class LaunchConfigurationResolver extends BaseConfigurationResolver<LaunchRequestArguments> {
@@ -22,8 +21,7 @@ export class LaunchConfigurationResolver extends BaseConfigurationResolver<Launc
2221
@inject(IDocumentManager) documentManager: IDocumentManager,
2322
@inject(IDiagnosticsService) @named(InvalidPythonPathInDebuggerServiceId) private readonly invalidPythonPathInDebuggerService: IInvalidPythonPathInDebuggerService,
2423
@inject(IPlatformService) private readonly platformService: IPlatformService,
25-
@inject(IConfigurationService) configurationService: IConfigurationService,
26-
@inject(IDebugEnvironmentVariablesService) private readonly debugEnvHelper: IDebugEnvironmentVariablesService
24+
@inject(IConfigurationService) configurationService: IConfigurationService
2725
) {
2826
super(workspaceService, documentManager, configurationService);
2927
}
@@ -65,10 +63,6 @@ export class LaunchConfigurationResolver extends BaseConfigurationResolver<Launc
6563
const settings = this.configurationService.getSettings(workspaceFolder);
6664
debugConfiguration.envFile = settings.envFile;
6765
}
68-
// Extract environment variables from .env file in the vscode context and
69-
// set the "env" debug configuration argument. This expansion should be
70-
// done here before handing of the environment settings to the debug adapter
71-
debugConfiguration.env = await this.debugEnvHelper.getEnvironmentVariables(debugConfiguration);
7266
if (typeof debugConfiguration.stopOnEntry !== 'boolean') {
7367
debugConfiguration.stopOnEntry = false;
7468
}

src/client/debugger/extension/serviceRegistry.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { DebugConfigurationProviderFactory } from './configuration/providers/pro
1818
import { PyramidLaunchDebugConfigurationProvider } from './configuration/providers/pyramidLaunch';
1919
import { RemoteAttachDebugConfigurationProvider } from './configuration/providers/remoteAttach';
2020
import { AttachConfigurationResolver } from './configuration/resolvers/attach';
21-
import { DebugEnvironmentVariablesHelper, IDebugEnvironmentVariablesService } from './configuration/resolvers/helper';
2221
import { LaunchConfigurationResolver } from './configuration/resolvers/launch';
2322
import { IDebugConfigurationProviderFactory, IDebugConfigurationResolver } from './configuration/types';
2423
import { ChildProcessAttachEventHandler } from './hooks/childProcessAttachHandler';
@@ -42,5 +41,4 @@ export function registerTypes(serviceManager: IServiceManager) {
4241
serviceManager.addSingleton<IDebugConfigurationProvider>(IDebugConfigurationProvider, RemoteAttachDebugConfigurationProvider, DebugConfigurationType.remoteAttach);
4342
serviceManager.addSingleton<IDebugConfigurationProvider>(IDebugConfigurationProvider, ModuleLaunchDebugConfigurationProvider, DebugConfigurationType.launchModule);
4443
serviceManager.addSingleton<IDebugConfigurationProvider>(IDebugConfigurationProvider, PyramidLaunchDebugConfigurationProvider, DebugConfigurationType.launchPyramid);
45-
serviceManager.addSingleton<IDebugEnvironmentVariablesService>(IDebugEnvironmentVariablesService, DebugEnvironmentVariablesHelper);
4644
}

src/test/debugger/envVars.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as path from 'path';
99
import * as shortid from 'shortid';
1010
import { ICurrentProcess, IPathUtils } from '../../client/common/types';
1111
import { IEnvironmentVariablesService } from '../../client/common/variables/types';
12-
import { DebugEnvironmentVariablesHelper, IDebugEnvironmentVariablesService } from '../../client/debugger/extension/configuration/resolvers/helper';
12+
import { DebugClientHelper } from '../../client/debugger/debugAdapter/DebugClients/helper';
1313
import { ConsoleType, LaunchRequestArguments } from '../../client/debugger/types';
1414
import { isOs, OSType } from '../common';
1515
import { closeActiveWindows, initialize, initializeTest, IS_MULTI_ROOT_TEST, TEST_DEBUGGER } from '../initialize';
@@ -19,7 +19,7 @@ use(chaiAsPromised);
1919

2020
suite('Resolving Environment Variables when Debugging', () => {
2121
let ioc: UnitTestIocContainer;
22-
let debugEnvParser: IDebugEnvironmentVariablesService;
22+
let helper: DebugClientHelper;
2323
let pathVariableName: string;
2424
let mockProcess: ICurrentProcess;
2525

@@ -37,7 +37,7 @@ suite('Resolving Environment Variables when Debugging', () => {
3737
const envParser = ioc.serviceContainer.get<IEnvironmentVariablesService>(IEnvironmentVariablesService);
3838
const pathUtils = ioc.serviceContainer.get<IPathUtils>(IPathUtils);
3939
mockProcess = ioc.serviceContainer.get<ICurrentProcess>(ICurrentProcess);
40-
debugEnvParser = new DebugEnvironmentVariablesHelper(envParser, pathUtils, mockProcess);
40+
helper = new DebugClientHelper(envParser, pathUtils, mockProcess);
4141
pathVariableName = pathUtils.getPathVariableName();
4242
});
4343
suiteTeardown(closeActiveWindows);
@@ -60,7 +60,7 @@ suite('Resolving Environment Variables when Debugging', () => {
6060
// tslint:disable-next-line:no-any
6161
} as any as LaunchRequestArguments;
6262

63-
const envVars = await debugEnvParser.getEnvironmentVariables(args);
63+
const envVars = await helper.getEnvironmentVariables(args);
6464
expect(envVars).not.be.undefined;
6565
expect(Object.keys(envVars)).lengthOf(expectedNumberOfVariables, 'Incorrect number of variables');
6666
expect(envVars).to.have.property('PYTHONUNBUFFERED', '1', 'Property not found');
@@ -97,7 +97,7 @@ suite('Resolving Environment Variables when Debugging', () => {
9797
// tslint:disable-next-line:no-any
9898
} as any as LaunchRequestArguments;
9999

100-
const envVars = await debugEnvParser.getEnvironmentVariables(args);
100+
const envVars = await helper.getEnvironmentVariables(args);
101101

102102
// tslint:disable-next-line:no-unused-expression chai-vague-errors
103103
expect(envVars).not.be.undefined;
@@ -154,7 +154,7 @@ suite('Resolving Environment Variables when Debugging', () => {
154154
console, env
155155
} as any as LaunchRequestArguments;
156156

157-
const envVars = await debugEnvParser.getEnvironmentVariables(args);
157+
const envVars = await helper.getEnvironmentVariables(args);
158158
expect(envVars).not.be.undefined;
159159
expect(Object.keys(envVars)).lengthOf(expectedNumberOfVariables, 'Incorrect number of variables');
160160
expect(envVars).to.have.property('PYTHONPATH');

src/test/debugger/extension/configuration/resolvers/launch.unit.test.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,31 @@ import { expect } from 'chai';
99
import * as path from 'path';
1010
import * as TypeMoq from 'typemoq';
1111
import { DebugConfiguration, DebugConfigurationProvider, TextDocument, TextEditor, Uri, WorkspaceFolder } from 'vscode';
12-
import { IInvalidPythonPathInDebuggerService } from '../../../../../client/application/diagnostics/types';
13-
import { IDocumentManager, IWorkspaceService } from '../../../../../client/common/application/types';
12+
import { InvalidPythonPathInDebuggerServiceId } from '../../../../../client/application/diagnostics/checks/invalidPythonPathInDebugger';
13+
import { IDiagnosticsService, IInvalidPythonPathInDebuggerService } from '../../../../../client/application/diagnostics/types';
14+
import { IApplicationShell, IDocumentManager, IWorkspaceService } from '../../../../../client/common/application/types';
1415
import { PYTHON_LANGUAGE } from '../../../../../client/common/constants';
15-
import { IPlatformService } from '../../../../../client/common/platform/types';
16+
import { IFileSystem, IPlatformService } from '../../../../../client/common/platform/types';
1617
import { IPythonExecutionFactory, IPythonExecutionService } from '../../../../../client/common/process/types';
17-
import { IConfigurationService, IPythonSettings } from '../../../../../client/common/types';
18+
import { IConfigurationService, ILogger, IPythonSettings } from '../../../../../client/common/types';
1819
import { DebuggerTypeName } from '../../../../../client/debugger/constants';
19-
import { IDebugEnvironmentVariablesService } from '../../../../../client/debugger/extension/configuration/resolvers/helper';
2020
import { LaunchConfigurationResolver } from '../../../../../client/debugger/extension/configuration/resolvers/launch';
2121
import { DebugOptions, LaunchRequestArguments } from '../../../../../client/debugger/types';
2222
import { IInterpreterHelper } from '../../../../../client/interpreter/contracts';
23+
import { IServiceContainer } from '../../../../../client/ioc/types';
2324

2425
suite('Debugging - Config Resolver Launch', () => {
26+
let serviceContainer: TypeMoq.IMock<IServiceContainer>;
2527
let debugProvider: DebugConfigurationProvider;
2628
let platformService: TypeMoq.IMock<IPlatformService>;
29+
let fileSystem: TypeMoq.IMock<IFileSystem>;
30+
let appShell: TypeMoq.IMock<IApplicationShell>;
2731
let pythonExecutionService: TypeMoq.IMock<IPythonExecutionService>;
32+
let logger: TypeMoq.IMock<ILogger>;
2833
let helper: TypeMoq.IMock<IInterpreterHelper>;
2934
let workspaceService: TypeMoq.IMock<IWorkspaceService>;
3035
let documentManager: TypeMoq.IMock<IDocumentManager>;
3136
let diagnosticsService: TypeMoq.IMock<IInvalidPythonPathInDebuggerService>;
32-
let debugEnvHelper: TypeMoq.IMock<IDebugEnvironmentVariablesService>;
3337
function createMoqWorkspaceFolder(folderPath: string) {
3438
const folder = TypeMoq.Mock.ofType<WorkspaceFolder>();
3539
folder.setup(f => f.uri).returns(() => Uri.file(folderPath));
@@ -39,10 +43,13 @@ suite('Debugging - Config Resolver Launch', () => {
3943
const confgService = TypeMoq.Mock.ofType<IConfigurationService>();
4044
workspaceService = TypeMoq.Mock.ofType<IWorkspaceService>();
4145
documentManager = TypeMoq.Mock.ofType<IDocumentManager>();
46+
serviceContainer = TypeMoq.Mock.ofType<IServiceContainer>();
4247

4348
platformService = TypeMoq.Mock.ofType<IPlatformService>();
49+
fileSystem = TypeMoq.Mock.ofType<IFileSystem>();
50+
appShell = TypeMoq.Mock.ofType<IApplicationShell>();
51+
logger = TypeMoq.Mock.ofType<ILogger>();
4452
diagnosticsService = TypeMoq.Mock.ofType<IInvalidPythonPathInDebuggerService>();
45-
debugEnvHelper = TypeMoq.Mock.ofType<IDebugEnvironmentVariablesService>();
4653

4754
pythonExecutionService = TypeMoq.Mock.ofType<IPythonExecutionService>();
4855
helper = TypeMoq.Mock.ofType<IInterpreterHelper>();
@@ -54,22 +61,24 @@ suite('Debugging - Config Resolver Launch', () => {
5461
.setup(h => h.validatePythonPath(TypeMoq.It.isAny(), TypeMoq.It.isAny(), TypeMoq.It.isAny()))
5562
.returns(() => Promise.resolve(true));
5663

64+
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IPythonExecutionFactory))).returns(() => factory.object);
65+
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IConfigurationService))).returns(() => confgService.object);
66+
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IPlatformService))).returns(() => platformService.object);
67+
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IFileSystem))).returns(() => fileSystem.object);
68+
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IApplicationShell))).returns(() => appShell.object);
69+
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(ILogger))).returns(() => logger.object);
70+
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IInterpreterHelper))).returns(() => helper.object);
71+
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IDiagnosticsService), TypeMoq.It.isValue(InvalidPythonPathInDebuggerServiceId))).returns(() => diagnosticsService.object);
72+
5773
const settings = TypeMoq.Mock.ofType<IPythonSettings>();
5874
settings.setup(s => s.pythonPath).returns(() => pythonPath);
5975
if (workspaceFolder) {
6076
settings.setup(s => s.envFile).returns(() => path.join(workspaceFolder!.uri.fsPath, '.env2'));
6177
}
6278
confgService.setup(c => c.getSettings(TypeMoq.It.isAny())).returns(() => settings.object);
6379
setupOs(isWindows, isMac, isLinux);
64-
debugEnvHelper.setup(x => x.getEnvironmentVariables(TypeMoq.It.isAny())).returns(() => Promise.resolve({}));
65-
66-
debugProvider = new LaunchConfigurationResolver(
67-
workspaceService.object,
68-
documentManager.object,
69-
diagnosticsService.object,
70-
platformService.object,
71-
confgService.object,
72-
debugEnvHelper.object);
80+
81+
debugProvider = new LaunchConfigurationResolver(workspaceService.object, documentManager.object, diagnosticsService.object, platformService.object, confgService.object);
7382
}
7483
function setupActiveEditor(fileName: string | undefined, languageId: string) {
7584
if (fileName) {
@@ -82,10 +91,12 @@ suite('Debugging - Config Resolver Launch', () => {
8291
} else {
8392
documentManager.setup(d => d.activeTextEditor).returns(() => undefined);
8493
}
94+
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IDocumentManager))).returns(() => documentManager.object);
8595
}
8696
function setupWorkspaces(folders: string[]) {
8797
const workspaceFolders = folders.map(createMoqWorkspaceFolder);
8898
workspaceService.setup(w => w.workspaceFolders).returns(() => workspaceFolders);
99+
serviceContainer.setup(c => c.get(TypeMoq.It.isValue(IWorkspaceService))).returns(() => workspaceService.object);
89100
}
90101
function setupOs(isWindows: boolean, isMac: boolean, isLinux: boolean) {
91102
platformService.setup(p => p.isWindows).returns(() => isWindows);

0 commit comments

Comments
 (0)