This has come up before here: Execution error in context can have an effect on layout · Issue #5015 · typst/typst · GitHub
In general, Typst will catch any error in a show rule (or context, which is resolved by a show rule) and only print it if it remains by the last iteration. This frees the users from caring about things like query(heading).first()
throwing in the first iteration (because nothing is known yet). But, on the downside, it can cause such strangeness. It’s a tradeoff.