Leptos 0.7 GetRandom Wasm JS Issue

Updated on

DISCLAIMER: Expressed views on this blog are my own.

You'll upgrade leptos at some point and encounter an issue related to getrandom. It's not really a leptos issue since it still uses 0.2, but more of when Leptos builds for wasm target.

Getrandom crate was updated to 0.3. Leptos uses 0.2 still, but because Leptos uses wasm target for hydrate and your current system target for ssr, you'll be confused on what the issue is. Most likely you brought in a crate like uuid that brings in latest getrandom and uuid is included on the hydrate wasm build.

Solution 1

Disable uuid and getrandom on wasm side, include only on ssr. uuid is only an example.

getrandom = { version = "^0.3", optional = true }
uuid
= { version = "^1.13", optional = true }

....

ssr = [
...
"dep:getrandom",
"dep:uuid",
...
]

Solution 2

Just follow the instructions if you must include getrandom 0.3 on the wasm side. You'll want to include flags for hydrate and enable the getrandom wasm backend for wasm target.

hydrate = [
...
"getrandom/wasm_js",
"uuid/js",
...
]

In your project, you must include this in your `./cargo/config.toml` file. Yes, you can create this in your project.

[target.wasm32-unknown-unknown]
rustflags = [
  "--cfg", "getrandom_backend=\"wasm_js\""
]

Mac linking issue

Theres a point where leptos build will fail due to a linking problem.
Something like

1  0x104817254  ld::DynamicAtomFile::makeNamedAtom(std::__1::basic_string_view<char, std::__1::char_traits<char>>, ld::file_format::Scope, bool) + 488
          2  0x1047db9b0  ld::InputFiles::SliceParser::parseObjectFile(mach_o::Header const*) const + 9268
          3  0x1047edba0  ld::InputFiles::parseAllFiles(void (ld::AtomFile const*) block_pointer)::$_0::operator()(unsigned long, ld::FileInfo const&) const + 432
          4  0x19efed5f4  _dispatch_client_callout2 + 20
          5  0x19f001d54  _dispatch_apply_invoke3 + 336
          6  0x19efed5b4  _dispatch_client_callout + 20
          7  0x19efeee00  _dispatch_once_callout + 32
          8  0x19f000d80  _dispatch_apply_invoke + 252
          9  0x19efed5b4  _dispatch_client_callout + 20
          10  0x19efff6a0  _dispatch_root_queue_drain + 860
          11  0x19efffcd8  _dispatch_worker_thread2 + 156
          12  0x19f19c39c  _pthread_wqthread + 228
          ld: Assertion failed: (name.size() <= maxLength), function makeSymbolStringInPlace, file SymbolString.cpp, line 74.
          clang: error: linker command failed with exit code 1 (use -v to see invocation)


Look at the last line, you will see that some symbol has aa really long name that Apple's linker cannot handle, so you will have to switch to lld.


brew install lld

In your project, you must include this in your `./cargo/config.toml` file. Yes, you can create this in your project. You could consider also `~/.cargo/config.toml` for system wide linker change.

[target.aarch64-apple-darwin]
linker = "/opt/homebrew/opt/lld/bin/ld.lld"

I did a system wide change for this part.

You just read "Leptos 0.7 GetRandom Wasm JS Issue". Please share if you liked it!
You can read more recent posts here.