#2005·elysia

isAsync source-matching fallback misses a handler whose arrow body is on the next line, so the handler is not awaited

Author: vlad-sensiCreated Sep 21, 2026Updated Sep 21, 2026
Labelsbug

What version of Elysia is running?

1.4.30

What platform is your computer?

Darwin 24.6.0 arm64 arm

What environment are you using

Node v24.7.0, @elysiajs/node 1.4.5

Are you using dynamic mode?

No - default AOT

What steps can reproduce the bug?

Two routes differing only by a line break:

typescript
import { Elysia, t } from 'elysia'

const payload = () => Promise.resolve({ ok: true })
const schema = { response: t.Object({ ok: t.Boolean() }) }

new Elysia()
  .get('/inline', () => payload(), schema)
  .get('/wrapped', () =>
    payload(), schema)
  .listen(3000)

Then curl localhost:3000/inline and curl localhost:3000/wrapped.

Neither handler contains the substring async or await, so the regex's a(?:sync|wait) alternative cannot rescue the match.

What is the expected behavior?

Both routes return 200 {"ok":true}. Formatting should not change whether a handler is awaited.

What do you see instead?

/inline returns 200 {"ok":true}. /wrapped returns 422, a response-validation error that reports the schema's properties as absent, because the body under validation is the Promise:

json
{
  "type": "validation",
  "on": "response",
  "property": "/ok",
  "message": "Expected boolean",
  "expected": { "ok": false },
  "found": {}
}

A parenthesised body fails identically:

typescript
.get('/paren', () => (
  payload()
), schema)

Additional information

Dispatch is at compose.ts:791 — isAsyncHandler = typeof handler === 'function' && isAsync(handler). The v.isAsync short-circuit inside isAsync is only reachable for a HookContainer; a route handler arrives as a bare function, so there is no way for a user to override the guess for a handler.

The two possible mistakes are not symmetric:

Heuristic wrong Cost
says async, actually sync one extra microtask tick
says sync, actually async unresolved Promise reaches response validation

So the fallback currently fails in the more expensive direction. Defaulting to await when the source cannot be positively classified as sync would keep the fast path for the cases the matcher recognises while making the failure mode a tick instead of a 422.

Loosening \s? to \s* is not sufficient on its own — \S+\( still cannot cross a newline, so the parenthesised form defeats it again:

javascript
/(?:return|=>)\s*\S+\(|a(?:sync|wait)/.test('() => (\n  doThing()\n)') // false

matchResponseClone = /=>\s?response\.clone\(/ (compose.ts:370) has the same \s?, so it presumably has the same formatting sensitivity.

Without a response schema the mapper resolves the Promise and the client still gets 200 {"ok":true}, so the misclassification stays invisible until a response schema is added — the wrong await decision can sit latent in a codebase until someone attaches one.

This is your perf tradeoff to weigh, not mine — the source-matching fallback exists precisely to avoid a microtask per request. Just flagging that the current failure direction is hard to attribute from the outside: the symptom is a 422 that appears or disappears when a formatter reflows a line.

Workaround for anyone hitting this: declare every handler async. The AsyncFunction check then matches and the source is never read.

typescript
.get('/wrapped', async () =>
  payload(), schema)

Have you try removing the node_modules and bun.lock and try again yet?

No. The defect is visible in the published main source, and reproduces in a clean scratch install of 1.4.30, so install state is not a factor.