Debugging in the terminal
You don't need an editor to debug a script. kscript --debug runs it in the terminal and pauses before its first statement. It shows where it is, with the lines around it, and waits for a command:
$ kscript --debug basket.kscript
Paused before the first line in basket.kscript:7
5 │ }
6 │
→ 7 │ $prices = [2.5, 4, 1.25]
8 │ $quantities = [2, 1, 4]
9 │ $basket_total = 0
Type h for the commands, n to run a line.
debug>--debug goes before the script's name, as kscript run --debug basket.kscript or just kscript --debug basket.kscript, because what follows the name is the script's $args. kscript run --debug on its own debugs the project's main script.
To pause further on instead, give --break with a line number. The script runs until it reaches that line. Give --break once for each line, and write file:line for a line in another script, such as one the script imports:
$ kscript --break 11 --break lib/prices.kscript:4 basket.kscriptThese are the commands. Each has a one-letter short form:
| Command | What it does |
|---|---|
n, next |
Runs the line and pauses on the next one. A function the line calls runs whole. Enter on its own repeats the last step. |
s, step |
Pauses on the first line inside the function the line calls. On a line with no call, the same as n. |
o, out |
Runs the rest of the function and pauses on the line after the call. |
c, continue |
Runs until the next breakpoint, or the end. |
p, print |
Prints a variable or works out an expression with the paused line's variables: p $basket_total, p $prices->sum(). |
v, vars |
Lists the variables the line sees: Locals, then Globals. |
w, where |
Shows the call stack: each function the script is inside, innermost first. |
l, list |
Shows the lines around the paused line again. |
b, break |
b 12 adds a breakpoint at line 12 (or b lib/prices.kscript:4). On its own, lists them. |
d, delete |
d 12 removes the breakpoint at line 12. |
q, quit |
Stops the script where it is. Nothing more runs. |
h, help |
Lists the commands. |
Here is the basket script, run with --break 11, stepping into line_total() and printing what it worked out:
Paused at a breakpoint in basket.kscript:11
9 │ $basket_total = 0
10 │ for price at index in $prices {
→ 11 │ $basket_total += @line_total($price, quantity: $quantities[$index])
12 │ }
13 │ echo "Basket: {$basket_total}"
debug> s
Paused in line_total(), basket.kscript:3
1 │ // basket.kscript
2 │ fn line_total(price: number, quantity: int) {
→ 3 │ $total = $price * $quantity
4 │ return $total
5 │ }
debug> n
Paused in line_total(), basket.kscript:4
debug> p $total
5 number
debug> w
→ line_total(), basket.kscript:4
basket.kscript:11A breakpoint on a blank line or a comment moves down to the next statement, as in the editor. An error pauses the script where it is thrown, with its message, so you can print the variables that led to it. The script's own output prints in the terminal between the stops. Since the terminal's input is for the debugger's commands, the script reads none: @ask() gets nothing back.
To see the .NET exception and stack trace behind an error, which --debug showed before it ran the debugger, use --stack-trace.