forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathlaunch.ts
199 lines (187 loc) · 9.41 KB
/
launch.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { inject, injectable, named } from 'inversify';
import { CancellationToken, Uri, WorkspaceFolder } from 'vscode';
import { InvalidPythonPathInDebuggerServiceId } from '../../../../application/diagnostics/checks/invalidPythonPathInDebugger';
import { IDiagnosticsService, IInvalidPythonPathInDebuggerService } from '../../../../application/diagnostics/types';
import { IConfigurationService } from '../../../../common/types';
import { getOSType, OSType } from '../../../../common/utils/platform';
import { IInterpreterService } from '../../../../interpreter/contracts';
import { DebuggerTypeName } from '../../../constants';
import { DebugOptions, DebugPurpose, LaunchRequestArguments } from '../../../types';
import { BaseConfigurationResolver } from './base';
import { getProgram, IDebugEnvironmentVariablesService } from './helper';
@injectable()
export class LaunchConfigurationResolver extends BaseConfigurationResolver<LaunchRequestArguments> {
constructor(
@inject(IDiagnosticsService)
@named(InvalidPythonPathInDebuggerServiceId)
private readonly invalidPythonPathInDebuggerService: IInvalidPythonPathInDebuggerService,
@inject(IConfigurationService) configurationService: IConfigurationService,
@inject(IDebugEnvironmentVariablesService) private readonly debugEnvHelper: IDebugEnvironmentVariablesService,
@inject(IInterpreterService) interpreterService: IInterpreterService,
) {
super(configurationService, interpreterService);
}
public async resolveDebugConfiguration(
folder: WorkspaceFolder | undefined,
debugConfiguration: LaunchRequestArguments,
_token?: CancellationToken,
): Promise<LaunchRequestArguments | undefined> {
if (
debugConfiguration.name === undefined &&
debugConfiguration.type === undefined &&
debugConfiguration.request === undefined &&
debugConfiguration.program === undefined &&
debugConfiguration.env === undefined
) {
const defaultProgram = getProgram();
debugConfiguration.name = 'Launch';
debugConfiguration.type = DebuggerTypeName;
debugConfiguration.request = 'launch';
debugConfiguration.program = defaultProgram ?? '';
debugConfiguration.env = {};
}
const workspaceFolder = LaunchConfigurationResolver.getWorkspaceFolder(folder);
await this.resolveAndUpdatePaths(workspaceFolder, debugConfiguration);
return debugConfiguration;
}
public async resolveDebugConfigurationWithSubstitutedVariables(
folder: WorkspaceFolder | undefined,
debugConfiguration: LaunchRequestArguments,
_token?: CancellationToken,
): Promise<LaunchRequestArguments | undefined> {
const workspaceFolder = LaunchConfigurationResolver.getWorkspaceFolder(folder);
await this.provideLaunchDefaults(workspaceFolder, debugConfiguration);
const isValid = await this.validateLaunchConfiguration(folder, debugConfiguration);
if (!isValid) {
return undefined;
}
if (Array.isArray(debugConfiguration.debugOptions)) {
debugConfiguration.debugOptions = debugConfiguration.debugOptions!.filter(
(item, pos) => debugConfiguration.debugOptions!.indexOf(item) === pos,
);
}
return debugConfiguration;
}
protected async provideLaunchDefaults(
workspaceFolder: Uri | undefined,
debugConfiguration: LaunchRequestArguments,
): Promise<void> {
if (debugConfiguration.python === undefined) {
debugConfiguration.python = debugConfiguration.pythonPath;
}
if (debugConfiguration.debugAdapterPython === undefined) {
debugConfiguration.debugAdapterPython = debugConfiguration.pythonPath;
}
if (debugConfiguration.debugLauncherPython === undefined) {
debugConfiguration.debugLauncherPython = debugConfiguration.pythonPath;
}
delete debugConfiguration.pythonPath;
if (typeof debugConfiguration.cwd !== 'string' && workspaceFolder) {
debugConfiguration.cwd = workspaceFolder.fsPath;
}
if (typeof debugConfiguration.envFile !== 'string' && workspaceFolder) {
const settings = this.configurationService.getSettings(workspaceFolder);
debugConfiguration.envFile = settings.envFile;
}
// Extract environment variables from .env file in the vscode context and
// set the "env" debug configuration argument. This expansion should be
// done here before handing of the environment settings to the debug adapter
debugConfiguration.env = await this.debugEnvHelper.getEnvironmentVariables(debugConfiguration);
if (typeof debugConfiguration.stopOnEntry !== 'boolean') {
debugConfiguration.stopOnEntry = false;
}
debugConfiguration.showReturnValue = debugConfiguration.showReturnValue !== false;
if (!debugConfiguration.console) {
debugConfiguration.console = 'integratedTerminal';
}
// If using a terminal, then never open internal console.
if (debugConfiguration.console !== 'internalConsole' && !debugConfiguration.internalConsoleOptions) {
debugConfiguration.internalConsoleOptions = 'neverOpen';
}
if (!Array.isArray(debugConfiguration.debugOptions)) {
debugConfiguration.debugOptions = [];
}
if (debugConfiguration.justMyCode === undefined) {
// Populate justMyCode using debugStdLib
debugConfiguration.justMyCode = !debugConfiguration.debugStdLib;
}
// Pass workspace folder so we can get this when we get debug events firing.
debugConfiguration.workspaceFolder = workspaceFolder ? workspaceFolder.fsPath : undefined;
const debugOptions = debugConfiguration.debugOptions!;
if (!debugConfiguration.justMyCode) {
LaunchConfigurationResolver.debugOption(debugOptions, DebugOptions.DebugStdLib);
}
if (debugConfiguration.stopOnEntry) {
LaunchConfigurationResolver.debugOption(debugOptions, DebugOptions.StopOnEntry);
}
if (debugConfiguration.showReturnValue) {
LaunchConfigurationResolver.debugOption(debugOptions, DebugOptions.ShowReturnValue);
}
if (debugConfiguration.django) {
LaunchConfigurationResolver.debugOption(debugOptions, DebugOptions.Django);
}
if (debugConfiguration.jinja) {
LaunchConfigurationResolver.debugOption(debugOptions, DebugOptions.Jinja);
}
if (debugConfiguration.redirectOutput === undefined && debugConfiguration.console === 'internalConsole') {
debugConfiguration.redirectOutput = true;
}
if (debugConfiguration.redirectOutput) {
LaunchConfigurationResolver.debugOption(debugOptions, DebugOptions.RedirectOutput);
}
if (debugConfiguration.sudo) {
LaunchConfigurationResolver.debugOption(debugOptions, DebugOptions.Sudo);
}
if (debugConfiguration.subProcess === true) {
LaunchConfigurationResolver.debugOption(debugOptions, DebugOptions.SubProcess);
}
if (getOSType() === OSType.Windows) {
LaunchConfigurationResolver.debugOption(debugOptions, DebugOptions.FixFilePathCase);
}
const isFastAPI = LaunchConfigurationResolver.isDebuggingFastAPI(debugConfiguration);
const isFlask = LaunchConfigurationResolver.isDebuggingFlask(debugConfiguration);
if (
(debugConfiguration.pyramid || isFlask || isFastAPI) &&
debugOptions.indexOf(DebugOptions.Jinja) === -1 &&
debugConfiguration.jinja !== false
) {
LaunchConfigurationResolver.debugOption(debugOptions, DebugOptions.Jinja);
}
// Unlike with attach, we do not set a default path mapping.
// (See: https://github.com/microsoft/vscode-python/issues/3568)
if (debugConfiguration.pathMappings) {
let { pathMappings } = debugConfiguration;
if (pathMappings.length > 0) {
pathMappings = LaunchConfigurationResolver.fixUpPathMappings(
pathMappings || [],
workspaceFolder ? workspaceFolder.fsPath : '',
);
}
debugConfiguration.pathMappings = pathMappings.length > 0 ? pathMappings : undefined;
}
const trigger =
debugConfiguration.purpose?.includes(DebugPurpose.DebugTest) || debugConfiguration.request === 'test'
? 'test'
: 'launch';
LaunchConfigurationResolver.sendTelemetry(trigger, debugConfiguration);
}
protected async validateLaunchConfiguration(
folder: WorkspaceFolder | undefined,
debugConfiguration: LaunchRequestArguments,
): Promise<boolean> {
const diagnosticService = this.invalidPythonPathInDebuggerService;
for (const executable of [
debugConfiguration.python,
debugConfiguration.debugAdapterPython,
debugConfiguration.debugLauncherPython,
]) {
if (!(await diagnosticService.validatePythonPath(executable, this.pythonPathSource, folder?.uri))) {
return false;
}
}
return true;
}
}