Skip to content

Commit

Permalink
Add support for one-step debugging for Blazor
Browse files Browse the repository at this point in the history
  • Loading branch information
captainsafia committed May 11, 2020
1 parent 793fcc8 commit e0bf1f0
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"activationEvents": [
"onWebviewPanel:razorReportIssue",
"onDebugInitialConfigurations",
"onDebug",
"onDebugResolve:coreclr",
"onDebugResolve:clr",
"onLanguage:csharp",
Expand Down Expand Up @@ -46,6 +47,44 @@
],
"main": "./dist/extension",
"contributes": {
"breakpoints": [
{
"language": "aspnetcorerazor"
}
],
"debuggers": [
{
"type": "blazor",
"label": "Blazor WebAssembly Debug",
"initialConfigurations": [
{
"type": "blazor",
"name": "Launch and Debug Blazor Application",
"request": "launch"
}
],
"configurationAttributes": {
"launch": {
"properties": {
"env": {
"type": "object",
"description": "Environment variables passed to dotnet"
},
"url": {
"type": "string",
"description": "The URL of the application",
"default": "https://localhost:5001"
},
"browser": {
"type": "string",
"description": "The debugging browser to launch (Edge or Chrome)",
"default": "chrome"
}
}
}
}
}
],
"semanticTokenTypes": [
{
"id": "razorTagHelperElement",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"typescript": "3.3.4000"
},
"dependencies": {
"ps-list": "^7.0.0",
"vscode-html-languageservice": "2.1.7",
"vscode-languageclient": "5.2.1"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* --------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import * as vscode from 'vscode';

import { RazorLogger } from './RazorLogger';

export class BlazorDebugConfigurationProvider implements vscode.DebugConfigurationProvider {
private logger: RazorLogger;
private vscodeType: typeof vscode;

constructor(logger: RazorLogger, vscodeType: typeof vscode) {
this.logger = logger;
this.vscodeType = vscodeType;
}

public resolveDebugConfiguration(folder: vscode.WorkspaceFolder | undefined, configuration: vscode.DebugConfiguration): vscode.ProviderResult<vscode.DebugConfiguration> {
const app = {
name: '.NET Core Launch (Blazor Standalone)',
type: 'coreclr',
request: 'launch',
program: 'dotnet',
args: ['run'],
cwd: '${workspaceFolder}',
env: {
ASPNETCORE_ENVIRONMENT: 'Development',
...configuration.env,
},
};
const browser = {
name: '.NET Core Debug Blazor Web Assembly in Browser',
type: configuration.browser === 'edge' ? 'edge' : 'pwa-chrome',
request: 'launch',
timeout: 30000,
url: configuration.url || 'https://localhost:5001',
webRoot: '${workspaceFolder}',
inspectUri: '{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}',
};

this.vscodeType.debug.startDebugging(folder, app).then(
appStartFulfilled => {
if (appStartFulfilled) {
this.vscodeType.debug.startDebugging(folder, browser).then(
debugStartFulfilled => {
if (debugStartFulfilled) {
this.logger.logVerbose('Launching JavaScript debugger...');
}
},
error => {
this.logger.logError('Error when launching Chrome debugger: ', error);
},
);
}
},
error => {
this.logger.logError('Error when launching application: ', error);
},
);

return undefined;
}
}
30 changes: 30 additions & 0 deletions src/Razor/src/Microsoft.AspNetCore.Razor.VSCode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
* ------------------------------------------------------------------------------------------ */

import { exec } from 'child_process';
import * as psList from 'ps-list';
import * as vscode from 'vscode';
import * as vscodeapi from 'vscode';
import { ExtensionContext } from 'vscode';
import { BlazorDebugConfigurationProvider } from './BlazorDebugConfigurationProvider';
import { CompositeCodeActionTranslator } from './CodeActions/CompositeRazorCodeActionTranslator';
import { RazorCodeActionProvider } from './CodeActions/RazorCodeActionProvider';
import { RazorFullyQualifiedCodeActionTranslator } from './CodeActions/RazorFullyQualifiedCodeActionTranslator';
Expand Down Expand Up @@ -182,6 +185,33 @@ export async function activate(vscodeType: typeof vscodeapi, context: ExtensionC
localRegistrations.length = 0;
});

const provider = new BlazorDebugConfigurationProvider(logger, vscodeType);
context.subscriptions.push(vscodeType.debug.registerDebugConfigurationProvider('blazor', provider));

/**
* On non-Windows platforms, we need to terminate the Blazor
* dev server and its child processes.
*/
vscodeType.debug.onDidTerminateDebugSession(event => {
logger.logVerbose('Terminating debugging session...');
if (process.platform !== 'win32') {
psList().then(processes => {
const devserver = processes.find(
(process: psList.ProcessDescriptor) => !!(process && process.cmd && process.cmd.includes('blazor-devserver')),
);
if (devserver) {
const command = `kill ${devserver.pid}`;
exec(command, (error, stdout, stderr) => {
if (error) {
logger.logError('Error during debug process clean-up: ', error);
}
return logger.logMessage('Debug process clean-up complete.');
});
}
}).catch(error => logger.logError('Error retrieving processes to clean-up: ', error));
}
});

languageServerClient.onStarted(async () => {
await documentManager.initialize();
});
Expand Down
5 changes: 5 additions & 0 deletions src/Razor/src/Microsoft.AspNetCore.Razor.VSCode/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ path-parse@^1.0.6:
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c"
integrity sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==

ps-list@^7.0.0:
version "7.0.0"
resolved "https://registry.yarnpkg.com/ps-list/-/ps-list-7.0.0.tgz#fd740a839786605d257117b899031db9b34b8b4b"
integrity sha512-ZDhdxqb+kE895BAvqIdGnWwfvB43h7KHMIcJC0hw7xLbbiJoprS+bqZxuGZ0jWdDxZEvB3jpnfgJyOn3lmsH+Q==

resolve@^1.3.2:
version "1.12.0"
resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.12.0.tgz#3fc644a35c84a48554609ff26ec52b66fa577df6"
Expand Down

0 comments on commit e0bf1f0

Please sign in to comment.