forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
base.ts
235 lines (215 loc) · 9.71 KB
/
base.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
'use strict';
import { injectable } from 'inversify';
import * as path from 'path';
import { CancellationToken, DebugConfiguration, Uri, WorkspaceFolder } from 'vscode';
import { IConfigurationService } from '../../../../common/types';
import { getOSType, OSType } from '../../../../common/utils/platform';
import {
getWorkspaceFolder as getVSCodeWorkspaceFolder,
getWorkspaceFolders,
} from '../../../../common/vscodeApis/workspaceApis';
import { IInterpreterService } from '../../../../interpreter/contracts';
import { sendTelemetryEvent } from '../../../../telemetry';
import { EventName } from '../../../../telemetry/constants';
import { DebuggerTelemetry } from '../../../../telemetry/types';
import { AttachRequestArguments, DebugOptions, LaunchRequestArguments, PathMapping } from '../../../types';
import { PythonPathSource } from '../../types';
import { IDebugConfigurationResolver } from '../types';
import { resolveVariables } from '../utils/common';
import { getProgram } from './helper';
@injectable()
export abstract class BaseConfigurationResolver<T extends DebugConfiguration>
implements IDebugConfigurationResolver<T> {
protected pythonPathSource: PythonPathSource = PythonPathSource.launchJson;
constructor(
protected readonly configurationService: IConfigurationService,
protected readonly interpreterService: IInterpreterService,
) {}
// This is a legacy hook used solely for backwards-compatible manual substitution
// of ${command:python.interpreterPath} in "pythonPath", for the sake of other
// existing implementations of resolveDebugConfiguration() that may rely on it.
//
// For all future config variables, expansion should be performed by VSCode itself,
// and validation of debug configuration in derived classes should be performed in
// resolveDebugConfigurationWithSubstitutedVariables() instead, where all variables
// are already substituted.
// eslint-disable-next-line class-methods-use-this
public async resolveDebugConfiguration(
_folder: WorkspaceFolder | undefined,
debugConfiguration: DebugConfiguration,
_token?: CancellationToken,
): Promise<T | undefined> {
return debugConfiguration as T;
}
public abstract resolveDebugConfigurationWithSubstitutedVariables(
folder: WorkspaceFolder | undefined,
debugConfiguration: DebugConfiguration,
token?: CancellationToken,
): Promise<T | undefined>;
protected static getWorkspaceFolder(folder: WorkspaceFolder | undefined): Uri | undefined {
if (folder) {
return folder.uri;
}
const program = getProgram();
const workspaceFolders = getWorkspaceFolders();
if (!Array.isArray(workspaceFolders) || workspaceFolders.length === 0) {
return program ? Uri.file(path.dirname(program)) : undefined;
}
if (workspaceFolders.length === 1) {
return workspaceFolders[0].uri;
}
if (program) {
const workspaceFolder = getVSCodeWorkspaceFolder(Uri.file(program));
if (workspaceFolder) {
return workspaceFolder.uri;
}
}
return undefined;
}
protected async resolveAndUpdatePaths(
workspaceFolder: Uri | undefined,
debugConfiguration: LaunchRequestArguments,
): Promise<void> {
BaseConfigurationResolver.resolveAndUpdateEnvFilePath(workspaceFolder, debugConfiguration);
await this.resolveAndUpdatePythonPath(workspaceFolder, debugConfiguration);
}
protected static resolveAndUpdateEnvFilePath(
workspaceFolder: Uri | undefined,
debugConfiguration: LaunchRequestArguments,
): void {
if (!debugConfiguration) {
return;
}
if (debugConfiguration.envFile && (workspaceFolder || debugConfiguration.cwd)) {
debugConfiguration.envFile = resolveVariables(
debugConfiguration.envFile,
(workspaceFolder ? workspaceFolder.fsPath : undefined) || debugConfiguration.cwd,
undefined,
);
}
}
protected async resolveAndUpdatePythonPath(
workspaceFolder: Uri | undefined,
debugConfiguration: LaunchRequestArguments,
): Promise<void> {
if (!debugConfiguration) {
return;
}
if (debugConfiguration.pythonPath === '${command:python.interpreterPath}' || !debugConfiguration.pythonPath) {
const interpreterPath =
(await this.interpreterService.getActiveInterpreter(workspaceFolder))?.path ??
this.configurationService.getSettings(workspaceFolder).pythonPath;
debugConfiguration.pythonPath = interpreterPath;
} else {
debugConfiguration.pythonPath = resolveVariables(
debugConfiguration.pythonPath ? debugConfiguration.pythonPath : undefined,
workspaceFolder?.fsPath,
undefined,
);
}
if (debugConfiguration.python === '${command:python.interpreterPath}' || !debugConfiguration.python) {
this.pythonPathSource = PythonPathSource.settingsJson;
} else {
this.pythonPathSource = PythonPathSource.launchJson;
}
debugConfiguration.python = resolveVariables(
debugConfiguration.python ? debugConfiguration.python : undefined,
workspaceFolder?.fsPath,
undefined,
);
}
protected static debugOption(debugOptions: DebugOptions[], debugOption: DebugOptions): void {
if (debugOptions.indexOf(debugOption) >= 0) {
return;
}
debugOptions.push(debugOption);
}
protected static isLocalHost(hostName?: string): boolean {
const LocalHosts = ['localhost', '127.0.0.1', '::1'];
return !!(hostName && LocalHosts.indexOf(hostName.toLowerCase()) >= 0);
}
protected static fixUpPathMappings(
pathMappings: PathMapping[],
defaultLocalRoot?: string,
defaultRemoteRoot?: string,
): PathMapping[] {
if (!defaultLocalRoot) {
return [];
}
if (!defaultRemoteRoot) {
defaultRemoteRoot = defaultLocalRoot;
}
if (pathMappings.length === 0) {
pathMappings = [
{
localRoot: defaultLocalRoot,
remoteRoot: defaultRemoteRoot,
},
];
} else {
// Expand ${workspaceFolder} variable first if necessary.
pathMappings = pathMappings.map(({ localRoot: mappedLocalRoot, remoteRoot }) => {
const resolvedLocalRoot = resolveVariables(mappedLocalRoot, defaultLocalRoot, undefined);
return {
localRoot: resolvedLocalRoot || '',
// TODO: Apply to remoteRoot too?
remoteRoot,
};
});
}
// If on Windows, lowercase the drive letter for path mappings.
// TODO: Apply even if no localRoot?
if (getOSType() === OSType.Windows) {
// TODO: Apply to remoteRoot too?
pathMappings = pathMappings.map(({ localRoot: windowsLocalRoot, remoteRoot }) => {
let localRoot = windowsLocalRoot;
if (windowsLocalRoot.match(/^[A-Z]:/)) {
localRoot = `${windowsLocalRoot[0].toLowerCase()}${windowsLocalRoot.substr(1)}`;
}
return { localRoot, remoteRoot };
});
}
return pathMappings;
}
protected static isDebuggingFastAPI(
debugConfiguration: Partial<LaunchRequestArguments & AttachRequestArguments>,
): boolean {
return !!(debugConfiguration.module && debugConfiguration.module.toUpperCase() === 'FASTAPI');
}
protected static isDebuggingFlask(
debugConfiguration: Partial<LaunchRequestArguments & AttachRequestArguments>,
): boolean {
return !!(debugConfiguration.module && debugConfiguration.module.toUpperCase() === 'FLASK');
}
protected static sendTelemetry(
trigger: 'launch' | 'attach' | 'test',
debugConfiguration: Partial<LaunchRequestArguments & AttachRequestArguments>,
): void {
const name = debugConfiguration.name || '';
const moduleName = debugConfiguration.module || '';
const telemetryProps: DebuggerTelemetry = {
trigger,
console: debugConfiguration.console,
hasEnvVars: typeof debugConfiguration.env === 'object' && Object.keys(debugConfiguration.env).length > 0,
django: !!debugConfiguration.django,
fastapi: BaseConfigurationResolver.isDebuggingFastAPI(debugConfiguration),
flask: BaseConfigurationResolver.isDebuggingFlask(debugConfiguration),
hasArgs: Array.isArray(debugConfiguration.args) && debugConfiguration.args.length > 0,
isLocalhost: BaseConfigurationResolver.isLocalHost(debugConfiguration.host),
isModule: moduleName.length > 0,
isSudo: !!debugConfiguration.sudo,
jinja: !!debugConfiguration.jinja,
pyramid: !!debugConfiguration.pyramid,
stopOnEntry: !!debugConfiguration.stopOnEntry,
showReturnValue: !!debugConfiguration.showReturnValue,
subProcess: !!debugConfiguration.subProcess,
watson: name.toLowerCase().indexOf('watson') >= 0,
pyspark: name.toLowerCase().indexOf('pyspark') >= 0,
gevent: name.toLowerCase().indexOf('gevent') >= 0,
scrapy: moduleName.toLowerCase() === 'scrapy',
};
sendTelemetryEvent(EventName.DEBUGGER, undefined, telemetryProps);
}
}