Liking cljdoc? Tell your friends :D

Debug Macro (dbg) Implementation Plan

For agentic workers: REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (- [ ]) syntax for tracking.

Goal: Add a dbg macro that prints a labelled, pretty-printed value and returns it, enabled by bb aoc solve --debug / bb aoc test --debug and expanded away entirely otherwise.

Architecture: A new aoc-clj-elf.debug namespace holds a settings atom and the dbg macro. The macro reads settings at expansion time: when disabled it expands to the bare form. The runner sets settings from the CLI options before it requires the solution namespace, so the decision is made as the solution file loads.

Tech Stack: Babashka (also JVM Clojure), babashka.cli, babashka.fs, bling, clojure.pprint.

Spec: docs/superpowers/specs/2026-12-04-debug-macro-design.md

Global Constraints

  • dbg takes exactly one argument (works in both -> and ->>)
  • Default settings: {:enabled? true :pretty? true :print-length 100 :print-level 10}; :enabled? defaults to true (REPL use)
  • When :enabled? is false at expansion time, (dbg form) expands to exactly form
  • Label format: [<file>:<line>] <form> =>, <file> relative to the working directory, :<line> left out when no line metadata is available; label dimmed with bling
  • --debug (default false) and --pretty/--no-pretty (default true) only on solve and test
  • Output goes to *out*
  • Release version: 0.4.0 (project.clj and aoc-clj-elf.init/version must match; an existing test enforces this)
  • The per-command task fns (solve-task, run-tests-task, ...) keep their current signatures (0.2.x bb.edn files must keep working)

Review Focus

  1. dbg around an infinite lazy seq ((dbg (cycle [1 2]))) must print a truncated value and return, not hang (bounded by *print-length*): test in Task 1
  2. The wrapped form is evaluated exactly once, so side effects aren't doubled: test in Task 1
  3. Falsey values (nil, false) are returned as-is: test in Task 1
  4. A form that throws propagates the same exception, and the runner isn't left in a broken state: covered by the let expansion. No extra test needed beyond Task 1's "returns value" tests
  5. bb aoc solve without --debug must not print debug output, even though the REPL default is on: verified end to end in Task 2

Task 1: aoc-clj-elf.debug namespace and dbg macro

Files:

  • Create: src/aoc_clj_elf/debug.clj
  • Create: test/aoc_clj_elf/debug_test.clj
  • Modify: bb.edn (the test task requires and runs both aoc-clj-elf.core-test and aoc-clj-elf.debug-test; exit 1 if either has failures or errors)

Interfaces:

  • Produces:

    • aoc-clj-elf.debug/settings: atom, default {:enabled? true :pretty? true :print-length 100 :print-level 10} (use defonce)
    • (aoc-clj-elf.debug/configure! {:keys [debug pretty]}): sets :enabled? to (boolean debug) and :pretty? to (not (false? pretty)); returns the new settings
    • (aoc-clj-elf.debug/dbg form): macro
    • (aoc-clj-elf.debug/print-debug label value): fn used by the expansion; prints using the current settings
  • [ ] Step 1: Write the failing tests in test/aoc_clj_elf/debug_test.clj

Use a fixture (use-fixtures :each) that saves @settings and restores it afterwards. Strip ANSI codes from captured output before asserting ((str/replace s #"\x1b\[[0-9;]*m" "")).

(deftest disabled-expands-to-bare-form
  (reset! debug/settings (assoc @debug/settings :enabled? false))
  (is (= '(inc x) (macroexpand-1 '(aoc-clj-elf.debug/dbg (inc x))))))

(deftest returns-value-and-prints-label
  ;; enabled (the default) when this file was loaded
  (let [out (with-out-str (is (= 3 (dbg (+ 1 2)))))]
    (is (str/includes? (strip out) "debug_test.clj:"))
    (is (str/includes? (strip out) "(+ 1 2) =>"))
    (is (str/includes? (strip out) "3"))))

(deftest file-is-relative-to-working-directory
  (let [out (strip (with-out-str (dbg 1)))]
    (is (str/includes? out "[test/aoc_clj_elf/debug_test.clj:"))))

(deftest works-in-thread-last
  (let [out (strip (with-out-str
                     (is (= 9 (->> (range 3) (map inc) dbg (reduce +))))))]
    (is (str/includes? out "(map inc (range 3)) =>"))
    (is (str/includes? out "(1 2 3)"))))

(deftest evaluates-form-once
  (let [n (atom 0)]
    (with-out-str (dbg (swap! n inc)))
    (is (= 1 @n))))

(deftest falsey-values-returned
  (with-out-str
    (is (nil? (dbg nil)))
    (is (false? (dbg false)))))

(deftest infinite-seq-is-truncated
  (let [out (strip (with-out-str (is (= [1 2] (take 2 (dbg (cycle [1 2])))))))]
    (is (str/includes? out "..."))))

(deftest pretty-vs-single-line
  (let [big (vec (range 40))]
    (reset! debug/settings (assoc @debug/settings :pretty? true))
    (is (< 2 (count (str/split-lines (strip (with-out-str (debug/print-debug "[x] big =>" {:a big :b big})))))))
    (reset! debug/settings (assoc @debug/settings :pretty? false))
    (is (= 1 (count (str/split-lines (strip (with-out-str (debug/print-debug "[x] big =>" {:a big :b big})))))))))

(deftest configure-sets-flags
  (is (= [false true] ((juxt :enabled? :pretty?) (debug/configure! {}))))
  (is (= [true false] ((juxt :enabled? :pretty?) (debug/configure! {:debug true :pretty false})))))
  • [ ] Step 2: Run the tests and see them fail

Run: bb test Expected: an error that aoc-clj-elf.debug can't be found

  • [ ] Step 3: Implement src/aoc_clj_elf/debug.clj

  • dbg: if (:enabled? @settings) is false at expansion time return form; otherwise expand to (let [v# form] (print-debug <label> v#) v#)

  • The label is built at expansion time as a string: [<file>:<line>] <pr-str of form> =>

    • line: (or (:line (meta &form)) (:line (meta form)))
    • file: *file* made relative to (fs/cwd) with fs/relativize when it is inside the working directory, otherwise used as is; leave out :<line> when there's no line
  • print-debug binds *print-length*/*print-level* from settings

    • when :pretty?: print the dimmed label on its own line, then clojure.pprint/pprint the value
    • otherwise: print the dimmed label, a space, and (pr-str value) on one line
    • dim the label with bling (for example (bling/bling [:subtle label]); check the bling 0.8.8 API)
  • [ ] Step 4: Run the tests and see them pass

Run: bb test Expected: all tests in both namespaces pass, 0 failures, 0 errors

  • [ ] Step 5: Commit
git add src/aoc_clj_elf/debug.clj test/aoc_clj_elf/debug_test.clj bb.edn
git commit -m "Add dbg debug macro"

Task 2: --debug / --no-pretty on solve and test

Files:

  • Modify: src/aoc_clj_elf/util.clj (option specs)
  • Modify: src/aoc_clj_elf/core.clj (run-for-puzzle, solve-task, run-tests-task, gen-new-task, get-input-task)
  • Modify: src/aoc_clj_elf/runner.clj (load-solution-ns, run-solution, run-tests)
  • Test: test/aoc_clj_elf/core_test.clj

Interfaces:

  • Consumes: aoc-clj-elf.debug/configure! (Task 1)

  • Produces:

    • aoc-clj-elf.util/debug-input-spec: general-input-spec plus :debug {:coerce :boolean :desc "Print dbg output from the solution"} and :pretty {:coerce :boolean :desc "Pretty-print dbg output (use --no-pretty for one line per value)"}. Build both specs from one shared puzzle option map so they don't drift; keep :help last in the help order
    • run-for-puzzle: becomes [spec args f], and f is called with the resolved opts map ({:year :day ...}) instead of (f year day)
    • (runner/run-solution auth-file input-dir year day) and (runner/run-tests year day) keep working; new extra arity takes a final debug-opts map ({:debug bool :pretty bool}), defaulting to {}
    • load-solution-ns calls (debug/configure! debug-opts) before require
  • [ ] Step 1: Write the failing test in core_test.clj

(deftest debug-input-spec-test
  (is (= {:debug true :pretty false :day 3}
         (select-keys (cli/parse-opts ["--debug" "--no-pretty" ":day" "3"] util/debug-input-spec)
                      [:debug :pretty :day])))
  (is (str/includes? (util/show-help util/debug-input-spec) "--debug"))
  (is (not (str/includes? (util/show-help util/general-input-spec) "--debug"))))
  • [ ] Step 2: Run the tests and see them fail

Run: bb test Expected: an error that util/debug-input-spec can't be resolved

  • [ ] Step 3: Implement the spec, the run-for-puzzle change, and the runner arities

solve-task and run-tests-task use debug-input-spec and pass (select-keys opts [:debug :pretty]) to the runner; gen-new-task and get-input-task keep general-input-spec.

  • [ ] Step 4: Run the tests and see them pass

Run: bb test Expected: 0 failures, 0 errors

  • [ ] Step 5: End-to-end check in a scratch project

In an empty scratch folder: run bb -Sdeps '{:deps {io.github.jjcomer/aoc-clj-elf {:local/root "/Users/jcomer/code/aoc-clj-elf"}}}' -m aoc-clj-elf.core init, then point the generated bb.edn dep at {:local/root "/Users/jcomer/code/aoc-clj-elf"}. Write src/y2026/d4.clj with a dbg in the generator, in solve-part-1, and in a deftest, plus input/2026/4.txt. Expected:

  • bb aoc solve :year 2026 :day 4: results only, no dbg output

  • bb aoc solve :year 2026 :day 4 --debug: labelled, multi-line output before the results

  • bb aoc solve :year 2026 :day 4 --debug --no-pretty: one line per dbg

  • bb aoc test :year 2026 :day 4 --debug: dbg output from the test; without --debug, none

  • bb aoc solve :help lists --debug; bb aoc new :help does not

  • [ ] Step 6: Commit

git add src/aoc_clj_elf/util.clj src/aoc_clj_elf/core.clj src/aoc_clj_elf/runner.clj test/aoc_clj_elf/core_test.clj
git commit -m "Add --debug and --no-pretty to solve and test"

Task 3: Template, docs, version 0.4.0

Files:

  • Modify: resources/solution.template (ns :require gains [aoc-clj-elf.debug :refer [dbg]])

  • Modify: README.md

  • Modify: project.clj, src/aoc_clj_elf/init.clj (version → "0.4.0"), and the 0.3.0 version strings in README setup snippets → 0.4.0

  • [ ] Step 1: Update the template and version, then run tests

Run: bb test Expected: 0 failures, 0 errors (the version-sync test confirms project.clj and init/version agree)

  • [ ] Step 2: Check the template in the scratch project

bb aoc new :year 2024 :day 1 creates a file whose ns requires aoc-clj-elf.debug; bb aoc test :year 2024 :day 1 passes.

  • [ ] Step 3: README

  • Add a "Debugging" section after "Running your Solution" with:

    • an example ->> pipeline using dbg
    • sample output
    • --debug / --no-pretty
    • "without --debug the calls are removed when the solution loads, so they cost nothing"
    • the REPL default (on)
    • that debug output is included in timings when on
    • that existing solution files need [aoc-clj-elf.debug :refer [dbg]] added to their ns
  • Add a 0.4.0 changelog entry

  • Leave Future Features unchanged ("Optionally pprint solution output" is about results, not dbg)

  • [ ] Step 4: Commit

git add resources/solution.template README.md project.clj src/aoc_clj_elf/init.clj
git commit -m "Require dbg in the solution template; docs and 0.4.0"

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close