[🐞] Optimizer: captured variable in `$()` parameter default throws ReferenceError
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
- Tech stack
- javascript, rust, typescript
- Domain
- frontend, performance
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
Starts the project's dev container in your browser, under your own GitHub account.
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from QwikDev/qwik
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
[🐞] SSG: "Qwik Router SSG was skipped" warning on every SSG build, while pages are prerenderedPossibly taken @itsnjstyle27 claimed this 2 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
docs router V2
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
DX enhancement V2
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
QwikDev/qwik#8480 · 2 comments ·
Maintainers usually reply within 1 day
-
[🐞] A layout's loader request runs the loaders of the page at the layout's path as guards: a redirect there bounces client navigations in productionPossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 4/5 3-5 days Newbie friendliness 46/100
Maintainers usually reply within 1 day
Similar issues
-
area:frontend kind:bug priority:p2 ready
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
handsontable/hyperformula#1879 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
RunestoneInteractive/rs#1661 ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
capricorn86/happy-dom#2594 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day