cloudflare / workers-rs

Write Cloudflare Workers in 100% Rust via WebAssembly
Apache License 2.0
2.53k stars 269 forks source link

[BUG] Compilation error when running npx wrangler dev command #620

Closed sontixyou closed 1 month ago

sontixyou commented 1 month ago

Is there an existing issue for this?

What version of workers-rs are you using?

0.3.3 features = ['http', 'axum']

What version of wrangler are you using?

3.72.0

Describe the bug

Observed behavior

Compilation error when running npx wrangler dev command

Expected behavior

Compilation successfully and run local server on localhost:8787 when running npx wrangler dev command.

Steps To Reproduce

Minimal repo is here. https://github.com/sontixyou/first_rusty_worker

I note the log output from my execution of the command. I executed cargo generate cloudflare/workers-rs command.

$ cargo generate cloudflare/workers-rs
⚠️    Favorite `cloudflare/workers-rs` not found in config, using it as a git repository: https://github.com/cloudflare/workers-rs.git
βœ” 🀷   Which template should be expanded? Β· templates/axum
🀷   Project Name: first_rusty_worker
πŸ”§   Destination: /Users/user_name/projects/rust-projects/first_rusty_worker ...
πŸ”§   project-name: first_rusty_worker ...
πŸ”§   Generating template ...
πŸ”§   Moving generated files into: `/Users/user_name/projects/rust-projects/first_rusty_worker`...
πŸ”§   Initializing a fresh Git repository
✨   Done! New project created /Users/user_name/projects/rust-projects/first_rusty_worker

Next I executed npx wrangler dev command

$ npx wrangler dev

 ⛅️ wrangler 3.72.0
-------------------

Running custom build: cargo install -q worker-build && worker-build --release
[INFO]: 🎯  Checking for the Wasm target...
[INFO]: πŸŒ€  Compiling to Wasm...
   Compiling worker v0.3.3
error[E0599]: no method named `unwrap` found for struct `js_sys::Iterator` in the current scope
  --> /Users/user_name/.cargo/registry/src/index.crates.io-6f17d22bba15001f/worker-0.3.3/src/headers.rs:83:14
   |
80 | / ...   self.0
81 | | ...       .keys()
82 | | ...       // Header.keys() doesn't error: https://developer.mozilla.org/en-US/docs/Web/API/Headers...
83 | | ...       .unwrap()
   | |           -^^^^^^ method not found in `Iterator`
   | |___________|
   |

error[E0599]: no method named `unwrap` found for struct `js_sys::Iterator` in the current scope
  --> /Users/user_name/.cargo/registry/src/index.crates.io-6f17d22bba15001f/worker-0.3.3/src/headers.rs:95:14
   |
92 | / ...   self.0
93 | | ...       .values()
94 | | ...       // Header.values() doesn't error: https://developer.mozilla.org/en-US/docs/Web/API/Heade...
95 | | ...       .unwrap()
   | |           -^^^^^^ method not found in `Iterator`
   | |___________|
   |

error[E0599]: no method named `unwrap` found for struct `js_sys::Iterator` in the current scope
  --> /Users/user_name/.cargo/registry/src/index.crates.io-6f17d22bba15001f/worker-0.3.3/src/headers.rs:69:14
   |
66 | / ...   self.0
67 | | ...       .entries()
68 | | ...       // Header.entries() doesn't error: https://developer.mozilla.org/en-US/docs/Web/API/Head...
69 | | ...       .unwrap()
   | |           -^^^^^^ method not found in `Iterator`
   | |___________|
   |

For more information about this error, try `rustc --explain E0599`.
error: could not compile `worker` (lib) due to 3 previous errors
Error: Compiling your crate to WebAssembly failed
Caused by: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit status: 101
  full command: cd "/Users/user_name/projects/rust-projects/first_rusty_worker" && "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"
Error: wasm-pack exited with status exit status: 1

✘ [ERROR] Running custom build `cargo install -q worker-build && worker-build --release` failed. There are likely more logs from your build command above.

πŸͺ΅  Logs were written to "/Users/user_name/Library/Preferences/.wrangler/logs/wrangler-2024-08-17_11-46-54_743.log"
kflansburg commented 1 month ago

This is caused by https://github.com/cloudflare/workers-rs/issues/617

Can you try pinning js_sys in your Cargo.toml?

sontixyou commented 1 month ago

I try pinning js_sys version

error[E0599]: no method named `unwrap` found for struct `js_sys::Iterator` in the current scope
  --> /Users/user_name/.cargo/registry/src/index.crates.io-6f17d22bba15001f/worker-0.3.3/src/headers.rs:69:14
   |
66 | /         self.0
67 | |             .entries()
68 | |             // Header.entries() doesn't error: https://developer.mozilla.org/en-US/docs/Web/API/Headers/entries
69 | |             .unwrap()
   | |             -^^^^^^ method not found in `Iterator`
   | |_____________|
   | 

error[E0277]: the `?` operator can only be applied to values that implement `Try`
  --> /Users/user_name/.cargo/registry/src/index.crates.io-6f17d22bba15001f/worker-0.3.3/src/http/header.rs:11:16
   |
11 |     for res in from_headers.entries()?.into_iter() {
   |                ^^^^^^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `js_sys::Iterator`
   |
   = help: the trait `Try` is not implemented for `js_sys::Iterator`

Some errors have detailed explanations: E0277, E0599.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `worker` (lib) due to 2 previous errors
Error: Compiling your crate to WebAssembly failed
Caused by: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit status: 101
  full command: cd "/Users/user_name/projects/rust-projects/first_rusty_worker" && "cargo" "build" "--lib" "--release" "--target" "wasm32-unknown-unknown"
Error: wasm-pack exited with status exit status: 1

✘ [ERROR] Running custom build `cargo install -q worker-build && worker-build --release` failed. There are likely more logs from your build command above.

πŸͺ΅  Logs were written to "/Users/user_name/Library/Preferences/.wrangler/logs/wrangler-2024-08-17_12-28-11_661.log"
sontixyou commented 1 month ago

@kflansburg I tried pin that crate version. https://github.com/sontixyou/first_rusty_worker/commit/43a93e97ca5fde51f7f72ce697db13cfd0445152 But I can't pin version of js-sys crate. Because js-sys crate depends wasm-bindgen. wasm-bindgen crate pinn js-sys ver 0.3.70. https://github.com/sontixyou/build_error_worker/blob/43a93e97ca5fde51f7f72ce697db13cfd0445152/Cargo.lock#L292

ref: https://github.com/rustwasm/wasm-bindgen/blob/main/crates/js-sys/Cargo.toml#L17 commit ref: https://github.com/rustwasm/wasm-bindgen/commit/bf9807c5a0c593cf03a5f648b93a738e74fce1d0#diff-2ea8bb4c7962b2dd1eeccf6f96b030787bfec583a77f3a76a9a43ba3c95fef6b

sontixyou commented 1 month ago

I pinned wasm-bindgen crate and it still failed to compile.

wasm-bindgen = "0.2.92"
kflansburg commented 1 month ago

You need:

wasm-bindgen = "=0.2.92"

Cargo will automatically pull in newer patch versions otherwise.

sontixyou commented 1 month ago

Thank you. I tried two methods. I was able to run a local server. But I'd like to run it on the latest version of the crate.

Is it correct that this worker-rs crate needs some kind of change?

Method1: Specified version of was-bindgen crate

ref : https://github.com/cloudflare/workers-rs/issues/620#issuecomment-2294882873

I install these crates succeessfully.

error[E0599]: no method named `unwrap` found for struct `js_sys::Iterator` in the current scope
  --> /Users/user_name/.cargo/registry/src/index.crates.io-6f17d22bba15001f/worker-0.3.3/src/headers.r
s:85:24
   |
85 |             .map(|a| a.unwrap().as_string().unwrap())
   |                        ^^^^^^ method not found in `Iterator`

error[E0599]: no method named `unwrap` found for struct `js_sys::Iterator` in the current scope
  --> /Users/user_name/.cargo/registry/src/index.crates.io-6f17d22bba15001f/worker-0.3.3/src/headers.r
s:97:24
   |
97 |             .map(|a| a.unwrap().as_string().unwrap())
   |                        ^^^^^^ method not found in `Iterator`

error[E0631]: type mismatch in function arguments
   --> /Users/user_name/.cargo/registry/src/index.crates.io-6f17d22bba15001f/worker-0.3.3/src/headers.
rs:71:18
    |
71  |             .map((|a| a.unwrap().into()) as F1)
    |              --- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |              |   |
    |              |   expected due to this
    |              |   found signature defined here
    |              required by a bound introduced by this call
    |
    = note: expected function signature `fn(js_sys::Iterator) -> _`
               found function signature `fn(std::result::Result<wasm_bindgen::JsValue, wasm_bindge
n::JsValue>) -> _`
note: required by a bound in `std::iter::Iterator::map`
   --> /Users/user_name/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/co
re/src/iter/traits/iterator.rs:764:12
    |
761 |     fn map<B, F>(self, f: F) -> Map<Self, F>
    |        --- required by a bound in this associated function
...
764 |         F: FnMut(Self::Item) -> B,
    |            ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Iterator::map`

error[E0599]: the method `map` exists for struct `Map<IntoIter<Iterator>, fn(Result<JsValue, JsVal
ue>) -> Array>`, but its trait bounds were not satisfied
  --> /Users/user_name/.cargo/registry/src/index.crates.io-6f17d22bba15001f/worker-0.3.3/src/headers.r
s:73:14
   |
66 | / ...   self.0
67 | | ...       .entries()
68 | | ...       // Header.entries() doesn't error: https://developer.mozilla.org/en-US/docs/We...
69 | | ...       .into_iter()
...  |
72 | | ...       // The entries iterator always returns an array[2] of strings
73 | | ...       .map(|a: Array| (a.get(0).as_string().unwrap(), a.get(1).as_string().unwrap()))
   | |           -^^^ method cannot be called due to unsatisfied trait bounds
   | |___________|
   | 
   |
  ::: /Users/user_name/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/cor
e/src/iter/adapters/map.rs:62:1
   |
62 |   pub struct Map<I, F> {
   |   -------------------- doesn't satisfy `_: Iterator`
   |
   = note: the full type name has been written to '/Users/user_name/projects/rust-projects/first_rusty
_worker/target/wasm32-unknown-unknown/release/deps/worker-e89e2ad91ae39750.long-type-3619179159829
353566.txt'
   = note: consider using `--verbose` to print the full type name to the console
   = note: the following trait bounds were not satisfied:
           `<fn(std::result::Result<wasm_bindgen::JsValue, wasm_bindgen::JsValue>) -> js_sys::Arra
y as FnOnce<(js_sys::Iterator,)>>::Output = _`
           which is required by `std::iter::Map<std::result::IntoIter<js_sys::Iterator>, fn(std::r
esult::Result<wasm_bindgen::JsValue, wasm_bindgen::JsValue>) -> js_sys::Array>: std::iter::Iterato
r`
           `fn(std::result::Result<wasm_bindgen::JsValue, wasm_bindgen::JsValue>) -> js_sys::Array
: FnMut<(js_sys::Iterator,)>`
           which is required by `std::iter::Map<std::result::IntoIter<js_sys::Iterator>, fn(std::r
esult::Result<wasm_bindgen::JsValue, wasm_bindgen::JsValue>) -> js_sys::Array>: std::iter::Iterato
r`
           `std::iter::Map<std::result::IntoIter<js_sys::Iterator>, fn(std::result::Result<wasm_bi
ndgen::JsValue, wasm_bindgen::JsValue>) -> js_sys::Array>: std::iter::Iterator`
           which is required by `&mut std::iter::Map<std::result::IntoIter<js_sys::Iterator>, fn(s
td::result::Result<wasm_bindgen::JsValue, wasm_bindgen::JsValue>) -> js_sys::Array>: std::iter::It
erator`

error[E0277]: the `?` operator can only be applied to values that implement `Try`
  --> /Users/user_name/.cargo/registry/src/index.crates.io-6f17d22bba15001f/worker-0.3.3/src/http/head
er.rs:13:24
   |
13 |         let a: Array = res?.into();
   |                        ^^^^ the `?` operator cannot be applied to type `js_sys::Iterator`
   |
   = help: the trait `Try` is not implemented for `js_sys::Iterator`

Some errors have detailed explanations: E0277, E0599, E0631.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `worker` (lib) due to 5 previous errors
Error: Compiling your crate to WebAssembly failed
Caused by: Compiling your crate to WebAssembly failed
Caused by: failed to execute `cargo build`: exited with exit status: 101
  full command: cd "/Users/user_name/projects/rust-projects/first_rusty_worker" && "cargo" "build" "--
lib" "--release" "--target" "wasm32-unknown-unknown"
Error: wasm-pack exited with status exit status: 1

✘ [ERROR] Running custom build `cargo install -q worker-build && worker-build --release` failed. T
here are likely more logs from your build command above.

Method2: Change worker-rs crate version

ref : https://github.com/sontixyou/first_rusty_worker/commit/b896420dd9007c192115ce6f441d5b4ee4265385

I executed npx wrangler dev successfully.