Skip to content

Enable WASM support#271

Open
mibes404 wants to merge 4 commits into
gibbz00:mainfrom
mibes404:main
Open

Enable WASM support#271
mibes404 wants to merge 4 commits into
gibbz00:mainfrom
mibes404:main

Conversation

@mibes404

@mibes404 mibes404 commented Dec 1, 2025

Copy link
Copy Markdown

Summary:
Adds getrandom dependency with JavaScript feature flag for WASM (WebAssembly) target architecture support.

Impact:
Enables the library to compile and run in WebAssembly (browser) environments

@codecov

codecov Bot commented Dec 1, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.12%. Comparing base (18772a5) to head (eced6b4).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #271      +/-   ##
==========================================
- Coverage   96.20%   96.12%   -0.08%     
==========================================
  Files          80       80              
  Lines        3136     3073      -63     
==========================================
- Hits         3017     2954      -63     
  Misses        119      119              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread crates/lib/Cargo.toml Outdated
Comment on lines +79 to +80
getrandom = { version = "0.2", default-features = false, features = ["js"] }

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Can you add something like this job to the ci.yaml?

  wasm_build:
    runs-on: ubuntu-latest
    steps:
    - uses: Swatinem/rust-cache@v2
      with:
        key: wasm32-unknown-unknown
    - uses: dtolnay/rust-toolchain@stable
      with:
        targets: wasm32-unknown-unknown
        toolchain: nightly
    - run: cargo check --all-features -- -D warnings

I'm a bit skeptical it works since "js" does not seem like the correct feature name. https://docs.rs/crate/getrandom/0.3.4/features

Also, what happens if you use getrandom 0.3?

@mibes404

mibes404 commented Dec 2, 2025

Copy link
Copy Markdown
Author

In getrandom 0.2, the feature was called js, but it was renamed to wasm_js in 0.3. I've included the features for both v0.2 and v0.3 on wasm32 targets to ensure the rops library compiles properly. Both are needed to prevent compilation errors.

[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2", default-features = false, features = ["js"] }
getrandom_03 = { package = "getrandom", version = "0.3", default-features = false, features = ["wasm_js"] }

I ended up with the CI task to compile and check the wasm build:

  wasm_build:
    name: WASM build
    runs-on: ubuntu-latest
    env:
      RUSTFLAGS: '--cfg getrandom_backend="wasm_js" -D warnings'
    steps:
    - name: Checkout sources
      uses: actions/checkout@v5
    - name: Cache Rust toolchain
      uses: Swatinem/rust-cache@v2
      with:
        key: wasm32-unknown-unknown
    - name: Install Rust toolchain
      uses: dtolnay/rust-toolchain@stable
      with:
        targets: wasm32-unknown-unknown
        toolchain: nightly
    - run: cargo check -p rops --features wasm --no-default-features --target wasm32-unknown-unknown

The wasm feature enables all features supported by the wasm32 target.

@gibbz00

gibbz00 commented Dec 17, 2025

Copy link
Copy Markdown
Owner

Apologies for the somewhat late reply. Anyhow, I get what you're saying now. I'm not a huge fan of forcing two versions of the same library into the same project though. If only getrandom 0.3 could be used, that would be great.

The rust crypto crates have release candidates with their getrandom dependency updated. Perhaps we can wait a bit for their release? The remaining crate that uses getrandom 0.2 is ring. Are you open to creating a PR there that updates it?

The wasm feature enables all features supported by the wasm32 target.

Not sure I like this. We're basically exposing a combined feature just for the same of shortening a CI job.
Better to just inline that list in the job script, and document that the aws kms feature does not work with wasm32. (rust-lang/cargo#3126 is unfortunately not there yet for a terser --all-features -aws-kms)

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.

2 participants