/
NetCoreDebugHelper.ts
233 lines (200 loc) · 11.1 KB
/
NetCoreDebugHelper.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE.md in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as fse from 'fs-extra';
import * as os from 'os';
import * as path from 'path';
import { DebugConfiguration } from 'vscode';
import { ext } from '../../extensionVariables';
import { NetCoreTaskHelper, NetCoreTaskOptions } from '../../tasks/netcore/NetCoreTaskHelper';
import { pathNormalize } from '../../utils/pathNormalize';
import { PlatformOS } from '../../utils/platform';
import { unresolveWorkspaceFolder } from '../../utils/resolveVariables';
import { ChildProcessProvider } from '../coreclr/ChildProcessProvider';
import { CommandLineDotNetClient } from '../coreclr/CommandLineDotNetClient';
import { LocalFileSystemProvider } from '../coreclr/fsProvider';
import { AspNetCoreSslManager, LocalAspNetCoreSslManager } from '../coreclr/LocalAspNetCoreSslManager';
import { LocalOSProvider } from '../coreclr/LocalOSProvider';
import { MsBuildNetCoreProjectProvider, NetCoreProjectProvider } from '../coreclr/netCoreProjectProvider';
import { DefaultOutputManager } from '../coreclr/outputManager';
import { OSTempFileProvider } from '../coreclr/tempFileProvider';
import { RemoteVsDbgClient, VsDbgClient } from '../coreclr/vsdbgClient';
import { DebugHelper, DockerDebugContext, DockerDebugScaffoldContext, inferContainerName, ResolvedDebugConfiguration, resolveDockerServerReadyAction } from '../DebugHelper';
import { DockerDebugConfiguration } from '../DockerDebugConfigurationProvider';
export interface NetCoreDebugOptions extends NetCoreTaskOptions {
appOutput?: string;
}
export interface NetCoreDockerDebugConfiguration extends DebugConfiguration {
netCore?: NetCoreDebugOptions;
}
export interface NetCoreDebugScaffoldingOptions {
appProject?: string;
}
export class NetCoreDebugHelper implements DebugHelper {
private readonly netCoreProjectProvider: NetCoreProjectProvider;
private readonly aspNetCoreSslManager: AspNetCoreSslManager;
private readonly vsDbgClientFactory: () => VsDbgClient;
private vsDbgClient: VsDbgClient;
constructor() {
const processProvider = new ChildProcessProvider();
const fsProvider = new LocalFileSystemProvider();
const osProvider = new LocalOSProvider();
const dotNetClient = new CommandLineDotNetClient(
processProvider,
fsProvider,
osProvider
);
this.netCoreProjectProvider = new MsBuildNetCoreProjectProvider(
fsProvider,
dotNetClient,
new OSTempFileProvider(osProvider, processProvider)
);
this.aspNetCoreSslManager = new LocalAspNetCoreSslManager(
dotNetClient,
this.netCoreProjectProvider,
processProvider,
osProvider
);
this.vsDbgClientFactory = () => {
if (this.vsDbgClient === undefined) {
this.vsDbgClient = new RemoteVsDbgClient(
new DefaultOutputManager(ext.outputChannel),
fsProvider,
ext.context.globalState,
osProvider,
processProvider
);
}
return this.vsDbgClient;
};
}
public async provideDebugConfigurations(context: DockerDebugScaffoldContext, options?: NetCoreDebugScaffoldingOptions): Promise<DockerDebugConfiguration[]> {
options = options || {};
options.appProject = options.appProject || await NetCoreTaskHelper.inferAppProject(context.folder); // This method internally checks the user-defined input first
return [
{
name: 'Docker .NET Core Launch',
type: 'docker',
request: 'launch',
preLaunchTask: 'docker-run: debug',
netCore: {
appProject: unresolveWorkspaceFolder(options.appProject, context.folder)
}
}
];
}
public async resolveDebugConfiguration(context: DockerDebugContext, debugConfiguration: DockerDebugConfiguration): Promise<ResolvedDebugConfiguration | undefined> {
debugConfiguration.netCore = debugConfiguration.netCore || {};
debugConfiguration.netCore.appProject = await NetCoreTaskHelper.inferAppProject(context.folder, debugConfiguration.netCore); // This method internally checks the user-defined input first
const { configureSsl, containerName, platformOS } = await this.loadExternalInfo(context, debugConfiguration);
const appOutput = await this.inferAppOutput(debugConfiguration.netCore);
if (context.cancellationToken && context.cancellationToken.isCancellationRequested) {
// inferAppOutput is slow, give a chance to cancel
return undefined;
}
await this.acquireDebuggers(platformOS);
if (context.cancellationToken && context.cancellationToken.isCancellationRequested) {
// acquireDebuggers is slow, give a chance to cancel
return undefined;
}
if (configureSsl) {
await this.configureSsl(debugConfiguration, appOutput);
if (context.cancellationToken && context.cancellationToken.isCancellationRequested) {
// configureSsl is slow, give a chance to cancel
return undefined;
}
}
const additionalProbingPathsArgs = NetCoreDebugHelper.getAdditionalProbingPathsArgs(platformOS);
const containerAppOutput = NetCoreDebugHelper.getContainerAppOutput(debugConfiguration, appOutput, platformOS);
const dockerServerReadyAction = resolveDockerServerReadyAction(
debugConfiguration,
{
containerName: containerName,
pattern: '^\\s*Now listening on:\\s+(https?://\\S+)',
action: 'openExternally',
uriFormat: '%s://localhost:%s',
},
configureSsl || await NetCoreTaskHelper.isWebApp(debugConfiguration.netCore.appProject) // For .NET Core Console we won't create a DockerServerReadyAction unless at least part of one is user-provided
);
return {
...debugConfiguration, // Gets things like name, preLaunchTask, serverReadyAction, etc.
type: 'coreclr',
request: 'launch',
program: debugConfiguration.program || 'dotnet',
args: debugConfiguration.args || [additionalProbingPathsArgs, containerAppOutput].join(' '),
cwd: debugConfiguration.cwd || platformOS === 'Windows' ? 'C:\\app' : '/app',
dockerOptions: {
containerNameToKill: containerName,
dockerServerReadyAction: dockerServerReadyAction,
removeContainerAfterDebug: debugConfiguration.removeContainerAfterDebug
},
pipeTransport: {
pipeProgram: 'docker',
// tslint:disable: no-invalid-template-strings
pipeArgs: ['exec', '-i', containerName, '${debuggerCommand}'],
pipeCwd: '${workspaceFolder}',
// tslint:enable: no-invalid-template-strings
debuggerPath: platformOS === 'Windows' ?
'C:\\remote_debugger\\win7-x64\\latest\\vsdbg' :
'/remote_debugger/vsdbg',
quoteArgs: false,
},
sourceFileMap: debugConfiguration.sourceFileMap || {
'/app/Views': path.join(path.dirname(debugConfiguration.netCore.appProject), 'Views'),
}
};
}
public static getHostDebuggerPathBase(): string {
return path.join(os.homedir(), '.vsdbg');
}
private async inferAppOutput(helperOptions: NetCoreDebugOptions): Promise<string> {
return await this.netCoreProjectProvider.getTargetPath(helperOptions.appProject);
}
private async loadExternalInfo(context: DockerDebugContext, debugConfiguration: DockerDebugConfiguration): Promise<{ configureSsl: boolean, containerName: string, platformOS: PlatformOS }> {
const associatedTask = context.runDefinition;
return {
configureSsl: associatedTask && associatedTask.netCore && associatedTask.netCore.configureSsl !== undefined ? associatedTask.netCore.configureSsl : await NetCoreTaskHelper.inferSsl(context.folder, debugConfiguration.netCore),
containerName: inferContainerName(debugConfiguration, context, context.folder.name),
platformOS: associatedTask && associatedTask.dockerRun && associatedTask.dockerRun.os || 'Linux',
}
}
private async acquireDebuggers(platformOS: PlatformOS): Promise<void> {
if (platformOS === 'Windows') {
await this.vsDbgClientFactory().getVsDbgVersion('latest', 'win7-x64');
} else {
await this.vsDbgClientFactory().getVsDbgVersion('latest', 'linux-x64');
await this.vsDbgClientFactory().getVsDbgVersion('latest', 'linux-musl-x64');
}
const debuggerScriptPath = path.join(ext.context.asAbsolutePath('resources'), 'vsdbg');
const destPath = path.join(NetCoreDebugHelper.getHostDebuggerPathBase(), 'vsdbg');
await fse.copyFile(debuggerScriptPath, destPath);
await fse.chmod(destPath, 0o755); // Give all read and execute permissions
}
private async configureSsl(debugConfiguration: DockerDebugConfiguration, appOutput: string): Promise<void> {
const appOutputName = path.parse(appOutput).name;
const certificateExportPath = path.join(LocalAspNetCoreSslManager.getHostSecretsFolders().certificateFolder, `${appOutputName}.pfx`);
await this.aspNetCoreSslManager.trustCertificateIfNecessary();
await this.aspNetCoreSslManager.exportCertificateIfNecessary(debugConfiguration.netCore.appProject, certificateExportPath);
}
private static getAdditionalProbingPathsArgs(platformOS: PlatformOS): string {
const additionalProbingPaths = platformOS === 'Windows'
? [
'C:\\.nuget\\packages',
'C:\\.nuget\\fallbackpackages'
]
: [
'/root/.nuget/packages',
'/root/.nuget/fallbackpackages'
];
return additionalProbingPaths.map(probingPath => `--additionalProbingPath ${probingPath}`).join(' ');
}
private static getContainerAppOutput(debugConfiguration: DockerDebugConfiguration, appOutput: string, platformOS: PlatformOS): string {
const relativePath = path.relative(path.dirname(debugConfiguration.netCore.appProject), appOutput);
let result = platformOS === 'Windows' ?
path.win32.join('C:\\app', relativePath) :
path.posix.join('/app', relativePath);
return pathNormalize(result, platformOS);
}
}
export const netCoreDebugHelper = new NetCoreDebugHelper();