Skip to content

Comments

fix(wasm): respect CARGO_TARGET_DIR in WASM build scripts#3155

Open
lklimek wants to merge 3 commits intov3.1-devfrom
fix/wasm-build-respect-cargo-target-dir
Open

fix(wasm): respect CARGO_TARGET_DIR in WASM build scripts#3155
lklimek wants to merge 3 commits intov3.1-devfrom
fix/wasm-build-respect-cargo-target-dir

Conversation

@lklimek
Copy link
Contributor

@lklimek lklimek commented Feb 24, 2026

Issue being fixed or feature implemented

WASM build scripts (build-wasm.sh and related utility scripts) hardcode ../../target as the Cargo output directory. When CARGO_TARGET_DIR is set to a custom path (e.g. /home/user/.cache/rust-target), wasm-bindgen fails because it cannot find the compiled .wasm artifacts at the expected location.

This primarily affects wasm-drive-verify (which uses cargo build directly) and wasm-dpp (which has its own legacy build script), but all WASM-related scripts were affected.

What was done?

Replaced all hardcoded ../../target references in WASM build scripts with a CARGO_OUT_DIR variable that respects CARGO_TARGET_DIR, falling back to the repo's target/ directory when the env var is not set.

Files changed:

  • packages/scripts/build-wasm.sh — unified build script (3 path references)
  • packages/wasm-dpp/scripts/build-wasm.sh — wasm-dpp legacy build script
  • packages/wasm-drive-verify/scripts/build-wasm.sh — wasm-drive-verify legacy build script
  • packages/wasm-drive-verify/scripts/build-separate-modules.sh — module build utility
  • packages/wasm-drive-verify/scripts/quick-size-check.sh — size check utility
  • packages/wasm-drive-verify/scripts/analyze-module-combinations.sh — analysis utility

How Has This Been Tested?

  • Ran yarn setup end-to-end with CARGO_TARGET_DIR=/home/ubuntu/.cache/rust-target set — all WASM packages (wasm-drive-verify, wasm-dpp, wasm-dpp2, wasm-sdk) build successfully.
  • Verified the fix is backward-compatible: when CARGO_TARGET_DIR is not set, the scripts fall back to the default <repo>/target/ directory.

Breaking Changes

None

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation if needed

🤖 Co-authored by Claudius the Magnificent AI Agent

Summary by CodeRabbit

  • Chores
    • Improved build tooling flexibility by updating WASM-related scripts to use a configurable build output directory instead of fixed paths, ensuring consistent artifact discovery across environments and supporting custom output locations. Changes apply across multiple build, analysis, and size-check scripts.

WASM build scripts hardcoded `../../target` as the Cargo output
directory. When CARGO_TARGET_DIR is set to a custom location, builds
fail because wasm-bindgen cannot find the compiled .wasm artifacts.

Use CARGO_TARGET_DIR with fallback to the repo's target/ directory.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions github-actions bot added this to the v3.1.0 milestone Feb 24, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 24, 2026

📝 Walkthrough

Walkthrough

The pull request adds a configurable CARGO_OUT_DIR (defaulting to REPO_ROOT/target) and REPO_ROOT to multiple WASM build and utility scripts, replacing hardcoded relative paths to built WASM artifacts with dynamic references that honor an optional CARGO_TARGET_DIR.

Changes

Cohort / File(s) Summary
Top-level WASM build script
packages/scripts/build-wasm.sh
Introduce CARGO_OUT_DIR and use it for wasm-snip and wasm-bindgen input/output paths instead of hardcoded target/... paths.
WASM DPP build script
packages/wasm-dpp/scripts/build-wasm.sh
Add REPO_ROOT and CARGO_OUT_DIR; update wasm-bindgen to read the .wasm from the configurable cargo output directory.
WASM Drive-Verify build & tooling scripts
packages/wasm-drive-verify/scripts/build-wasm.sh, packages/wasm-drive-verify/scripts/build-separate-modules.sh, packages/wasm-drive-verify/scripts/analyze-module-combinations.sh, packages/wasm-drive-verify/scripts/quick-size-check.sh
Add REPO_ROOT and CARGO_OUT_DIR to each script and replace hardcoded ../../target/wasm32-unknown-unknown/release/... (or similar) with "$CARGO_OUT_DIR/wasm32-unknown-unknown/release/..." in wasm-bindgen and related invocations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 I nibble paths, then hop away,

CARGO_OUT_DIR lights the way.
No more hardcoded trails to roam,
Builds find home and callers home. 🥕✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: introducing support for CARGO_TARGET_DIR in WASM build scripts.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/wasm-build-respect-cargo-target-dir

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Consistent with the legacy build scripts.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/wasm-dpp/scripts/build-wasm.sh`:
- Around line 19-20: REPO_ROOT is only going up two levels so it resolves to
packages/ instead of the repo root, causing CARGO_OUT_DIR fallback to point at
$REPO_ROOT/target inside packages; update the REPO_ROOT computation used by
CARGO_OUT_DIR by adding one more parent traversal (change the path in the
REPO_ROOT assignment from "$(dirname "${BASH_SOURCE[0]}")/../.." to use three
".." segments so REPO_ROOT points to the repository root and CARGO_OUT_DIR
defaults to $REPO_ROOT/target as intended), keeping the rest of the script
(including the later wasm-bindgen usage) unchanged.

In `@packages/wasm-drive-verify/scripts/build-wasm.sh`:
- Around line 19-20: REPO_ROOT is computed with "/../.." which is off-by-one for
scripts located in packages/*/scripts; update the path in the REPO_ROOT
assignment to use "/../../.." so it resolves to the repository root (change the
assignment that sets REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &&
pwd)" to use "/../../.." instead), leaving the CARGO_OUT_DIR fallback logic
(CARGO_OUT_DIR="${CARGO_TARGET_DIR:-$REPO_ROOT/target}") unchanged.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0fa82e6 and 98a4aee.

📒 Files selected for processing (6)
  • packages/scripts/build-wasm.sh
  • packages/wasm-dpp/scripts/build-wasm.sh
  • packages/wasm-drive-verify/scripts/analyze-module-combinations.sh
  • packages/wasm-drive-verify/scripts/build-separate-modules.sh
  • packages/wasm-drive-verify/scripts/build-wasm.sh
  • packages/wasm-drive-verify/scripts/quick-size-check.sh

@lklimek lklimek requested a review from shumkov February 24, 2026 11:35
Scripts under packages/*/scripts/ are 3 levels deep, not 2.
The previous /../.. resolved to packages/ instead of the repo root,
which would break the CARGO_OUT_DIR fallback when CARGO_TARGET_DIR
is unset.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/wasm-drive-verify/scripts/build-wasm.sh`:
- Around line 19-22: The BUILD_COMMAND and BINDGEN_COMMAND strings use unquoted
CARGO_OUT_DIR (and other vars) which will break if CARGO_TARGET_DIR contains
spaces; change these to build command arrays or ensure each variable is properly
quoted/expanded when constructing/executing the command (update BUILD_COMMAND
and BINDGEN_COMMAND usage to use arrays or wrap
"${CARGO_OUT_DIR}/${TARGET}/${PROFILE}/wasm_drive_verify.wasm" and other
variables in quotes) so word-splitting is avoided when running the commands.

ℹ️ Review info

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 98a4aee and 0ba33ae.

📒 Files selected for processing (5)
  • packages/wasm-dpp/scripts/build-wasm.sh
  • packages/wasm-drive-verify/scripts/analyze-module-combinations.sh
  • packages/wasm-drive-verify/scripts/build-separate-modules.sh
  • packages/wasm-drive-verify/scripts/build-wasm.sh
  • packages/wasm-drive-verify/scripts/quick-size-check.sh
🚧 Files skipped from review as they are similar to previous changes (3)
  • packages/wasm-drive-verify/scripts/quick-size-check.sh
  • packages/wasm-dpp/scripts/build-wasm.sh
  • packages/wasm-drive-verify/scripts/analyze-module-combinations.sh

Comment on lines +19 to +22
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." && pwd)"
CARGO_OUT_DIR="${CARGO_TARGET_DIR:-$REPO_ROOT/target}"
BUILD_COMMAND="cargo build --config net.git-fetch-with-cli=true --target=${TARGET} ${PROFILE_ARG}"
BINDGEN_COMMAND="wasm-bindgen --out-dir=${OUTPUT_DIR} --target=web --omit-default-module-path ../../target/${TARGET}/${PROFILE}/wasm_drive_verify.wasm"
BINDGEN_COMMAND="wasm-bindgen --out-dir=${OUTPUT_DIR} --target=web --omit-default-module-path ${CARGO_OUT_DIR}/${TARGET}/${PROFILE}/wasm_drive_verify.wasm"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Quote CARGO_OUT_DIR to avoid word-splitting. If CARGO_TARGET_DIR contains spaces, the current string-based command will break. Consider using an array.

🛠️ Suggested fix
-BINDGEN_COMMAND="wasm-bindgen --out-dir=${OUTPUT_DIR} --target=web --omit-default-module-path ${CARGO_OUT_DIR}/${TARGET}/${PROFILE}/wasm_drive_verify.wasm"
+BINDGEN_COMMAND=(wasm-bindgen --out-dir="${OUTPUT_DIR}" --target=web --omit-default-module-path "${CARGO_OUT_DIR}/${TARGET}/${PROFILE}/wasm_drive_verify.wasm")
@@
-  AR=${AR_PATH} CC=${CLANG_PATH} ${BINDGEN_COMMAND}
+  AR=${AR_PATH} CC=${CLANG_PATH} "${BINDGEN_COMMAND[@]}"
@@
-  ${BINDGEN_COMMAND}
+  "${BINDGEN_COMMAND[@]}"
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/wasm-drive-verify/scripts/build-wasm.sh` around lines 19 - 22, The
BUILD_COMMAND and BINDGEN_COMMAND strings use unquoted CARGO_OUT_DIR (and other
vars) which will break if CARGO_TARGET_DIR contains spaces; change these to
build command arrays or ensure each variable is properly quoted/expanded when
constructing/executing the command (update BUILD_COMMAND and BINDGEN_COMMAND
usage to use arrays or wrap
"${CARGO_OUT_DIR}/${TARGET}/${PROFILE}/wasm_drive_verify.wasm" and other
variables in quotes) so word-splitting is avoided when running the commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant