Nim debugging

From planetis-m/skills · View interactive version

Nim debugging is an AI agent skill from the GitHub repository planetis-m/skills. Debug Nim compile failures, crashes, wrong results, macro expansion, ownership behavior, and unsafe-memory defects using focused reproduction, stack traces, inspection, compiler expansion, and sanitizers. Use when diagnosing runtime failures, release-only behavior, generic or macro issues, unexpecte

How to use this skill

Skill file

Nim Debugging

Debug by reducing uncertainty: preserve the failure, state one hypothesis, and collect the smallest piece of evidence that can reject it. Find the first violated invariant rather than stopping at the final crash.

Rules

Preserve the failure

thread setting, build mode, and relevant environment.

preserve a seed, event order, or failing fixture.

fails is a comparison point, not a reproduction.

instrumentation.

Choose evidence by failure class

| Failure | First evidence |

| --- | --- |

| Compile-time type or generic question | Normal compiler diagnostic; use compiles only for a yes/no probe |

| Unhandled exception or defect | Default stack trace, or --lineTrace:on in the failing optimized mode |

| Wrong runtime value | echo, debugEcho, or repr at invariant boundaries |

| Buffered output lost at a crash | stdout.write followed by stdout.flushFile() |

| Macro-generated code | --expandMacro:<name> |

| Unexpected copy, move, or destruction | --expandArc:<proc> under the project memory manager |

| Pointer, manual allocation, or FFI memory error | AddressSanitizer with the complete required recipe |

| Independent leak or invalid-access check | Valgrind |

Stack traces and build modes

Default build behavior:

| Mode | Unhandled exception trace | writeStackTrace() |

| --- | --- | --- |

| default or -d:debug | full paths and line numbers | full trace |

| -d:release | raising frame only | no traceback available |

| -d:danger | raising frame only | no traceback available |

call chain and line numbers. It implies --stackTrace:on.

on form includes full paths.

exists.

until the first invalid state is found.

Runtime inspection

formatting.

there.

could occur before the buffer drains.

dumping large structures when one length, index, tag, or identity answers

the hypothesis.

compile the candidate normally to obtain the actual diagnostic.

subtype. Use when, not if, for code that depends on a field's name or type.

Compiler-generated code

The compiler emits an [ExpandMacro] hint.

and destroy operations.

target project's --mm: mode.

module top level.

reason to optimize by itself. move is destructive and requires deliberate

last use plus behavior tests.

configuration may select ARC or atomic ARC.

AddressSanitizer

Use ASan for ptr, addr, cstring, cstringArray, manual allocation, and

C-FFI memory faults. The required Linux/GCC recipe is:

nim c \
  --passC:"-fsanitize=address -fno-omit-frame-pointer" \
  --passL:"-fsanitize=address -fno-omit-frame-pointer" \
  -g \
  -d:useMalloc \
  -d:noSignalHandler \
  file.nim

allocator.

reports signal faults.

ensure llvm-symbolizer is in PATH.

editing code.

See references/memory_sanitizers.md for a complete runnable example. Platform

recipes not verified in this repository are intentionally omitted.

Valgrind

Use Valgrind as an independent native-memory check:

nim c -g -o:program file.nim
valgrind --leak-check=full --error-exitcode=1 ./program

It requires no sanitizer instrumentation and is slower than ASan. A non-zero

exit distinguishes detected errors in automated checks.

Debug symbols and native debuggers

variable inspection slower and less reliable than stack traces, focused

inspection, ASan, Valgrind, and compiler expansion.

Workflow

  1. Reproduce the failure with an exact command and fixed input.
  2. Minimize unrelated setup while keeping the same failure and configuration.
  3. State one falsifiable hypothesis and the invariant it predicts.
  4. Select the narrowest evidence source from the failure-class table.
  5. Follow the failing path backward and locate the first invalid state.
  6. Fix the invariant at its owner instead of suppressing the final symptom.
  7. Re-run the original reproduction and relevant sanitizer or expansion check.
  8. Add a regression test, then remove temporary prints and scratch artifacts.

Common Mistakes

| Mistake | Why it is wrong |

| --- | --- |

| Changing input, build mode, and code together | The result cannot identify which change affected the failure |

| Reading only the final crashing line | Corruption or invalid state commonly begins earlier |

| Expecting full traces from release or danger defaults | Add --lineTrace:on to the failing optimized command |

| Using compiles as a replacement for compiler diagnostics | It answers only yes or no and suppresses the candidate diagnostic |

| Printing with stdout.write immediately before a crash without flushing | Buffered evidence can be lost |

| Running ASan without -d:useMalloc | Nim allocator overflows can remain invisible to ASan |

| Running ASan without -d:noSignalHandler | Nim can intercept signal faults before ASan reports them |

| Passing ASan flags only to C compilation | The sanitizer runtime must also be linked |

| Using Clang ASan without llvm-symbolizer | Reports can contain raw addresses instead of Nim locations |

| Adding move merely because --expandArc shows a copy | move changes ownership semantics and must be safe at last use |

| Using GDB for routine Nim debugging | The project uses faster Nim-aware evidence sources instead |

References

behind a crash.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.