-
Notifications
You must be signed in to change notification settings - Fork 698
/
Copy pathactivate.ts
313 lines (282 loc) · 13.2 KB
/
activate.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as path from 'path';
import * as vscode from 'vscode';
import * as common from '../common';
import { CoreClrDebugUtil, getTargetArchitecture } from './util';
import { PlatformInformation } from '../shared/platform';
import {
DebuggerPrerequisiteWarning,
DebuggerPrerequisiteFailure,
DebuggerNotInstalledFailure,
} from '../omnisharp/loggingEvents';
import { EventStream } from '../eventStream';
import { getRuntimeDependencyPackageWithId } from '../tools/runtimeDependencyPackageUtils';
import { getDotnetInfo } from '../shared/utils/getDotnetInfo';
import { RemoteAttachPicker } from '../features/processPicker';
import CompositeDisposable from '../compositeDisposable';
import { BaseVsDbgConfigurationProvider } from '../shared/configurationProvider';
import { omnisharpOptions } from '../shared/options';
export async function activate(
thisExtension: vscode.Extension<any>,
context: vscode.ExtensionContext,
platformInformation: PlatformInformation,
eventStream: EventStream,
csharpOutputChannel: vscode.OutputChannel
) {
const disposables = new CompositeDisposable();
const debugUtil = new CoreClrDebugUtil(context.extensionPath);
if (!CoreClrDebugUtil.existsSync(debugUtil.debugAdapterDir())) {
const isValidArchitecture: boolean = await checkIsValidArchitecture(platformInformation, eventStream);
// If this is a valid architecture, we should have had a debugger, so warn if we didn't, otherwise
// a warning was already issued, so do nothing.
if (isValidArchitecture) {
eventStream.post(
new DebuggerPrerequisiteFailure(
vscode.l10n.t('[ERROR]: C# Extension failed to install the debugger package.')
)
);
showInstallErrorMessage(eventStream);
}
} else if (!CoreClrDebugUtil.existsSync(debugUtil.installCompleteFilePath())) {
completeDebuggerInstall(debugUtil, platformInformation, eventStream);
}
// register process picker for attach for legacy configurations.
disposables.add(vscode.commands.registerCommand('csharp.listProcess', () => ''));
disposables.add(vscode.commands.registerCommand('csharp.listRemoteProcess', () => ''));
// List remote processes for docker extension.
// Change to return "" when https://github.com/microsoft/vscode/issues/110889 is resolved.
disposables.add(
vscode.commands.registerCommand('csharp.listRemoteDockerProcess', async (args) => {
const attachItem = await RemoteAttachPicker.ShowAttachEntries(args, platformInformation);
return attachItem
? attachItem.id
: Promise.reject<string>(new Error(vscode.l10n.t('Could not find a process id to attach.')));
})
);
// Register a command to fire attach to process for the coreclr debug engine.
disposables.add(
vscode.commands.registerCommand('csharp.attachToProcess', async () => {
vscode.debug.startDebugging(
undefined,
{
name: '.NET Core Attach',
type: 'coreclr',
request: 'attach',
},
undefined
);
})
);
const factory = new DebugAdapterExecutableFactory(
debugUtil,
platformInformation,
eventStream,
thisExtension.packageJSON,
thisExtension.extensionPath
);
/** 'clr' type does not have a intial configuration provider, but we need to register it to support the common debugger features listed in {@link BaseVsDbgConfigurationProvider} */
context.subscriptions.push(
vscode.debug.registerDebugConfigurationProvider(
'clr',
new BaseVsDbgConfigurationProvider(platformInformation, csharpOutputChannel)
)
);
disposables.add(vscode.debug.registerDebugAdapterDescriptorFactory('coreclr', factory));
disposables.add(vscode.debug.registerDebugAdapterDescriptorFactory('clr', factory));
disposables.add(vscode.debug.registerDebugAdapterDescriptorFactory('monovsdbg', factory));
context.subscriptions.push(disposables);
}
async function checkIsValidArchitecture(
platformInformation: PlatformInformation,
eventStream: EventStream
): Promise<boolean> {
if (platformInformation) {
if (platformInformation.isMacOS()) {
if (platformInformation.architecture === 'arm64') {
return true;
}
// Validate we are on compatiable macOS version if we are x86_64
if (
platformInformation.architecture !== 'x86_64' ||
(platformInformation.architecture === 'x86_64' && !CoreClrDebugUtil.isMacOSSupported())
) {
eventStream.post(
new DebuggerPrerequisiteFailure(
vscode.l10n.t(
'[ERROR] The debugger cannot be installed. The debugger requires macOS 10.12 (Sierra) or newer.'
)
)
);
return false;
}
return true;
} else if (platformInformation.isWindows()) {
if (platformInformation.architecture === 'x86') {
eventStream.post(
new DebuggerPrerequisiteWarning(
vscode.l10n.t(
`[WARNING]: x86 Windows is not supported by the .NET debugger. Debugging will not be available.`
)
)
);
return false;
}
return true;
} else if (platformInformation.isLinux()) {
return true;
}
}
eventStream.post(
new DebuggerPrerequisiteFailure(vscode.l10n.t('[ERROR] The debugger cannot be installed. Unknown platform.'))
);
return false;
}
async function completeDebuggerInstall(
debugUtil: CoreClrDebugUtil,
platformInformation: PlatformInformation,
eventStream: EventStream
): Promise<boolean> {
try {
await debugUtil.checkDotNetCli(omnisharpOptions.dotNetCliPaths);
const isValidArchitecture = await checkIsValidArchitecture(platformInformation, eventStream);
if (!isValidArchitecture) {
eventStream.post(new DebuggerNotInstalledFailure());
vscode.window.showErrorMessage(
vscode.l10n.t(
'Failed to complete the installation of the C# extension. Please see the error in the output window below.'
)
);
return false;
}
// Write install.complete
CoreClrDebugUtil.writeEmptyFile(debugUtil.installCompleteFilePath());
return true;
} catch (err) {
const error = err as Error;
// Check for dotnet tools failed. pop the UI
showDotnetToolsWarning(error.message);
eventStream.post(new DebuggerPrerequisiteWarning(error.message));
// TODO: log telemetry?
return false;
}
}
function showInstallErrorMessage(eventStream: EventStream) {
eventStream.post(new DebuggerNotInstalledFailure());
vscode.window.showErrorMessage(
vscode.l10n.t(
'An error occurred during installation of the .NET Debugger. The C# extension may need to be reinstalled.'
)
);
}
function showDotnetToolsWarning(message: string): void {
const config = vscode.workspace.getConfiguration('csharp');
if (!config.get('suppressDotnetInstallWarning', false)) {
const getDotNetMessage = vscode.l10n.t('Get the SDK');
const goToSettingsMessage = vscode.l10n.t('Disable message in settings');
const helpMessage = vscode.l10n.t('Help');
// Buttons are shown in right-to-left order, with a close button to the right of everything;
// getDotNetMessage will be the first button, then goToSettingsMessage, then the close button.
vscode.window.showErrorMessage(message, goToSettingsMessage, getDotNetMessage, helpMessage).then((value) => {
if (value === getDotNetMessage) {
const dotnetcoreURL = 'https://dot.net/core-sdk-vscode';
vscode.env.openExternal(vscode.Uri.parse(dotnetcoreURL));
} else if (value === goToSettingsMessage) {
vscode.commands.executeCommand('workbench.action.openGlobalSettings');
} else if (value == helpMessage) {
const helpURL = 'https://aka.ms/VSCode-CS-DotnetNotFoundHelp';
vscode.env.openExternal(vscode.Uri.parse(helpURL));
}
});
}
}
// The activate method registers this factory to provide DebugAdapterDescriptors
// If the debugger components have not finished downloading, the proxy displays an error message to the user
// Else it will launch the debug adapter
export class DebugAdapterExecutableFactory implements vscode.DebugAdapterDescriptorFactory {
constructor(
private readonly debugUtil: CoreClrDebugUtil,
private readonly platformInfo: PlatformInformation,
private readonly eventStream: EventStream,
private readonly packageJSON: any,
private readonly extensionPath: string
) {}
async createDebugAdapterDescriptor(
_session: vscode.DebugSession,
executable: vscode.DebugAdapterExecutable | undefined
): Promise<vscode.DebugAdapterDescriptor> {
const util = new CoreClrDebugUtil(common.getExtensionPath());
// Check for .debugger folder. Handle if it does not exist.
if (!CoreClrDebugUtil.existsSync(util.debugAdapterDir())) {
// our install.complete file does not exist yet, meaning we have not completed the installation. Try to figure out what if anything the package manager is doing
// the order in which files are dealt with is this:
// 1. install.Begin is created
// 2. install.Lock is created
// 3. install.Begin is deleted
// 4. install.complete is created
// install.Lock does not exist, need to wait for packages to finish downloading.
let installLock = false;
const debuggerPackage = getRuntimeDependencyPackageWithId(
'Debugger',
this.packageJSON,
this.platformInfo,
this.extensionPath
);
if (debuggerPackage?.installPath) {
installLock = await common.installFileExists(debuggerPackage.installPath, common.InstallFileType.Lock);
}
if (!installLock) {
this.eventStream.post(new DebuggerNotInstalledFailure());
throw new Error(
vscode.l10n.t(
'The C# extension is still downloading packages. Please see progress in the output window below.'
)
);
}
// install.complete does not exist, check dotnetCLI to see if we can complete.
else if (!CoreClrDebugUtil.existsSync(util.installCompleteFilePath())) {
const success = await completeDebuggerInstall(this.debugUtil, this.platformInfo, this.eventStream);
if (!success) {
this.eventStream.post(new DebuggerNotInstalledFailure());
throw new Error(
vscode.l10n.t(
'Failed to complete the installation of the C# extension. Please see the error in the output window below.'
)
);
}
}
}
// debugger has finished installation, kick off our debugger process
// use the executable specified in the package.json if it exists or determine it based on some other information (e.g. the session)
if (!executable) {
const dotNetInfo = await getDotnetInfo(omnisharpOptions.dotNetCliPaths);
const targetArchitecture = getTargetArchitecture(
this.platformInfo,
_session.configuration.targetArchitecture,
dotNetInfo
);
const command = path.join(
common.getExtensionPath(),
'.debugger',
targetArchitecture,
'vsdbg-ui' + CoreClrDebugUtil.getPlatformExeExtension()
);
// Look to see if DOTNET_ROOT is set, then use dotnet cli path
const dotnetRoot: string =
process.env.DOTNET_ROOT ?? (dotNetInfo.CliPath ? path.dirname(dotNetInfo.CliPath) : '');
let options: vscode.DebugAdapterExecutableOptions | undefined = undefined;
if (dotnetRoot) {
options = {
env: {
DOTNET_ROOT: dotnetRoot,
},
};
}
executable = new vscode.DebugAdapterExecutable(command, [], options);
}
// make VS Code launch the DA executable
return executable;
}
}