Debugging in VS Code
You debug a Novis program in VS Code with the editor extension. The extension
starts nvs dap, the debug adapter that is part of the nvs program. You do not run nvs dap
yourself.
While the program runs, you can stop it at a breakpoint and step through the code. The Variables view shows the value of each variable. What the program prints appears in the debug console.
The launch configuration
Section titled “The launch configuration”Add a configuration of type nvs to .vscode/launch.json:
{ "version": "0.2.0", "configurations": [ { "type": "nvs", "request": "launch", "name": "Run file", "mode": "run", "program": "${file}" } ]}program is the .nvs file to debug. ${file} is the file that is open in the editor. This field
is required.
mode says how the program runs:
mode |
What it does |
|---|---|
run |
Runs the program, as nvs run does. This is the default. |
test |
Runs the #[Test] methods of the program, as nvs test does. The tests run one at a time. |
serve |
Starts a server, as nvs serve does. The server runs in development mode. |
You can also start a configuration from the Run and Debug view. When you add a configuration to
launch.json, VS Code offers one for each mode.
Stop at a breakpoint
Section titled “Stop at a breakpoint”Click left of a line number to set a breakpoint. Then press F5 to start debugging. The program stops before it runs that line.
Here the breakpoint is on the line in the loop. Each F5 continues the program until it reaches
the breakpoint again. The Variables view shows $count and $total at each stop.
What this recording shows
- Set a breakpoint on line 7 and start debugging. The program stops on line 7.
$wordsisarray(3),$totalis0and$countis120. - Press F5 to continue. The program stops on line 7.
$wordsisarray(3),$totalis120and$countis340. - Press F5 to continue. The program stops on line 7.
$wordsisarray(3),$totalis460and$countis95. - Press F5 to continue. The program runs to its end. It ends with exit code
0.
A breakpoint can also have a condition, a hit count or a log message. Right-click the breakpoint and choose Edit Breakpoint to add one.
- A condition is an expression, such as
$count > 200. The program stops only when the condition is true. A condition can read variables and properties. It cannot call a function or a method. - A hit count is a number, such as
3. The program stops from the third time on. You can also write>3,>=3,==3or%3. - A log message prints a line in the debug console, and the program does not stop. A part in
{}is an expression, such astotal is {$total}.
Step through the code
Section titled “Step through the code”When the program has stopped, you can run it one step at a time:
| Key | Step | What it does |
|---|---|---|
| F10 | Step over | Runs the current line and stops at the next one. |
| F11 | Step into | Goes into the method that the line calls. |
| Shift+F11 | Step out | Runs the rest of the method and stops in the code that called it. |
| F5 | Continue | Runs until the next breakpoint or the end of the program. |
Here the program steps into Shop::withShipping, steps over one line, and steps out again. After the
step out, the Variables view shows Shop::withShipping() returned first. Its value is 2450.
What this recording shows
- Set a breakpoint on line 14 and start debugging. The program stops on line 14.
$priceis2000. - Press F11 to step into the call. The program stops on line 7 in
Shop::withShipping.$centsis2000. - Press F10 to step over the line. The program stops on line 8 in
Shop::withShipping.$centsis2000and$shippingis450. - Press Shift+F11 to step out of the method. The program stops on line 15.
Shop::withShipping() returnedis2450,$priceis2000and$totalis2450. - Press F5 to continue. The program runs to its end. It ends with exit code
0.
The returned value is shown only when the method has a return type. A step into a Core method
steps over it.
A secret value, such as a password, is hidden in the Variables view.
Stop at an exception
Section titled “Stop at an exception”In the Breakpoints view, Uncaught Exceptions is on by default. The program stops at a throw
when no catch handles the exception. All Exceptions stops the program at every throw.
See the tasks of the program
Section titled “See the tasks of the program”Each task of the program is a thread in the Call Stack view. When one task stops, every task of the same request stops.
While the program is stopped, the Novis Tasks view in Run and Debug shows each task under the
task that started it. For example, the functions you give to Core\Task::all are shown under the
task that called it. A task that waits for its child tasks is shown as waiting. Click a stopped
task to open its file at the line where it stopped.
What the debugger cannot do
Section titled “What the debugger cannot do”nvs dap only starts programs. It cannot attach to a program that is already running. So you
cannot debug a server that is already running, such as a production server.
Read more in Reference: nvs dap.
