Fix orderbook package bug for unused wasm import - #2305
Conversation
WalkthroughReplaces the dynamic Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant ESM as ESM wrapper
participant WasmBlob as Embedded WASM blob (base64 JSON)
participant WASM as WebAssembly module
rect rgb(230, 247, 255)
ESM->>WasmBlob: read inlined base64 data
Note right of WasmBlob: New path — no runtime URL fetch
WasmBlob-->>ESM: provide binary bytes
ESM->>WASM: initialize with bytes (WebAssembly.instantiate)
WASM-->>ESM: exports ready
end
(Comparison: previous flow used ESM -> fetch WASM URL -> instantiate WASM.) Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Suggested reviewers
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (1)
packages/orderbook/scripts/buildPackage.js(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
packages/**
📄 CodeRabbit inference engine (AGENTS.md)
JavaScript/Svelte projects live under packages/* (webapp, ui-components, orderbook)
Files:
packages/orderbook/scripts/buildPackage.js
🧠 Learnings (5)
📓 Common learnings
Learnt from: findolor
Repo: rainlanguage/rain.orderbook PR: 1913
File: Cargo.toml:55-55
Timestamp: 2025-06-06T16:38:04.182Z
Learning: In rain.orderbook repository, during PR chains involving dependency updates, wasm-bindgen-utils may temporarily point to git commits that don't contain the full required functionality, with the understanding that subsequent PRs in the chain will update it to the correct commit.
Learnt from: rouzwelt
Repo: rainlanguage/rain.orderbook PR: 2246
File: packages/webapp/src/hooks.client.ts:2-2
Timestamp: 2025-10-19T20:24:08.629Z
Learning: In the rain.orderbook repository, the import `rainlanguage/orderbook/esm` in packages/webapp/src/hooks.client.ts is intentional and should not be changed to the root package path, as it's specific to the webapp build configuration.
Learnt from: rouzwelt
Repo: rainlanguage/rain.orderbook PR: 2246
File: packages/orderbook/scripts/buildEsm.js:38-53
Timestamp: 2025-10-25T21:07:19.704Z
Learning: In the rain.orderbook repository, the deduplication logic in buildEsm.js (and similar build scripts) intentionally lacks defensive checks for indexOf returning -1. If duplicates are not found, the script should throw to surface breaking changes in wasm-bindgen output that require attention.
Learnt from: findolor
Repo: rainlanguage/rain.orderbook PR: 1744
File: crates/subgraph/src/orderbook_client.rs:44-46
Timestamp: 2025-05-14T05:13:59.713Z
Learning: In the rain.orderbook project, WASM dependencies are intentionally made available in non-WASM targets to facilitate testing of WASM-related functionality, so conditional compilation guards like `#[cfg(target_family = "wasm")]` should not be added to imports or implementations that may be needed for tests.
Learnt from: findolor
Repo: rainlanguage/rain.orderbook PR: 1710
File: crates/quote/Cargo.toml:30-30
Timestamp: 2025-05-14T04:49:14.621Z
Learning: In the rain.orderbook repository, wasm-bindgen-utils is intentionally used as an unconditional dependency because non-wasm targets (like tests) sometimes need to use it.
Learnt from: brusherru
Repo: rainlanguage/rain.orderbook PR: 2044
File: crates/common/src/raindex_client/vaults_list.rs:363-423
Timestamp: 2025-07-31T19:34:11.716Z
Learning: In the rainlanguage/rain.orderbook project, for WASM-exposed functionality like VaultsList, the team prefers to keep comprehensive tests in the non-WASM environment due to the complexity of recreating objects like RaindexVaults in WASM. WASM tests focus on basic functionality and error cases since the WASM code reuses the already-tested non-WASM implementation.
Learnt from: findolor
Repo: rainlanguage/rain.orderbook PR: 1715
File: crates/js_api/src/common/mod.rs:55-59
Timestamp: 2025-05-14T05:51:50.277Z
Learning: In the Rain Orderbook project, the error handling for WASM exported functions uses WasmEncodedError which includes both the original error message and a human-readable version, making direct modification of the JsValue conversion unnecessary.
Learnt from: findolor
Repo: rainlanguage/rain.orderbook PR: 1938
File: crates/js_api/src/raindex/orders.rs:1-11
Timestamp: 2025-06-18T12:54:47.807Z
Learning: In the rain.orderbook repository, findolor prefers to keep unused imports in place when they will be used in upcoming follow-up PRs, rather than removing and re-adding them during incremental development.
Learnt from: brusherru
Repo: rainlanguage/rain.orderbook PR: 2036
File: crates/js_api/src/filters/raindex_filter_store.rs:446-533
Timestamp: 2025-08-02T02:34:32.237Z
Learning: In the rainlanguage/rain.orderbook project's RaindexFilterStore WASM tests (crates/js_api/src/filters/raindex_filter_store.rs), brusherru decided to focus on testing only methods without side effects (that don't use web_sys) due to difficulties with mocking localStorage and window APIs in the WASM test environment. This pragmatic approach tests pure logic separately from browser integration.
Learnt from: rouzwelt
Repo: rainlanguage/rain.orderbook PR: 0
File: :0-0
Timestamp: 2025-05-21T21:26:31.566Z
Learning: For PR #1884 and future PRs in rainlanguage/rain.orderbook repository, provide PR size reports in a separate comment with ONLY the exact format "TOTAL=number\nADDITIONS=number\nDELETIONS=number", without any additional text or explanation. Reports should exclude irrelevant files such as lock files (package-lock.json, yarn.lock, pnpm-lock.yaml, cargo.lock).
Learnt from: CR
Repo: rainlanguage/rain.orderbook PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-10-06T08:05:50.452Z
Learning: WASM bundle via nix develop -c rainix-wasm-artifacts
📚 Learning: 2025-10-25T21:07:19.704Z
Learnt from: rouzwelt
Repo: rainlanguage/rain.orderbook PR: 2246
File: packages/orderbook/scripts/buildEsm.js:38-53
Timestamp: 2025-10-25T21:07:19.704Z
Learning: In the rain.orderbook repository, the deduplication logic in buildEsm.js (and similar build scripts) intentionally lacks defensive checks for indexOf returning -1. If duplicates are not found, the script should throw to surface breaking changes in wasm-bindgen output that require attention.
Applied to files:
packages/orderbook/scripts/buildPackage.js
📚 Learning: 2025-10-19T20:24:08.629Z
Learnt from: rouzwelt
Repo: rainlanguage/rain.orderbook PR: 2246
File: packages/webapp/src/hooks.client.ts:2-2
Timestamp: 2025-10-19T20:24:08.629Z
Learning: In the rain.orderbook repository, the import `rainlanguage/orderbook/esm` in packages/webapp/src/hooks.client.ts is intentional and should not be changed to the root package path, as it's specific to the webapp build configuration.
Applied to files:
packages/orderbook/scripts/buildPackage.js
📚 Learning: 2025-10-25T21:06:38.487Z
Learnt from: rouzwelt
Repo: rainlanguage/rain.orderbook PR: 2246
File: packages/orderbook/scripts/buildCjs.js:38-53
Timestamp: 2025-10-25T21:06:38.487Z
Learning: In the rain.orderbook repository, build scripts in packages/orderbook/scripts/ (such as buildCjs.js and buildEsm.js) should keep duplicated logic separate rather than extracting to shared utilities, even when identical, to allow future divergence and to surface breaking changes as early failures.
Applied to files:
packages/orderbook/scripts/buildPackage.js
📚 Learning: 2025-10-06T08:05:50.452Z
Learnt from: CR
Repo: rainlanguage/rain.orderbook PR: 0
File: AGENTS.md:0-0
Timestamp: 2025-10-06T08:05:50.452Z
Learning: WASM bundle via nix develop -c rainix-wasm-artifacts
Applied to files:
packages/orderbook/scripts/buildPackage.js
|
|
||
| // remove unused wasm import as we import the wasm through json | ||
| esm = esm.replaceAll(`module_or_path = new URL('${pkg}_bg.wasm', import.meta.url);`, ""); | ||
|
|
There was a problem hiding this comment.
🛠️ Refactor suggestion | 🟠 Major
🧩 Analysis chain
Fix correctly removes unused wasm URL import.
The change properly addresses the compilation error by removing the unused URL-based wasm import, since the wasm is already loaded via the JSON base64 approach (lines 82-90, 123-126).
However, consider adding a defensive check to ensure the replacement actually occurred, consistent with the team's preference for explicit failures when wasm-bindgen output changes unexpectedly:
// remove unused wasm import as we import the wasm through json
-esm = esm.replaceAll(`module_or_path = new URL('${pkg}_bg.wasm', import.meta.url);`, "");
+const beforeLength = esm.length;
+esm = esm.replaceAll(`module_or_path = new URL('${pkg}_bg.wasm', import.meta.url);`, "");
+if (esm.length === beforeLength) {
+ throw new Error(`Expected to find and remove wasm URL import pattern for ${pkg}, but pattern was not found. wasm-bindgen output may have changed.`);
+}This ensures the script fails loudly if wasm-bindgen changes its output format, rather than silently leaving the problematic import in place.
Verify that the pattern matches the current wasm-bindgen output:
🏁 Script executed:
#!/bin/bash
# Description: Verify the wasm URL import pattern exists in wasm-bindgen generated output
# First, check if there are any existing generated files in temp/web/
if [ -d "./packages/orderbook/temp/web" ]; then
echo "Checking existing generated files..."
fd -e js -x grep -l "module_or_path = new URL" {} \; ./packages/orderbook/temp/web/
echo "---"
fd -e js -x grep "module_or_path = new URL" {} \; ./packages/orderbook/temp/web/
fi
# Check the buildPackage.js to see what the exact pattern is
echo "Pattern in buildPackage.js:"
rg -A 2 "remove unused wasm import" ./packages/orderbook/scripts/buildPackage.jsLength of output: 190
🏁 Script executed:
#!/bin/bash
# Check if there are similar URL import patterns that need removal
echo "=== Searching for other URL import patterns in buildPackage.js ==="
rg -n "new URL" ./packages/orderbook/scripts/buildPackage.js
echo ""
echo "=== Checking for test files related to buildPackage ==="
fd -e test.js -e test.ts -e spec.js "buildPackage" ./packages/orderbook/
echo ""
echo "=== Looking for generated wasm output validation or tests ==="
rg -n "module_or_path\|bg\.wasm\|wasm-bindgen" ./packages/orderbook/test/ -C 2
echo ""
echo "=== Comparing with buildEsm.js defensive checks ==="
rg -n "indexOf" ./packages/orderbook/scripts/buildEsm.js -B 2 -A 2Length of output: 581
🏁 Script executed:
#!/bin/bash
# Search for URL import patterns and defensive checks in build scripts
echo "=== Searching for URL import patterns in buildPackage.js ==="
rg -n "new URL" packages/orderbook/scripts/buildPackage.js
echo ""
echo "=== Searching for the wasm pattern in buildPackage.js ==="
rg -n "module_or_path.*new URL" packages/orderbook/scripts/buildPackage.js -C 3
echo ""
echo "=== Comparing defensive checks in buildEsm.js ==="
rg -n "indexOf.*-1\|throw.*Error" packages/orderbook/scripts/buildEsm.js -B 2 -A 1
echo ""
echo "=== Check for tests that validate wasm output ==="
fd . packages/orderbook/test/ -name "*.test.js" -o -name "*.test.ts" -o -name "*.spec.js" | head -5
echo ""
echo "=== Look for replaceAll usage patterns in build scripts ==="
rg -n "replaceAll" packages/orderbook/scripts/ -B 1 -A 1Length of output: 1834
🏁 Script executed:
#!/bin/bash
# Find buildEsm.js and check its structure
echo "=== Locating buildEsm.js ==="
find packages -name "buildEsm.js" -o -name "*buildEsm*" 2>/dev/null
echo ""
echo "=== Show full context of deduplication with defensive checks ==="
rg -n "if.*indexOf.*-1" packages/orderbook/scripts/buildPackage.js -B 3 -A 2
echo ""
echo "=== Check if wasm pattern has any condition/check around it ==="
rg -n "module_or_path.*new URL\|replaceAll.*wasm" packages/orderbook/scripts/buildPackage.js -B 5 -A 2Length of output: 159
🏁 Script executed:
#!/bin/bash
# Simpler approach - just look at the full buildPackage.js around the relevant lines
echo "=== View lines 40-60 (first deduplication block) ==="
sed -n '40,60p' packages/orderbook/scripts/buildPackage.js
echo ""
echo "=== View lines 100-120 (wasm removal) ==="
sed -n '100,120p' packages/orderbook/scripts/buildPackage.js
echo ""
echo "=== Count replaceAll usage without defensive checks ==="
rg "replaceAll" packages/orderbook/scripts/buildPackage.js -nLength of output: 1755
Add defensive check to ensure wasm URL pattern replacement succeeds.
The fix correctly removes the unused wasm URL import (line 119), but lacks a defensive check present in other similar operations within the same file. Lines 49 and 106 both use conditional checks before replaceAll, whereas the wasm pattern removal operates without verification.
Since the wasm URL import should always be present in wasm-bindgen output, a missing pattern indicates a breaking change that requires explicit attention:
// remove unused wasm import as we import the wasm through json
-esm = esm.replaceAll(`module_or_path = new URL('${pkg}_bg.wasm', import.meta.url);`, "");
+const beforeLength = esm.length;
+esm = esm.replaceAll(`module_or_path = new URL('${pkg}_bg.wasm', import.meta.url);`, "");
+if (esm.length === beforeLength) {
+ throw new Error(`Expected wasm URL import pattern for ${pkg} not found. wasm-bindgen output may have changed.`);
+}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // remove unused wasm import as we import the wasm through json | |
| esm = esm.replaceAll(`module_or_path = new URL('${pkg}_bg.wasm', import.meta.url);`, ""); | |
| // remove unused wasm import as we import the wasm through json | |
| const beforeLength = esm.length; | |
| esm = esm.replaceAll(`module_or_path = new URL('${pkg}_bg.wasm', import.meta.url);`, ""); | |
| if (esm.length === beforeLength) { | |
| throw new Error(`Expected wasm URL import pattern for ${pkg} not found. wasm-bindgen output may have changed.`); | |
| } |
🤖 Prompt for AI Agents
In packages/orderbook/scripts/buildPackage.js around lines 117 to 120, the
unconditional call to replaceAll removing the wasm URL import should be guarded
like the other replacements; first check that the target string or pattern (the
module_or_path = new URL('${pkg}_bg.wasm', import.meta.url); fragment) is
present (or test with a regex) and only then call replaceAll, and if it is
missing, log or throw a clear error so missing wasm-bindgen output is detected
instead of silently proceeding.
|
@coderabbitai assess this PR size classification for the totality of the PR with the following criterias and report it in your comment: S/M/L PR Classification Guidelines:This guide helps classify merged pull requests by effort and complexity rather than just line count. The goal is to assess the difficulty and scope of changes after they have been completed. Small (S)Characteristics:
Review Effort: Would have taken 5-10 minutes Examples:
Medium (M)Characteristics:
Review Effort: Would have taken 15-30 minutes Examples:
Large (L)Characteristics:
Review Effort: Would have taken 45+ minutes Examples:
Additional Factors to ConsiderWhen deciding between sizes, also consider:
Notes:
|
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: Length of output: 76 🏁 Script executed: Length of output: 684 SIZE=S 🧠 Learnings used |
Motivation
Warning
Reported bug:
Reproduction steps:
Solution
This is caused by an unused wasm URL import which some bundlers like webpack resolve it internally causing the error to appear, so we need to remove that as its totally unused as we import the wasm through json import.
Checks
By submitting this for review, I'm confirming I've done the following:
Summary by CodeRabbit