[🐞] Optimizer: captured variable in `$()` parameter default throws ReferenceError

Open
#9,388 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active

Research direction

Start by reproducing the failure with the supplied createOptimizer example and inspect the Rust and TypeScript optimizer paths that transform captured variables in parameter defaults. The issue names no source files or tests, so trace the transform from the emitted function body and add a regression test for a default parameter using an outer capture. Done means the QRL resolves the omitted argument from the capture without a ReferenceError in both optimizers.

Written by the indexing model from the issue text.

Description

Which component is affected?

Qwik Optimizer (rust)

Describe the bug

When a $() function uses a captured outer variable inside a parameter default (in @qwik.dev/optimizer 2.1.0-rc.10, both Rust and TS optimizers), the optimizer unpacks the capture inside the extracted function body instead of making it available in the parameter scope. Calling the QRL without that argument throws ReferenceError at runtime.

import { createOptimizer } from "@qwik.dev/optimizer";

const optimizer = await createOptimizer();
const { modules } = await optimizer.transformModules({
  input: [{
    path: "test.js",
    code: `import { $ } from "@qwik.dev/core";
export const make = (x) => $((a = x) => a);`,
  }],
  entryStrategy: { type: "inline" },
  mode: "prod",
});

console.log(modules[0].code);

This emits:

q_s_wVGAgzOzrJg.s((a = x)=>{
    let x = _capturesObj._[0];
    return a;
});
export const make = (x)=>q_s_wVGAgzOzrJg.w([x]);

Calling await make(3)() throws ReferenceError: x is not defined instead of resolving to 3, because parameter default initializers evaluate in the parameter scope before the function body's let x = _capturesObj._[0] declaration is reached.

Reproduction

https://gist.github.com/trackoor/0313dd231eecdf4ed1c356336f1a1b71

Steps to reproduce

Run npm install @qwik.dev/[email protected] @qwik.dev/[email protected] and node repro.mjs.

System Info
  System:
    OS: macOS 26.3
    CPU: (14) arm64 Apple M3 Max
    Memory: 1.59 GB / 36.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 24.14.1 - /Users/hanzhi/.nvm/versions/node/v24.14.1/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 11.11.0 - /Users/hanzhi/.nvm/versions/node/v24.14.1/bin/npm
    pnpm: 11.24.0 - /opt/homebrew/bin/pnpm
  Browsers:
    Chrome: 156.0.8078.80
    Safari: 26.3
  npmPackages:
    @qwik.dev/core: 2.1.0-rc.10 => 2.1.0-rc.10
    @qwik.dev/optimizer: 2.1.0-rc.10 => 2.1.0-rc.10
Additional Information

Credit: https://ucsbplse.github.io/

Dominant language
TypeScript
Stars
22.1k
Forks
1.4k
Avg merge
2d 17h
Merged PRs (30d)
99

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from QwikDev/qwik

All issues in QwikDev/qwik

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.