Skip to content

typespec_client_core: New web_runtime to support wasm32-unknown-unknown #2838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

magodo
Copy link

@magodo magodo commented Jul 30, 2025

The typespec_client_core::sleep::sleep is using the async_runtime's sleep implementation, which is complicated to support wasm32, and will panic now when targeting to wasm32. Instead of make the standard_runtime to support wasm32 sleep (I wonder if it's feasible), we can instead just introduce another sleep implementation to simply use the gloo_timers::future::sleep.

Also, this PR adds a cfg_attr for conditionally remove the Send trait from the async_trait for the RetryPolicy, as the other parts of the code do.

Fix #2754
Supersedes: #2770

Test

sdk/typespec/typespec_client_core on  wasm_sleep is 📦 v0.6.0 via 🦀 v1.88.0
💤  cargo test -q

running 90 tests
i.................................................................i.................... 87/90
...
test result: ok. 88 passed; 0 failed; 2 ignored; 0 measured; 0 filtered out; finished in 0.84s


running 23 tests
.......................
test result: ok. 23 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 3.27s


sdk/typespec/typespec_client_core on  wasm_sleep is 📦 v0.6.0 via 🦀 v1.88.0 took 4s
💤  cargo test --features=tokio -q

running 91 tests
i.....................................................................i................ 87/91
....
test result: ok. 89 passed; 0 failed; 2 ignored; 0 measured; 0 filtered out; finished in 0.10s


running 23 tests
.......................
test result: ok. 23 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 3.21s


sdk/typespec/typespec_client_core on  wasm_sleep is 📦 v0.6.0 via 🦀 v1.88.0 took 3s
💤  RUSTFLAGS='--cfg getrandom_backend="wasm_js"' wasm-pack test --node --features wasm-bindgen
[INFO]: 🎯  Checking for the Wasm target...
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.14s
[INFO]: ⬇️   Installing wasm-bindgen...
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.11s
     Running unittests src/lib.rs (/home/magodo/github/azure-sdk-for-rust/target/wasm32-unknown-unknown/debug/deps/typespec_client_core-365ee3948d4ee278.wasm)
running 2 tests
test async_runtime::tests::wasm_bindgen_task_execution ... ok
test async_runtime::tests::test_sleep ... ok

test result: ok. 2 passed; 0 failed; 0 ignored; 0 filtered out; finished in 0.17s

Some note about the wasm32 test:

  • We can't run the spawned job using futures::executor::block_on(handle), as it introduces a deadlock in browser (since the block_on blocks the current thread)
  • The sleep test I can't use Instant::now() when using node as runtime

magodo and others added 16 commits July 8, 2025 14:03
…` when target wasm

The `typespec_client_core::sleep::sleep` is using the async_runtime's sleep implementation, which is complicated to support `wasm32`, and will panic now when targeting to `wasm32`. Instead of make the `standard_runtime` to support wasm32 sleep (I wonder if it's feasible), we can instead just introduce another `sleep` implementation to simply use the `gloo_timers::future::sleep`.

Also, this PR adds a `cfg_attr` for conditionally remove the `Send` trait from the `async_trait` for the `RetryPolicy`, as the other parts of the code do.

Additionally, the `getrandom` crate will enable `wasm_js` features when targetting to `wasm`, which is necessary.
@Copilot Copilot AI review requested due to automatic review settings July 30, 2025 04:41
@github-actions github-actions bot added Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization. labels Jul 30, 2025
Copy link

Thank you for your contribution @magodo! We will review the pull request and get back to you soon.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR introduces WASM support for typespec_client_core by adding a new web_runtime module that provides a WASM-compatible async runtime implementation. The main goal is to fix the sleep functionality that was previously panicking on wasm32-unknown-unknown targets.

  • Adds a new WasmBindgenRuntime implementation using gloo-timers for sleep and wasm-bindgen-futures for task spawning
  • Updates the RetryPolicy trait to conditionally remove the Send bound for WASM targets
  • Adds comprehensive test coverage for WASM functionality using wasm-bindgen-test

Reviewed Changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
Cargo.toml Adds workspace dependencies for gloo-timers and wasm-bindgen-futures
sdk/typespec/typespec_client_core/Cargo.toml Adds optional dependencies and WASM-specific features configuration
sdk/typespec/typespec_client_core/src/async_runtime/mod.rs Updates runtime creation logic to prioritize WASM runtime when appropriate
sdk/typespec/typespec_client_core/src/async_runtime/web_runtime.rs Implements new WASM-compatible async runtime using browser APIs
sdk/typespec/typespec_client_core/src/async_runtime/tests.rs Adds WASM-specific tests and conditional compilation for existing tests
sdk/typespec/typespec_client_core/src/async_runtime/standard_runtime.rs Updates sleep method signature to use TaskFuture type alias
sdk/typespec/typespec_client_core/src/http/policies/retry/mod.rs Conditionally removes Send bound from async_trait for WASM compatibility
eng/dict/crates.txt Adds gloo to the dictionary for spell checking

magodo and others added 2 commits July 30, 2025 16:00
…e.rs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…e.rs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wasm32-unknown-unknown failed to run
2 participants