matrix-org / matrix-rust-sdk-crypto-wasm

Apache License 2.0
13 stars 7 forks source link

Updating dependencies breaks generated TypeScript #161

Open andybalaam opened 2 days ago

andybalaam commented 2 days ago

If I am using Rust 1.82.0 and run a cargo update then I get this error:

$ yarn build
...
[INFO]: ⬇️  Installing wasm-bindgen...
[INFO]: Optimizing wasm binaries with `wasm-opt`...
[wasm-validator error in function olmmachine_receiveSyncChanges\20externref\20shim] unexpected false: table.fill requires bulk-memory [--enable-bulk-memory], on 
(table.fill $1
 (local.get $7)
 (ref.null noextern)
 (i32.const 1)
)
...
Error: failed to execute `wasm-opt`: exited with exit status: 1
  full command: "/home/andy/.cache/.wasm-pack/wasm-opt-1ceaaea8b7b5f7e0/bin/wasm-opt" "/home/andy/code/public/matrix-rust/matrix-rust-sdk-crypto-wasm/pkg/matrix_sdk_crypto_wasm_bg.wasm" "-o" "/home/andy/code/public/matrix-rust/matrix-rust-sdk-crypto-wasm/pkg/matrix_sdk_crypto_wasm_bg.wasm-opt.wasm" "-Oz" "-g"
To disable `wasm-opt`, add `wasm-opt = false` to your package metadata in your `Cargo.toml`.
Caused by: failed to execute `wasm-opt`: exited with exit status: 1

Reading around this, it looks like some change in the dependencies causes this version of rustc¹ to use bulk memory operations in the generated wasm.

¹ Before I upgraded to rustc 1.82.0 it all actually built fine with no errors, but staying on an old rustc version doesn't seem viable.

If I enable bulk memory operations in wasm-opt by doing:

diff --git a/Cargo.toml b/Cargo.toml
index a5a87bd..df4b95e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -37,7 +37,7 @@ dwarf-debug-info = true
 #  * `-Oz`: optimise for size
 #  * `-g`: include the "name" section (which holds the printable names for
 #    symbols) in the output.
-wasm-opt = ['-Oz', '-g']
+wasm-opt = ['-Oz', '-g', '--enable-bulk-memory']

Then the build works OK but linting the generated TypeScript fails:

$ yarn lint
...
> tsc --noEmit
pkg/matrix_sdk_crypto_wasm_bg.wasm.d.ts:5:59 - error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
5 export function attachment_encrypt(a: number, b: number): Array;
                                                            ~~~~~
...

It looks like this lint is correctly identifying invalid/incomplete TypeScript being emitted (by wasm-bindgen?).

So we are unable to update our dependencies until we can fix this problem.

richvdh commented 2 days ago

possibly we can work around this by post-processing the generated typescript, but that is clearly an unsatisfactory solution :/

andybalaam commented 2 days ago

I strongly suspect that the dependency update causing the change is the wasm-bindgen dependency itself. This looks like a similar issue: https://github.com/rustwasm/wasm-bindgen/issues/4234

andybalaam commented 2 days ago

Ah, it looks like this is caused by https://github.com/rustwasm/wasm-bindgen/issues/4207 or at least very closely related.

andybalaam commented 2 days ago

So we should stay on wasm-bindgen v0.2.93 until https://github.com/rustwasm/wasm-bindgen/issues/4207 is fixed.