diff --git a/src/client/debugger/jupyter/kernelDebugAdapter.ts b/src/client/debugger/jupyter/kernelDebugAdapter.ts index 65a564c3333..042899b4e5a 100644 --- a/src/client/debugger/jupyter/kernelDebugAdapter.ts +++ b/src/client/debugger/jupyter/kernelDebugAdapter.ts @@ -32,6 +32,8 @@ import { Commands, Identifiers } from '../../datascience/constants'; import { IKernel } from '../../datascience/jupyter/kernels/types'; import { sendTelemetryEvent } from '../../telemetry'; import { DebuggingTelemetry } from '../constants'; +import { parseForComments } from '../../../datascience-ui/common'; +import { noop } from '../../common/utils/misc'; interface dumpCellResponse { sourcePath: string; // filename for the dumped source @@ -496,31 +498,47 @@ export class KernelDebugAdapter implements DebugAdapter, IKernelDebugAdapter, ID await this.dumpCell(cell.document.uri.toString()); if (this.configuration.__mode === KernelDebugMode.RunByLine) { - const initialBreakpoint: DebugProtocol.SourceBreakpoint = { - line: 1 - }; - const splitPath = cell.notebook.uri.path.split('/'); - const name = splitPath[splitPath.length - 1]; - const message: DebugProtocol.SetBreakpointsRequest = { - seq: seq + 1, - type: 'request', - command: 'setBreakpoints', - arguments: { - source: { - name: name, - path: cell.document.uri.toString() - }, - lines: [1], - breakpoints: [initialBreakpoint], - sourceModified: false + // This will save the code lines of the cell in lineList (so ignore comments and emtpy lines) + // Its done to set the Run by Line breakpoint on the first code line + const textLines = cell.document.getText().splitLines({ trim: false, removeEmptyEntries: false }); + const lineList: number[] = []; + parseForComments( + textLines, + () => noop(), + (s, i) => { + if (s.trim().length !== 0) { + lineList.push(i); + } } - }; - this.sendRequestToJupyterSession(message); + ); + lineList.sort(); + + // Don't send the SetBreakpointsRequest or open the variable view if there are no code lines + if (lineList.length !== 0) { + const initialBreakpoint: DebugProtocol.SourceBreakpoint = { + line: lineList[0] + 1 + }; + const message: DebugProtocol.SetBreakpointsRequest = { + seq: seq + 1, + type: 'request', + command: 'setBreakpoints', + arguments: { + source: { + name: path.basename(cell.notebook.uri.path), + path: cell.document.uri.toString() + }, + lines: [lineList[0] + 1], + breakpoints: [initialBreakpoint], + sourceModified: false + } + }; + this.sendRequestToJupyterSession(message); - // Open variable view - const settings = this.settings.getSettings(); - if (settings.showVariableViewWhenDebugging) { - await this.commandManager.executeCommand(Commands.OpenVariableView); + // Open variable view + const settings = this.settings.getSettings(); + if (settings.showVariableViewWhenDebugging) { + void this.commandManager.executeCommand(Commands.OpenVariableView); + } } }