ReviewOS

stacks/stx

:for silently no-ops when the list expression is a called signal; bare-ref works (parens vs no-parens inconsistency)

#1716
Closed glennmichael123 opened this 23 days ago · 1 comment
23 days ago

Summary

:for="item in store.signalRef" (bare reference) works correctly. :for="item in store.signalRef()" (call syntax) silently iterates zero items, even when the signal IS populated. No console error, no warning — the dropdown just stays empty.

This is inconsistent with every other binding site (:text, :if, x-class, x-style, …) where both bare and call syntax work interchangeably. :for is the only attribute where call syntax silently fails.

Reproducer

```html

<button :for="judge in judgesStore.searchResults" :text="judge.name"></button>

<button :for="judge in judgesStore.searchResults()" :text="judge.name"></button> ```

Tested with judgesStore.searchResults defined as `const searchResults = state<judge>[]>([])` exported from a `defineStore('judges', ...)`.

Root cause

`signals.ts:1980-2227` (`bindFor`). The list expression is evaluated via `evalLazy` (line 2092) which uses an auto-unwrap proxy as the `with()` scope:

```ts const fn = new Function('scope', 'with(scope) { return ' + expression + ' }') return fn(unwrapScope) ```

The proxy's get-trap (line 281-292) unwraps signals on access: if a property is a function with `_isSignal` / `_isDerived`, it calls it and returns the value. For stx stores (`_isStxStore`), it recursively wraps.

So:

  • `judgesStore.searchResults` (bare): proxy.get('judgesStore') → sub-proxy → proxy.get('searchResults') → detects signal, calls it, returns the array. ✓
  • `judgesStore.searchResults()` (call): proxy auto-unwraps as above, returns the array. Then `()` is applied to the array → `TypeError: array is not a function` → caught by evalLazy's try/catch → returns `''`. Then `bindFor` at line 2227: `if (!Array.isArray(list)) return` → silently bails. ✗

The TypeError is swallowed at `signals.ts:2101-2104` because the catch filter skips `ReferenceError` and `TypeError` for the warn path.

Impact

This is a real footgun. The exact same expression that works fine everywhere else (`{⁠{ store.signal() }}`, `:text="store.signal()"`, `x-class="store.signal().length > 0 ? '' : 'hidden'"`) silently breaks the iteration in `:for`. Caught while migrating a header search dropdown — the same data was reactive via `x-class` (so the section headers showed) but the `:for` button list rendered zero items. No warning. Section headers + empty list looked like the component was "loaded but with no data."

Even the framework's own components have leave-behind comments documenting this gotcha (`resources/components/Bench/Judge/ReviewJudgeSearch.stx`):

"Bind `:for` to the store signal directly (not wrapped in a local derived) so the list re-renders when results land."

…which is the same issue rediscovered manually.

Suggested fix

Pick one:

  1. Accept both forms (preferred). In bindFor's effect, after `evalLazy(listExpr)`, if the result is not an array but IS a function with signal markers, call it. Symmetric with how other bindings already tolerate both.

  2. Strip trailing `()` from the listExpr at parse time if the bare reference also resolves to a signal. Cheap and avoids any runtime cost.

  3. At minimum, log a clear warning. Currently the silent return at `signals.ts:2227` masks the error. `if (!Array.isArray(list)) { console.warn('[STX] :for expected an array, got', typeof list, '— did you call a signal with () inside :for? Try the bare reference instead.'); return; }` would have saved several iterations of debugging here.

Workaround in the meantime

Drop the parens: `:for="item in signal"` instead of `:for="item in signal()"`.

Environment

  • stx vendored via stacks pantry (symlinked to a local checkout)
  • Bun 1.3.13
  • Caught while implementing a global header search typeahead on a Stacks app — `<button :for="">"judge in judgesStore.searchResults()">` rendered zero rows despite the same store signal driving `x-class="...length > 0 ? '' : 'hidden'"` on the wrapper just fine.
22 days ago

Fixed by 70f5acf on main.

Two-part fix landed in bindFor:

  1. When the initial list evaluation isn't an array AND the expression ends in (), retry with the trailing parens stripped — bare-ref and call-syntax forms now produce the same reactive list.
  2. When the result is still not an array after the retry, surface a clear console.warn instead of silently returning, so future misuse (typo'd identifier, signal that threw, non-iterable value) is loud rather than a debugging cycle.

Commit didn't include a Closes # trigger so the issue stayed open — closing manually.

Sign in to comment on this issue.