Skip to content

Add options for handling errors encountered during debugging #717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,27 @@
"tags": [
"experimental"
]
},
"debugpy.onErrors": {
"default": "debugAnyway",
"description": "%debugpy.onErrors.description%",
"scope": "resource",
"type": "string",
"enum": [
"debugAnyway",
"showErrors",
"abort",
"prompt"
],
"enumDescriptions": [
"Continue debugging.",
"Show errors and stop debugging.",
"Stop debugging.",
"Let the user choose the action."
],
"tags": [
"experimental"
]
}
},
"title": "Python Debugger",
Expand Down
3 changes: 2 additions & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"debugpy.command.reportIssue.title": "Report Issue...",
"debugpy.command.viewOutput.title": "Show Output",
"debugpy.debugJustMyCode.description": "When debugging only step through user-written code. Disable this to allow stepping into library code.",
"debugpy.showPythonInlineValues.description": "Whether to display inline values in the editor while debugging."
"debugpy.showPythonInlineValues.description": "Whether to display inline values in the editor while debugging.",
"debugpy.onErrors.description": "Controls what to do when errors are encountered before debugging."
}
49 changes: 49 additions & 0 deletions src/extension/common/onErrorsAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

'use strict';

import { DiagnosticSeverity, l10n, languages, MessageOptions, window } from 'vscode';
import { getConfiguration } from './vscodeapi';

export async function resolveOnErrorsAction(): Promise<OnErrorsActions> {
const onErrors = getConfiguration('debugpy').get<string>('onErrors', OnErrorsActions.debugAnyway);
if (onErrors === OnErrorsActions.debugAnyway) {
return OnErrorsActions.debugAnyway;
}

const hasErrors = languages
.getDiagnostics()
.map((d) => {
return d[1];
})
.flat()
.some((d) => {
return d.severity === DiagnosticSeverity.Error;
});
if (!hasErrors) {
return OnErrorsActions.debugAnyway;
}

if (onErrors === OnErrorsActions.prompt) {
const message = l10n.t('Error exists before debugging.');
const options: MessageOptions = { modal: true };
const actions = [
{ title: 'Debug Anyway', id: OnErrorsActions.debugAnyway },
{ title: 'Show Errors', id: OnErrorsActions.showErrors },
{ title: 'Abort', id: OnErrorsActions.abort, isCloseAffordance: true },
];

const result = await window.showWarningMessage(message, options, ...actions);
return (result?.id as OnErrorsActions) ?? OnErrorsActions.abort;
}

return onErrors as OnErrorsActions;
}

export enum OnErrorsActions {
debugAnyway = 'debugAnyway',
showErrors = 'showErrors',
abort = 'abort',
prompt = 'prompt',
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import { cloneDeep } from 'lodash';
import { CancellationToken, DebugConfiguration, QuickPickItem, WorkspaceFolder } from 'vscode';
import { CancellationToken, commands, DebugConfiguration, QuickPickItem, WorkspaceFolder } from 'vscode';
import { DebugConfigStrings } from '../../common/utils/localize';
import { IMultiStepInputFactory, InputStep, IQuickPickParameters, MultiStepInput } from '../../common/multiStepInput';
import { AttachRequestArguments, DebugConfigurationArguments, LaunchRequestArguments } from '../../types';
Expand All @@ -17,6 +17,7 @@ import { buildPyramidLaunchConfiguration } from './providers/pyramidLaunch';
import { buildRemoteAttachConfiguration } from './providers/remoteAttach';
import { IDebugConfigurationResolver } from './types';
import { buildFileWithArgsLaunchDebugConfiguration } from './providers/fileLaunchWithArgs';
import { OnErrorsActions, resolveOnErrorsAction } from '../../common/onErrorsAction';

export class PythonDebugConfigurationService implements IDebugConfigurationService {
private cacheDebugConfig: DebugConfiguration | undefined = undefined;
Expand Down Expand Up @@ -48,6 +49,15 @@ export class PythonDebugConfigurationService implements IDebugConfigurationServi
debugConfiguration: DebugConfiguration,
token?: CancellationToken,
): Promise<DebugConfiguration | undefined> {
const action = await resolveOnErrorsAction();
switch (action) {
case OnErrorsActions.showErrors:
await commands.executeCommand('workbench.panel.markers.view.focus');
return undefined;

case OnErrorsActions.abort:
return undefined;
}
if (debugConfiguration.request === 'attach') {
return this.attachResolver.resolveDebugConfiguration(
folder,
Expand Down
20 changes: 20 additions & 0 deletions src/extension/extensionInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
'use strict';

import {
commands,
ConfigurationChangeEvent,
debug,
DebugConfigurationProviderTriggerKind,
Expand Down Expand Up @@ -54,6 +55,7 @@ import { registerHexDebugVisualizationTreeProvider } from './debugger/visualizer
import { PythonInlineValueProvider } from './debugger/inlineValue/pythonInlineValueProvider';
import { traceLog } from './common/log/logging';
import { registerNoConfigDebug } from './noConfigDebugInit';
import { OnErrorsActions, resolveOnErrorsAction } from './common/onErrorsAction';

export async function registerDebugger(context: IExtensionContext): Promise<IExtensionApi> {
const childProcessAttachService = new ChildProcessAttachService();
Expand Down Expand Up @@ -87,6 +89,15 @@ export async function registerDebugger(context: IExtensionContext): Promise<IExt

context.subscriptions.push(
registerCommand(Commands.Debug_In_Terminal, async (file?: Uri) => {
const action = await resolveOnErrorsAction();
switch (action) {
case OnErrorsActions.showErrors:
await commands.executeCommand('workbench.panel.markers.view.focus');
return;

case OnErrorsActions.abort:
return;
}
traceLog("Debugging using the editor button 'Debug in terminal'");
sendTelemetryEvent(EventName.DEBUG_IN_TERMINAL_BUTTON);
const interpreter = await getInterpreterDetails(file);
Expand All @@ -101,6 +112,15 @@ export async function registerDebugger(context: IExtensionContext): Promise<IExt

context.subscriptions.push(
registerCommand(Commands.Debug_Using_Launch_Config, async (file?: Uri) => {
const action = await resolveOnErrorsAction();
switch (action) {
case OnErrorsActions.showErrors:
await commands.executeCommand('workbench.panel.markers.view.focus');
return;

case OnErrorsActions.abort:
return;
}
traceLog("Debugging using the editor button 'Debug using the launch.json'");
sendTelemetryEvent(EventName.DEBUG_USING_LAUNCH_CONFIG_BUTTON);
const interpreter = await getInterpreterDetails(file);
Expand Down