Repository navigation
fix(errors): render exception messages lazily so ANSI gating holds - #386
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/1and stored it. Construction happens during VM execution, whereIO.ANSI.enabled?/0is true, soErrorFormatter.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.Fix
Render at
Exception.message/1call time instead of construction, mirroring the patternLua.VM.ArgumentErroralready used.RuntimeError,TypeError,AssertionError— drop the memoized:messagefield; add a lazymessage/1that formats from the stored fields.Lua.RuntimeExceptionwrapper — stop memoizing the wrapped exception's render; the generic (VM-wrapping) clause defers tomessage/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 viaParser.Error.format/2. The:errorsfield now holds bare, ANSI-free messages (log-friendly).parse_chunk/1andeval!feed structured errors viaParser.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/2and the now-clean:errorsfield.Lua semantics unaffected
pcall/xpcall hand Lua code the
:value/:lua_value/raw_messageview viaProtectedCall.error_value/1— the richException.message/1is host-facing only. Protected-call return values are byte-for-byte unchanged (guarded bypcall_error_value_test.exs,pcall_test.exs).Tests
:messagestruct field now callException.message/1(error_messages_test.exs,runtime_exception_test.exs). Removing the frozen field forces the correct accessor.error_ansi_test.exs: the color assertion moves from the.errorsfield toException.message/1(the field is now ANSI-free by design).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
:messagestruct field is removed fromLua.VM.RuntimeError/TypeError/AssertionErrorand isnilon theRuntimeExceptionwrapper when it wraps a VM exception. Consumers must useException.message/1(the idiomatic accessor) rather than readinge.messagedirectly. This is the correction that makes the gate work; it lands before the 1.0 freeze.