Skip to content

fix(errors): render exception messages lazily so ANSI gating holds - #386

Merged
davydog187 merged 2 commits into
mainfrom
fix/exception-defer-ansi-render
Jul 14, 2026
Merged

davydog187 merged 2 commits into
mainfrom
fix/exception-defer-ansi-render

Conversation

@davydog187

Copy link
Copy Markdown
Contributor

Problem

Closes #384. Follow-up to #382 / #385 — same root cause, one level deeper.

Every Lua exception computed its fully-formatted, ANSI-colored message eagerly in exception/1 and stored it. Construction happens during VM execution, where IO.ANSI.enabled?/0 is true, so ErrorFormatter.color/2 (and the parser formatter) emitted escape codes that got frozen into the struct. Later — in a log file, container stdout, or a Sentry/AppSignal title — the codes (and the multi-line render) survived, because the TTY gate fired at build time and never again at the point output is written.

ex = Lua.VM.RuntimeError.exception(value: "kaboom", source: "demo", line: 1)
Application.put_env(:elixir, :ansi_enabled, false)
Exception.message(ex)
#=> before: "\e[2mat demo:1:\e[0m\n\n  runtime error: kaboom"   # ANSI survived the gate
#=> after:  "at demo:1:\n\n  runtime error: kaboom"             # gated at render time

Fix

Render at Exception.message/1 call time instead of construction, mirroring the pattern Lua.VM.ArgumentError already used.

  • RuntimeError, TypeError, AssertionError — drop the memoized :message field; add a lazy message/1 that formats from the stored fields.
  • Lua.RuntimeException wrapper — stop memoizing the wrapped exception's render; the generic (VM-wrapping) clause defers to message/1. The terminal-independent clauses (:lua_error, :api_error, keyword-list, binary) still memoize their ANSI-free strings.
  • CompilerException — store structured parse diagnostics + source and render colored lazily via Parser.Error.format/2. The :errors field now holds bare, ANSI-free messages (log-friendly). parse_chunk/1 and eval! feed structured errors via Parser.parse_structured/1.

The rendered text is unchanged — only when it renders. Multi-line rich output is preserved (matches Elixir's own exceptions); structured/one-line logging remains available via to_map/2 and the now-clean :errors field.

Lua semantics unaffected

pcall/xpcall hand Lua code the :value/:lua_value/raw_message view via ProtectedCall.error_value/1 — the rich Exception.message/1 is host-facing only. Protected-call return values are byte-for-byte unchanged (guarded by pcall_error_value_test.exs, pcall_test.exs).

Tests

  • Tests that read the frozen :message struct field now call Exception.message/1 (error_messages_test.exs, runtime_exception_test.exs). Removing the frozen field forces the correct accessor.
  • Reconciles fix(parser): gate parse-error ANSI on IO.ANSI.enabled?/0 #385's error_ansi_test.exs: the color assertion moves from the .errors field to Exception.message/1 (the field is now ANSI-free by design).
  • New exception_ansi_gating_test.exs (async: false) covers each VM exception, the wrapper, and the parse path in both ANSI directions (off → no escapes, on → escapes) — proving a gate, not a strip.

Full suite: 2590 passed, 7 skipped, 30 excluded (+10 new).

Note — minor breaking change

The :message struct field is removed from Lua.VM.RuntimeError/TypeError/AssertionError and is nil on the RuntimeException wrapper when it wraps a VM exception. Consumers must use Exception.message/1 (the idiomatic accessor) rather than reading e.message directly. This is the correction that makes the gate work; it lands before the 1.0 freeze.

Exceptions computed their fully-formatted, ANSI-colored message eagerly in
exception/1 and stored it. Construction happens during VM execution where
IO.ANSI.enabled?/0 is true, so escape codes were frozen into the struct and
survived into non-TTY sinks (log files, container stdout, Sentry titles) even
though ErrorFormatter/Parser.Error gate color correctly — the gate fired at
build time and never again at output time.

Render at Exception.message/1 call time instead, mirroring the pattern
Lua.VM.ArgumentError already used:

- RuntimeError, TypeError, AssertionError: drop the memoized :message field;
  add a lazy message/1 that formats from the stored fields.
- RuntimeException wrapper: stop memoizing the wrapped exception's render;
  the generic clause defers to message/1 (terminal-independent clauses still
  memoize their ANSI-free strings).
- CompilerException: store structured parse diagnostics + source and render
  colored lazily; the :errors field now holds bare, ANSI-free messages.
  parse_chunk/1 and eval! feed structured errors via Parser.parse_structured/1.

Lua semantics are unaffected: pcall/xpcall return the :value/:lua_value/
raw_message view via ProtectedCall.error_value/1, not the host-facing message.

Tests that read the frozen :message field now call Exception.message/1. Adds
exception_ansi_gating_test.exs covering each exception, the wrapper, and the
parse path in both ANSI directions.

Closes #384
Add CHANGELOG entries for the lazy exception rendering (#384): a Fixed
note for the ANSI-leak fix and a pre-1.0-freeze breaking note covering
the removed `:message` struct field and the now-bare `:errors` field.
Correct the existing parse_chunk/1 entry, which described `:errors` as
holding formatted diagnostics.

Guard clean_message/1 against a nil Error message so a future parser
error variant without a message string can't crash rendering.
@davydog187
davydog187 merged commit cfef224 into main Jul 14, 2026
5 checks passed
@davydog187
davydog187 deleted the fix/exception-defer-ansi-render branch July 14, 2026 19:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exception.message/1 on exception returns ANSI codes

1 participant