Stack overflow (abort) on ~100 levels of nested `function` blocks, instead of formatting or reporting an error
Summary
StyLua 2.6.0 aborts with thread '<unknown>' has overflowed its stack on input with about 100 nested function ... end blocks. Valid code triggers it, and so does invalid code where the ends are missing. With a larger thread stack (RUST_MIN_STACK=268435456), the same inputs run to completion: the invalid ones report a normal parse error and exit with code 2. So this looks like recursion depth in parsing or formatting that exceeds the default worker-thread stack. It doesn't look like an infinite loop.
Reproduction
Valid Luau, 100 nested functions:
{ for i in $(seq 100); do echo "function F$i()"; done; for i in $(seq 100); do echo end; done; } > nested.lua
stylua --check nested.lua
thread '<unknown>' (…) has overflowed its stack
fatal runtime error: stack overflow, aborting
Aborted (core dumped) # exit 134
Unterminated functions (the case I actually hit):
for i in $(seq 100); do echo "function F$i(self): number"; done > open.lua
stylua --check open.lua # exit 134, same stack overflow
The same thing with 20 or 50 lines gives a normal parse error (exit 2).
With a bigger stack, the second example gives a normal parse error instead of crashing:
RUST_MIN_STACK=268435456 stylua --check open.lua # exit 2, "error parsing: ..."
How I hit it
I hit this through a Luau definitions file (globalTypes.None.d.luau from luau-lsp 1.70.1, ~680 KB) that ended up in a directory passed to stylua --check. Definition files use declare extern type X with ... end, where member lines like function Foo(self, x: number): string have no end. full-moon treats each one as an unclosed function body, so the nesting depth grows with the number of methods. Two consecutive copies of one ~100-line declare extern type EditableMesh ... block are enough to crash. Formatting a definitions file isn't expected to work, and I've excluded it with .styluaignore. But a parse error would be better than an abort, especially in CI or editor integrations.
Expected
- Valid input: format it, or fail with an error, without aborting the process.
- Invalid input: the usual
error parsing:diagnostics and exit code 2.
Possible approaches: run the parser and formatter on a thread with a larger stack, or use something like stacker to grow the stack at recursion points. Failing that, a depth limit that returns an error.
Environment
- StyLua 2.6.0 (Arch Linux package
stylua 2.6.0-1), x86_64 - Default config,
--syntaxdefault (--syntax luaubehaves the same)
Possibly related: #824 (stack overflow in test_large_example, reported by Arch packagers).