KScript docs
EN

Debugging

Breakpoints

A breakpoint pauses the script just before its line runs. Click in the margin next to a line number to add one, and click the red dot again to remove it. F9 does the same on the line the cursor is on.

A breakpoint on a line with no statement, such as a blank line, a comment or a closing }, moves down to the next line that has one. The script would never stop on the line you picked, so VS Code shows the dot where it will stop.

When a statement and its body share a line, the line is one stop. Here the debugger pauses on line 2 once, not again for the echo inside it:

<let name="ready" value="true" />
<if $ready>
  <echo>ready to go</echo>
</if>
<echo>done</echo>
$ready = true
if $ready { echo 'ready to go' }
echo 'done'

Conditional breakpoints

A breakpoint inside a loop pauses on every pass. To pause only on the pass you care about, right-click the red dot, choose Edit Breakpoint, and give it a condition. The condition is any KScript expression, worked out each time the line is reached with the variables the line sees:

<!-- orders.kscript -->
<let name="orders">[
    { id: 101, total: 25 },
    { id: 102, total: 0 },
    { id: 103, total: 40 },
  ]</let>
<for each="order" in="$orders">
  <!-- A breakpoint here with the condition $order.total == 0 pauses on order 102 only. -->
  <echo>Order {$order.id}: {$order.total}</echo>
</for>
// orders.kscript
$orders = [
  { id: 101, total: 25 },
  { id: 102, total: 0 },
  { id: 103, total: 40 },
]
for order in $orders {
  // A breakpoint here with the condition $order.total == 0 pauses on order 102 only.
  echo "Order {$order.id}: {$order.total}"
}

A condition that has a mistake in it, such as a variable the line can't see, pauses the script, so you can see what went wrong rather than miss the stop.

Hit counts

A hit count pauses the script when the line has been reached a number of times. Choose Hit Count in the same box. 3 (or >= 3) pauses on the third time and every time after it, == 3 pauses on the third time only, > 3 from the fourth time on, and % 3 on every third time. A breakpoint with a condition and a hit count counts only the times its condition holds.

Log points

A log point writes a message to the Debug Console instead of pausing. It is an echo you don't have to add to the script, and remove again afterwards. Right-click in the margin, choose Add Logpoint, and write the message. Each {expression} in it is filled in, as in a double-quoted string:

<!-- stock.kscript -->
<let name="stock" value="{ apples: 12, pears: 0, plums: 5 }" />
<for each="item, count" in="$stock">
  <!--
    A log point here with the message "checking {$item}: {$count} left" writes
    checking apples: 12 left, checking pears: 0 left and checking plums: 5 left.
  -->
  <if $count == 0>
    <echo>{$item} sold out</echo>
  </if>
</for>
// stock.kscript
$stock = { apples: 12, pears: 0, plums: 5 }
for item, count in $stock {
  // A log point here with the message "checking {$item}: {$count} left" writes
  // checking apples: 12 left, checking pears: 0 left and checking plums: 5 left.
  if $count == 0 => echo "{$item} sold out"
}

Edit this page on GitHub