|
4 | 4 |
|
5 | 5 | 'use strict';
|
6 | 6 |
|
7 |
| -import { inject, injectable } from 'inversify'; |
8 | 7 | import { PYTHON_LANGUAGE } from '../../../common/constants';
|
9 | 8 | import { getSearchPathEnvVarNames } from '../../../common/utils/exec';
|
10 |
| -import { EnvironmentVariables, IEnvironmentVariablesService } from '../../../common/variables/types'; |
| 9 | +import { EnvironmentVariables } from '../../../common/variables/types'; |
11 | 10 | import { getActiveTextEditor } from '../../../common/vscodeapi';
|
12 | 11 | import { LaunchRequestArguments } from '../../../types';
|
| 12 | +import * as envParser from '../../../common/variables/environment'; |
13 | 13 |
|
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]; |
22 | 16 |
|
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 } : {}; |
25 | 24 |
|
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 }); |
33 | 28 |
|
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); |
37 | 32 |
|
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 | + } |
53 | 44 |
|
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); |
59 | 50 |
|
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]; |
66 | 53 | }
|
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; |
73 | 56 | }
|
| 57 | + } |
74 | 58 |
|
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 | + } |
78 | 65 |
|
79 |
| - return env; |
| 66 | + if (args.gevent) { |
| 67 | + env.GEVENT_SUPPORT = 'True'; // this is read in pydevd_constants.py |
80 | 68 | }
|
| 69 | + |
| 70 | + return env; |
81 | 71 | }
|
82 | 72 |
|
| 73 | + |
83 | 74 | export function getProgram(): string | undefined {
|
84 | 75 | const activeTextEditor = getActiveTextEditor();
|
85 | 76 | if (activeTextEditor && activeTextEditor.document.languageId === PYTHON_LANGUAGE) {
|
|
0 commit comments