commit c2b24b64fc523ce1f7bf6825c22da1befa5bd716 parent 6044e4593f6c5104a42deaf5b0b15742fb42409e Author: citbl <citbl@citbl.org> Date: Sun, 24 May 2026 13:51:09 +1000 explain lua errors on demand Diffstat:
| M | mtcl/makefile | | | 47 | ++++++++++++++++++++++++++++++++++++++++++++--- |
1 file changed, 44 insertions(+), 3 deletions(-)
diff --git a/mtcl/makefile b/mtcl/makefile @@ -1,8 +1,49 @@ MAKEFLAGS += --silent +LUA ?= lua +LUA_ENTRY ?= main.lua +LUA_INPUT ?= target.mty +LUA_COMMAND ?= $(LUA) $(LUA_ENTRY) $(LUA_INPUT) + +FMT_COMMAND ?= stylua --column-width 80 . + +EXPLAIN_ENABLED ?= 1 +# EXPLAIN_MODEL ?= google/gemini-2.5-flash-lite +EXPLAIN_MODEL ?= openai/gpt-5.4-mini-fast +# EXPLAIN_MODEL ?= opencode-go/kimi-k2.6 + +EXPLAIN_CMD ?= opencode run --model "$(EXPLAIN_MODEL)" +EXPLAIN_PROMPT ?= Explain this error in concise English. Include the likely cause and a concise fix. Error: +EXPLAIN_TITLE ?= AI explanation for Lua error: +EXPLAIN_MARKER ?= stack traceback: +EXPLAIN_TMP ?= $${TMPDIR:-/tmp}/mtcl-explain.XXXXXX + +# we don't write any code with AI +# but LUA errors can be confusing being all dynamic +# use `make huh`, to ask a model to explain the issue, via opencode. + +define run_with_explain +@tmp="$$(mktemp "$(EXPLAIN_TMP)")"; \ +$(1) >"$$tmp" 2>&1; \ +status=$$?; \ +cat "$$tmp"; \ +if [ $$status -ne 0 ] && [ "$(EXPLAIN_ENABLED)" != "0" ]; then \ + line=$$(awk -v marker="$(EXPLAIN_MARKER)" 'index($$0, marker) { print prev; found=1; exit } { prev = $$0 } END { exit !found }' "$$tmp" 2>/dev/null || true); \ + if [ -n "$$line" ]; then \ + printf '\n%s\n' "$(EXPLAIN_TITLE)"; \ + $(EXPLAIN_CMD) "$(EXPLAIN_PROMPT) $$line" || true; \ + fi; \ +fi; \ +rm -f "$$tmp"; \ +exit $$status +endef + default: - lua main.lua target.mty - stylua --column-width 80 . + $(LUA_COMMAND) + $(FMT_COMMAND) fmt: - stylua --column-width 80 . + $(FMT_COMMAND) + +huh: + $(call run_with_explain,$(LUA_COMMAND))