Skip to content

fix(parser): gate parse-error ANSI on IO.ANSI.enabled?/0 - #385

Merged
davydog187 merged 1 commit into
mainfrom
fix/parse-chunk-ansi-gating
Jul 14, 2026
Merged

davydog187 merged 1 commit into
mainfrom
fix/parse-chunk-ansi-gating

Conversation

@davydog187

Copy link
Copy Markdown
Contributor

Problem

Closes #382. Lua.parse_chunk("asdf") returned error strings full of raw ANSI escape codes (\e[31m…) even when ANSI is disabled. Lua.Parser.Error.format/2 emitted IO.ANSI escapes unconditionally, bypassing the IO.ANSI.enabled?/0 gating that the runtime VM path (Lua.VM.ErrorFormatter, #304) already respects. Those strings flow through parse_chunk/1 into %Lua.CompilerException{errors: [...]}, so the escapes leaked into the returned tuple regardless of environment.

(The contributor's follow-up ask on the issue — return a %Lua.CompilerException{} rather than a bare string list — was already delivered by #380.)

Fix

  • lib/lua/parser/error.ex — add a private color/2 helper mirroring Lua.VM.ErrorFormatter.color/2, and route every colorized fragment in format/2, format_context/2, and format_multiple/2 through it. No message text or public API changes — only the presence of escape codes, and only when ANSI is disabled.

Behavior

# ANSI off (default non-TTY) — clean
{:error, %Lua.CompilerException{errors: ["Parse Error\n\n  at line 1, column 1:\n\n  A bare variable reference isn't a Lua statement.\n..."]}}

# ANSI on (real TTY) — colored, as before
{:error, %Lua.CompilerException{errors: ["\e[31m\e[1mParse Error\e[0m\n..."]}}

Tests

  • test/lua/parser/error_test.exs — drops the now-invalid "ANSI always present" assertions; adds a gating regression (ANSI off → no \e[).
  • test/lua/parser/error_ansi_test.exs (new, async: false) — the ANSI-on assertions, isolated into a sync module so toggling the global ansi_enabled can't race concurrent async tests asserting plain text.
  • test/lua_test.exs — end-to-end guard that parse_chunk/1's %Lua.CompilerException{} errors carry no escapes when color is disabled.

Full suite: 2580 passed, 7 skipped, 30 excluded.

Note on #384

#384 reports the same class of bug at a deeper level: exceptions memoize their gated render at construction time, so a render frozen in a TTY keeps its escapes into non-TTY sinks. This PR adds the parser gate that a construction-time fix depends on, but the full deferral fix is tracked separately in #384.

Lua.Parser.Error.format/2, format_context/2, and format_multiple/2
emitted IO.ANSI escapes unconditionally, bypassing the IO.ANSI.enabled?/0
gating the runtime VM path already respects. Those strings flow through
parse_chunk/1 into %Lua.CompilerException{errors: [...]}, so escape codes
leaked into the returned tuple regardless of environment.

Add a color/2 helper mirroring Lua.VM.ErrorFormatter.color/2 and route
every colorized fragment through it. Colored on a real TTY, plain text
otherwise.

Closes #382
@davydog187
davydog187 merged commit 22349fc into main Jul 14, 2026
5 checks passed
@davydog187
davydog187 deleted the fix/parse-chunk-ansi-gating branch July 14, 2026 03:07
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.

parse_chunk/1 returns ANSI escape codes

1 participant