Skip to content

Commit 269571b

Browse files
remove inject
1 parent 3d6a6e6 commit 269571b

File tree

9 files changed

+143
-184
lines changed

9 files changed

+143
-184
lines changed

.vscode/launch.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
"name": "Unit Tests",
2222
"type": "extensionHost",
2323
"request": "launch",
24+
"runtimeExecutable": "${execPath}",
2425
"args": [
26+
"./out/test/**/*.unit.test.js",
2527
"--extensionDevelopmentPath=${workspaceFolder}",
26-
"--extensionTestsPath=${workspaceFolder}/out/test/index"
28+
"--extensionTestsPath=${workspaceFolder}/out/test/unittest/index"
2729
],
2830
"outFiles": [
2931
"${workspaceFolder}/out/**/*.js",
30-
"${workspaceFolder}/dist/**/*.js"
3132
],
3233
"preLaunchTask": "tasks: watch-tests"
3334
},

.vscode/tasks.json

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@
1818
}
1919
},
2020
{
21-
"type": "npm",
22-
"script": "watch-tests",
23-
"problemMatcher": "$tsc-watch",
24-
"isBackground": true,
25-
"presentation": {
26-
"reveal": "never",
27-
"group": "watchers"
28-
},
29-
"group": "build"
30-
},
31-
{
32-
"label": "tasks: watch-tests",
33-
"dependsOn": ["npm: watch", "npm: watch-tests"],
34-
"problemMatcher": []
35-
}
21+
"type": "npm",
22+
"script": "watch-tests",
23+
"problemMatcher": "$tsc-watch",
24+
"isBackground": true,
25+
"presentation": {
26+
"reveal": "never",
27+
"group": "watchers"
28+
},
29+
"group": "build"
30+
},
31+
{
32+
"label": "tasks: watch-tests",
33+
"dependsOn": [
34+
"npm: watch",
35+
"npm: watch-tests"
36+
],
37+
"problemMatcher": []
38+
}
3639
]
3740
}

src/extension/common/variables/environment.ts

Lines changed: 70 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -7,102 +7,92 @@ import { sendTelemetryEvent } from '../../telemetry';
77
import { EventName } from '../../telemetry/constants';
88
import { traceError } from '../log/logging';
99
import { getSearchPathEnvVarNames } from '../utils/exec';
10-
import { EnvironmentVariables, IEnvironmentVariablesService } from './types';
10+
import { EnvironmentVariables } from './types';
1111

12-
export class EnvironmentVariablesService implements IEnvironmentVariablesService {
13-
private _pathVariable?: 'Path' | 'PATH';
14-
constructor() {}
1512

16-
public async parseFile(
17-
filePath?: string,
18-
baseVars?: EnvironmentVariables,
19-
): Promise<EnvironmentVariables | undefined> {
20-
if (!filePath || !(await fs.pathExists(filePath))) {
21-
return;
22-
}
23-
const contents = await fs.readFile(filePath).catch((ex) => {
24-
traceError('Custom .env is likely not pointing to a valid file', ex);
25-
return undefined;
26-
});
27-
if (!contents) {
28-
return;
29-
}
30-
return parseEnvFile(contents, baseVars);
13+
export async function parseFile(
14+
filePath?: string,
15+
baseVars?: EnvironmentVariables,
16+
): Promise<EnvironmentVariables | undefined> {
17+
if (!filePath || !(await fs.pathExists(filePath))) {
18+
return;
3119
}
32-
33-
public parseFileSync(filePath?: string, baseVars?: EnvironmentVariables): EnvironmentVariables | undefined {
34-
if (!filePath || !fs.pathExistsSync(filePath)) {
35-
return;
36-
}
37-
let contents: string | undefined;
38-
try {
39-
contents = fs.readFileSync(filePath, { encoding: 'utf8' });
40-
} catch (ex) {
41-
traceError('Custom .env is likely not pointing to a valid file', ex);
42-
}
43-
if (!contents) {
44-
return;
45-
}
46-
return parseEnvFile(contents, baseVars);
20+
const contents = await fs.readFile(filePath).catch((ex) => {
21+
traceError('Custom .env is likely not pointing to a valid file', ex);
22+
return undefined;
23+
});
24+
if (!contents) {
25+
return;
4726
}
27+
return parseEnvFile(contents, baseVars);
28+
}
4829

49-
public mergeVariables(
50-
source: EnvironmentVariables,
51-
target: EnvironmentVariables,
52-
options?: { overwrite?: boolean },
53-
) {
54-
if (!target) {
55-
return;
56-
}
57-
const settingsNotToMerge = ['PYTHONPATH', this.pathVariable];
58-
Object.keys(source).forEach((setting) => {
59-
if (settingsNotToMerge.indexOf(setting) >= 0) {
60-
return;
61-
}
62-
if (target[setting] === undefined || options?.overwrite) {
63-
target[setting] = source[setting];
64-
}
65-
});
30+
export function parseFileSync(filePath?: string, baseVars?: EnvironmentVariables): EnvironmentVariables | undefined {
31+
if (!filePath || !fs.pathExistsSync(filePath)) {
32+
return;
6633
}
67-
68-
public appendPythonPath(vars: EnvironmentVariables, ...pythonPaths: string[]) {
69-
return this.appendPaths(vars, 'PYTHONPATH', ...pythonPaths);
34+
let contents: string | undefined;
35+
try {
36+
contents = fs.readFileSync(filePath, { encoding: 'utf8' });
37+
} catch (ex) {
38+
traceError('Custom .env is likely not pointing to a valid file', ex);
7039
}
71-
72-
public appendPath(vars: EnvironmentVariables, ...paths: string[]) {
73-
return this.appendPaths(vars, this.pathVariable, ...paths);
40+
if (!contents) {
41+
return;
7442
}
43+
return parseEnvFile(contents, baseVars);
44+
}
7545

76-
private get pathVariable(): 'Path' | 'PATH' {
77-
if (!this._pathVariable) {
78-
this._pathVariable = getSearchPathEnvVarNames()[0];
79-
}
80-
return this._pathVariable!;
46+
export function mergeVariables(
47+
source: EnvironmentVariables,
48+
target: EnvironmentVariables,
49+
options?: { overwrite?: boolean },
50+
) {
51+
if (!target) {
52+
return;
8153
}
82-
83-
private appendPaths(
84-
vars: EnvironmentVariables,
85-
variableName: 'PATH' | 'Path' | 'PYTHONPATH',
86-
...pathsToAppend: string[]
87-
) {
88-
const valueToAppend = pathsToAppend
89-
.filter((item) => typeof item === 'string' && item.trim().length > 0)
90-
.map((item) => item.trim())
91-
.join(path.delimiter);
92-
if (valueToAppend.length === 0) {
93-
return vars;
54+
const settingsNotToMerge = ['PYTHONPATH', getSearchPathEnvVarNames()[0]];
55+
Object.keys(source).forEach((setting) => {
56+
if (settingsNotToMerge.indexOf(setting) >= 0) {
57+
return;
9458
}
95-
96-
const variable = vars ? vars[variableName] : undefined;
97-
if (variable && typeof variable === 'string' && variable.length > 0) {
98-
vars[variableName] = variable + path.delimiter + valueToAppend;
99-
} else {
100-
vars[variableName] = valueToAppend;
59+
if (target[setting] === undefined || options?.overwrite) {
60+
target[setting] = source[setting];
10161
}
62+
});
63+
}
64+
65+
export function appendPythonPath(vars: EnvironmentVariables, ...pythonPaths: string[]) {
66+
return appendPaths(vars, 'PYTHONPATH', ...pythonPaths);
67+
}
68+
69+
export function appendPath(vars: EnvironmentVariables, ...paths: string[]) {
70+
return appendPaths(vars, getSearchPathEnvVarNames()[0], ...paths);
71+
}
72+
73+
export function appendPaths(
74+
vars: EnvironmentVariables,
75+
variableName: 'PATH' | 'Path' | 'PYTHONPATH',
76+
...pathsToAppend: string[]
77+
) {
78+
const valueToAppend = pathsToAppend
79+
.filter((item) => typeof item === 'string' && item.trim().length > 0)
80+
.map((item) => item.trim())
81+
.join(path.delimiter);
82+
if (valueToAppend.length === 0) {
10283
return vars;
10384
}
85+
86+
const variable = vars ? vars[variableName] : undefined;
87+
if (variable && typeof variable === 'string' && variable.length > 0) {
88+
vars[variableName] = variable + path.delimiter + valueToAppend;
89+
} else {
90+
vars[variableName] = valueToAppend;
91+
}
92+
return vars;
10493
}
10594

95+
10696
export function parseEnvFile(lines: string | Buffer, baseVars?: EnvironmentVariables): EnvironmentVariables {
10797
const globalVars = baseVars ? baseVars : {};
10898
const vars: EnvironmentVariables = {};

src/extension/common/variables/types.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,3 @@
22
// Licensed under the MIT License.
33

44
export type EnvironmentVariables = Object & Record<string, string | undefined>;
5-
6-
// eslint-disable-next-line @typescript-eslint/naming-convention
7-
export const IEnvironmentVariablesService = Symbol('IEnvironmentVariablesService');
8-
9-
export interface IEnvironmentVariablesService {
10-
parseFile(filePath?: string, baseVars?: EnvironmentVariables): Promise<EnvironmentVariables | undefined>;
11-
parseFileSync(filePath?: string, baseVars?: EnvironmentVariables): EnvironmentVariables | undefined;
12-
mergeVariables(source: EnvironmentVariables, target: EnvironmentVariables, options?: { overwrite?: boolean }): void;
13-
appendPythonPath(vars: EnvironmentVariables, ...pythonPaths: string[]): void;
14-
appendPath(vars: EnvironmentVariables, ...paths: string[]): void;
15-
}

src/extension/debugger/attachQuickPick/picker.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33

44
'use strict';
55

6-
import { injectable } from 'inversify';
76
import { Disposable, ThemeIcon } from 'vscode';
87
import { AttachProcess } from '../../common/utils/localize';
98
import { createQuickPick } from '../../common/vscodeapi';
109
import { IAttachItem, IAttachPicker, IAttachProcessProvider } from './types';
1110

12-
@injectable()
1311
export class AttachPicker implements IAttachPicker {
1412
constructor(private readonly attachItemsProvider: IAttachProcessProvider) {}
1513

src/extension/debugger/configuration/launch.json/interpreterPathCommand.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
'use strict';
55

6-
// import { injectable } from 'inversify';
76
import { Uri } from 'vscode';
87
import { getInterpreterDetails } from '../../../common/python';
98

src/extension/debugger/configuration/resolvers/helper.ts

Lines changed: 49 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,82 +4,73 @@
44

55
'use strict';
66

7-
import { inject, injectable } from 'inversify';
87
import { PYTHON_LANGUAGE } from '../../../common/constants';
98
import { getSearchPathEnvVarNames } from '../../../common/utils/exec';
10-
import { EnvironmentVariables, IEnvironmentVariablesService } from '../../../common/variables/types';
9+
import { EnvironmentVariables } from '../../../common/variables/types';
1110
import { getActiveTextEditor } from '../../../common/vscodeapi';
1211
import { LaunchRequestArguments } from '../../../types';
12+
import * as envParser from '../../../common/variables/environment';
1313

14-
export const IDebugEnvironmentVariablesService = Symbol('IDebugEnvironmentVariablesService');
15-
export interface IDebugEnvironmentVariablesService {
16-
getEnvironmentVariables(args: LaunchRequestArguments): Promise<EnvironmentVariables>;
17-
}
18-
19-
@injectable()
20-
export class DebugEnvironmentVariablesHelper implements IDebugEnvironmentVariablesService {
21-
constructor(@inject(IEnvironmentVariablesService) private envParser: IEnvironmentVariablesService) {}
14+
export async function getDebugEnvironmentVariables(args: LaunchRequestArguments): Promise<EnvironmentVariables> {
15+
const pathVariableName = getSearchPathEnvVarNames()[0];
2216

23-
public async getEnvironmentVariables(args: LaunchRequestArguments): Promise<EnvironmentVariables> {
24-
const pathVariableName = getSearchPathEnvVarNames()[0];
17+
// Merge variables from both .env file and env json variables.
18+
const debugLaunchEnvVars: Record<string, string> =
19+
args.env && Object.keys(args.env).length > 0
20+
? ({ ...args.env } as Record<string, string>)
21+
: ({} as Record<string, string>);
22+
const envFileVars = await envParser.parseFile(args.envFile, debugLaunchEnvVars);
23+
const env = envFileVars ? { ...envFileVars } : {};
2524

26-
// Merge variables from both .env file and env json variables.
27-
const debugLaunchEnvVars: Record<string, string> =
28-
args.env && Object.keys(args.env).length > 0
29-
? ({ ...args.env } as Record<string, string>)
30-
: ({} as Record<string, string>);
31-
const envFileVars = await this.envParser.parseFile(args.envFile, debugLaunchEnvVars);
32-
const env = envFileVars ? { ...envFileVars } : {};
25+
// "overwrite: true" to ensure that debug-configuration env variable values
26+
// take precedence over env file.
27+
envParser.mergeVariables(debugLaunchEnvVars, env, { overwrite: true });
3328

34-
// "overwrite: true" to ensure that debug-configuration env variable values
35-
// take precedence over env file.
36-
this.envParser.mergeVariables(debugLaunchEnvVars, env, { overwrite: true });
29+
// Append the PYTHONPATH and PATH variables.
30+
envParser.appendPath(env, debugLaunchEnvVars[pathVariableName]);
31+
envParser.appendPythonPath(env, debugLaunchEnvVars.PYTHONPATH);
3732

38-
// Append the PYTHONPATH and PATH variables.
39-
this.envParser.appendPath(env, debugLaunchEnvVars[pathVariableName]);
40-
this.envParser.appendPythonPath(env, debugLaunchEnvVars.PYTHONPATH);
41-
42-
if (typeof env[pathVariableName] === 'string' && env[pathVariableName]!.length > 0) {
43-
// Now merge this path with the current system path.
44-
// We need to do this to ensure the PATH variable always has the system PATHs as well.
45-
this.envParser.appendPath(env, process.env[pathVariableName]!);
46-
}
47-
if (typeof env.PYTHONPATH === 'string' && env.PYTHONPATH.length > 0) {
48-
// We didn't have a value for PATH earlier and now we do.
49-
// Now merge this path with the current system path.
50-
// We need to do this to ensure the PATH variable always has the system PATHs as well.
51-
this.envParser.appendPythonPath(env, process.env.PYTHONPATH!);
52-
}
33+
if (typeof env[pathVariableName] === 'string' && env[pathVariableName]!.length > 0) {
34+
// Now merge this path with the current system path.
35+
// We need to do this to ensure the PATH variable always has the system PATHs as well.
36+
envParser.appendPath(env, process.env[pathVariableName]!);
37+
}
38+
if (typeof env.PYTHONPATH === 'string' && env.PYTHONPATH.length > 0) {
39+
// We didn't have a value for PATH earlier and now we do.
40+
// Now merge this path with the current system path.
41+
// We need to do this to ensure the PATH variable always has the system PATHs as well.
42+
envParser.appendPythonPath(env, process.env.PYTHONPATH!);
43+
}
5344

54-
if (args.console === 'internalConsole') {
55-
// For debugging, when not using any terminal, then we need to provide all env variables.
56-
// As we're spawning the process, we need to ensure all env variables are passed.
57-
// Including those from the current process (i.e. everything, not just custom vars).
58-
this.envParser.mergeVariables(process.env, env);
45+
if (args.console === 'internalConsole') {
46+
// For debugging, when not using any terminal, then we need to provide all env variables.
47+
// As we're spawning the process, we need to ensure all env variables are passed.
48+
// Including those from the current process (i.e. everything, not just custom vars).
49+
envParser.mergeVariables(process.env, env);
5950

60-
if (env[pathVariableName] === undefined && typeof process.env[pathVariableName] === 'string') {
61-
env[pathVariableName] = process.env[pathVariableName];
62-
}
63-
if (env.PYTHONPATH === undefined && typeof process.env.PYTHONPATH === 'string') {
64-
env.PYTHONPATH = process.env.PYTHONPATH;
65-
}
51+
if (env[pathVariableName] === undefined && typeof process.env[pathVariableName] === 'string') {
52+
env[pathVariableName] = process.env[pathVariableName];
6653
}
67-
68-
if (!env.hasOwnProperty('PYTHONIOENCODING')) {
69-
env.PYTHONIOENCODING = 'UTF-8';
70-
}
71-
if (!env.hasOwnProperty('PYTHONUNBUFFERED')) {
72-
env.PYTHONUNBUFFERED = '1';
54+
if (env.PYTHONPATH === undefined && typeof process.env.PYTHONPATH === 'string') {
55+
env.PYTHONPATH = process.env.PYTHONPATH;
7356
}
57+
}
7458

75-
if (args.gevent) {
76-
env.GEVENT_SUPPORT = 'True'; // this is read in pydevd_constants.py
77-
}
59+
if (!env.hasOwnProperty('PYTHONIOENCODING')) {
60+
env.PYTHONIOENCODING = 'UTF-8';
61+
}
62+
if (!env.hasOwnProperty('PYTHONUNBUFFERED')) {
63+
env.PYTHONUNBUFFERED = '1';
64+
}
7865

79-
return env;
66+
if (args.gevent) {
67+
env.GEVENT_SUPPORT = 'True'; // this is read in pydevd_constants.py
8068
}
69+
70+
return env;
8171
}
8272

73+
8374
export function getProgram(): string | undefined {
8475
const activeTextEditor = getActiveTextEditor();
8576
if (activeTextEditor && activeTextEditor.document.languageId === PYTHON_LANGUAGE) {

0 commit comments

Comments
 (0)