forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.ts
More file actions
234 lines (207 loc) · 10.8 KB
/
Copy pathanalysis.ts
File metadata and controls
234 lines (207 loc) · 10.8 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as path from 'path';
import { ExtensionContext, OutputChannel } from 'vscode';
import { Message } from 'vscode-jsonrpc';
import { CloseAction, Disposable, ErrorAction, ErrorHandler, LanguageClient, LanguageClientOptions, ServerOptions } from 'vscode-languageclient';
import { IApplicationShell } from '../common/application/types';
import { isTestExecution, STANDARD_OUTPUT_CHANNEL } from '../common/constants';
import { createDeferred, Deferred } from '../common/helpers';
import { IFileSystem, IPlatformService } from '../common/platform/types';
import { IProcessService } from '../common/process/types';
import { StopWatch } from '../common/stopWatch';
import { IConfigurationService, IOutputChannel, IPythonSettings } from '../common/types';
import { IEnvironmentVariablesProvider } from '../common/variables/types';
import { IServiceContainer } from '../ioc/types';
import {
PYTHON_ANALYSIS_ENGINE_DOWNLOADED,
PYTHON_ANALYSIS_ENGINE_ENABLED,
PYTHON_ANALYSIS_ENGINE_ERROR,
PYTHON_ANALYSIS_ENGINE_STARTUP
} from '../telemetry/constants';
import { getTelemetryReporter } from '../telemetry/telemetry';
import { AnalysisEngineDownloader } from './downloader';
import { InterpreterDataService } from './interpreterDataService';
import { PlatformData } from './platformData';
import { IExtensionActivator } from './types';
const PYTHON = 'python';
const dotNetCommand = 'dotnet';
const languageClientName = 'Python Tools';
const analysisEngineFolder = 'analysis';
class LanguageServerStartupErrorHandler implements ErrorHandler {
constructor(private readonly deferred: Deferred<void>) { }
public error(error: Error, message: Message, count: number): ErrorAction {
this.deferred.reject(error);
return ErrorAction.Shutdown;
}
public closed(): CloseAction {
this.deferred.reject();
return CloseAction.DoNotRestart;
}
}
export class AnalysisExtensionActivator implements IExtensionActivator {
private readonly configuration: IConfigurationService;
private readonly appShell: IApplicationShell;
private readonly output: OutputChannel;
private readonly fs: IFileSystem;
private readonly sw = new StopWatch();
private readonly platformData: PlatformData;
private languageClient: LanguageClient | undefined;
constructor(private readonly services: IServiceContainer, pythonSettings: IPythonSettings) {
this.configuration = this.services.get<IConfigurationService>(IConfigurationService);
this.appShell = this.services.get<IApplicationShell>(IApplicationShell);
this.output = this.services.get<OutputChannel>(IOutputChannel, STANDARD_OUTPUT_CHANNEL);
this.fs = this.services.get<IFileSystem>(IFileSystem);
this.platformData = new PlatformData(services.get<IPlatformService>(IPlatformService), this.fs);
}
public async activate(context: ExtensionContext): Promise<boolean> {
const clientOptions = await this.getAnalysisOptions(context);
if (!clientOptions) {
return false;
}
return this.startLanguageServer(context, clientOptions);
}
public async deactivate(): Promise<void> {
if (this.languageClient) {
await this.languageClient.stop();
}
}
private async startLanguageServer(context: ExtensionContext, clientOptions: LanguageClientOptions): Promise<boolean> {
// Determine if we are running MSIL/Universal via dotnet or self-contained app.
const mscorlib = path.join(context.extensionPath, analysisEngineFolder, 'mscorlib.dll');
let downloadPackage = false;
const reporter = getTelemetryReporter();
reporter.sendTelemetryEvent(PYTHON_ANALYSIS_ENGINE_ENABLED);
if (!await this.fs.fileExistsAsync(mscorlib)) {
// Depends on .NET Runtime or SDK
this.languageClient = this.createSimpleLanguageClient(context, clientOptions);
try {
await this.tryStartLanguageClient(context, this.languageClient);
return true;
} catch (ex) {
if (await this.isDotNetInstalled()) {
this.appShell.showErrorMessage(`.NET Runtime appears to be installed but the language server did not start. Error ${ex}`);
reporter.sendTelemetryEvent(PYTHON_ANALYSIS_ENGINE_ERROR, { error: 'Failed to start (MSIL)' });
return false;
}
// No .NET Runtime, no mscorlib - need to download self-contained package.
downloadPackage = true;
}
}
if (downloadPackage) {
const downloader = new AnalysisEngineDownloader(this.services, analysisEngineFolder);
await downloader.downloadAnalysisEngine(context);
reporter.sendTelemetryEvent(PYTHON_ANALYSIS_ENGINE_DOWNLOADED);
}
const serverModule = path.join(context.extensionPath, analysisEngineFolder, this.platformData.getEngineExecutableName());
// Now try to start self-contained app
this.languageClient = this.createSelfContainedLanguageClient(context, serverModule, clientOptions);
try {
await this.tryStartLanguageClient(context, this.languageClient);
return true;
} catch (ex) {
this.appShell.showErrorMessage(`Language server failed to start. Error ${ex}`);
reporter.sendTelemetryEvent(PYTHON_ANALYSIS_ENGINE_ERROR, { error: 'Failed to start (platform)' });
return false;
}
}
private async tryStartLanguageClient(context: ExtensionContext, lc: LanguageClient): Promise<void> {
let disposable: Disposable | undefined;
const deferred = createDeferred<void>();
try {
const sw = new StopWatch();
lc.clientOptions.errorHandler = new LanguageServerStartupErrorHandler(deferred);
disposable = lc.start();
lc.onReady()
.then(() => deferred.resolve())
.catch(deferred.reject);
await deferred.promise;
this.output.appendLine(`Language server ready: ${this.sw.elapsedTime} ms`);
context.subscriptions.push(disposable);
const reporter = getTelemetryReporter();
reporter.sendTelemetryEvent(PYTHON_ANALYSIS_ENGINE_STARTUP, {}, { startup_time: sw.elapsedTime });
} catch (ex) {
if (disposable) {
disposable.dispose();
}
throw ex;
}
}
private createSimpleLanguageClient(context: ExtensionContext, clientOptions: LanguageClientOptions): LanguageClient {
const commandOptions = { stdio: 'pipe' };
const serverModule = path.join(context.extensionPath, analysisEngineFolder, this.platformData.getEngineDllName());
const serverOptions: ServerOptions = {
run: { command: dotNetCommand, args: [serverModule], options: commandOptions },
debug: { command: dotNetCommand, args: [serverModule, '--debug'], options: commandOptions }
};
return new LanguageClient(PYTHON, languageClientName, serverOptions, clientOptions);
}
private createSelfContainedLanguageClient(context: ExtensionContext, serverModule: string, clientOptions: LanguageClientOptions): LanguageClient {
const options = { stdio: 'pipe' };
const serverOptions: ServerOptions = {
run: { command: serverModule, rgs: [], options: options },
debug: { command: serverModule, args: ['--debug'], options }
};
return new LanguageClient(PYTHON, languageClientName, serverOptions, clientOptions);
}
private async getAnalysisOptions(context: ExtensionContext): Promise<LanguageClientOptions | undefined> {
// tslint:disable-next-line:no-any
const properties = new Map<string, any>();
// Microsoft Python code analysis engine needs full path to the interpreter
const interpreterDataService = new InterpreterDataService(context, this.services);
const interpreterData = await interpreterDataService.getInterpreterData();
if (!interpreterData) {
const appShell = this.services.get<IApplicationShell>(IApplicationShell);
appShell.showErrorMessage('Unable to determine path to Python interpreter.');
return;
}
// tslint:disable-next-line:no-string-literal
properties['InterpreterPath'] = interpreterData.path;
// tslint:disable-next-line:no-string-literal
properties['Version'] = interpreterData.version;
// tslint:disable-next-line:no-string-literal
properties['PrefixPath'] = interpreterData.prefix;
// tslint:disable-next-line:no-string-literal
properties['DatabasePath'] = path.join(context.extensionPath, analysisEngineFolder);
let searchPaths = interpreterData.searchPaths;
const settings = this.configuration.getSettings();
if (settings.autoComplete) {
const extraPaths = settings.autoComplete.extraPaths;
if (extraPaths && extraPaths.length > 0) {
searchPaths = `${searchPaths};${extraPaths.join(';')}`;
}
}
const envProvider = this.services.get<IEnvironmentVariablesProvider>(IEnvironmentVariablesProvider);
const pythonPath = (await envProvider.getEnvironmentVariables()).PYTHONPATH;
// tslint:disable-next-line:no-string-literal
properties['SearchPaths'] = `${searchPaths};${pythonPath ? pythonPath : ''}`;
const selector: string[] = [PYTHON];
// Options to control the language client
return {
// Register the server for Python documents
documentSelector: selector,
synchronize: {
configurationSection: PYTHON
},
outputChannel: this.output,
initializationOptions: {
interpreter: {
properties
},
displayOptions: {
trimDocumentationLines: false,
maxDocumentationLineLength: 0,
trimDocumentationText: false,
maxDocumentationTextLength: 0
},
asyncStartup: true,
testEnvironment: isTestExecution()
}
};
}
private async isDotNetInstalled(): Promise<boolean> {
const ps = this.services.get<IProcessService>(IProcessService);
const result = await ps.exec('dotnet', ['--version']).catch(() => { return { stdout: '' }; });
return result.stdout.trim().startsWith('2.');
}
}