How to shrink .wasm size in a cargo workspace?

Hi again, sorry for another wasm question, there doesn't seem to be a lot of wasm experts around here but I haven't had any luck anywhere (also asking in the wg-wasm channel of rust discord without success) so I'll ask anyway.

In the guide, there is a section about shrinking .wasm size. However, some of the advice there like enabling lto and setting opt-level = "z" requires changes of the profile in Cargo.toml. Same for usage of the twiggy profiler, which requires to add wasm-opt = ['-g', '-O'] to the wasm-pack package profile.

But the problem is I have my wasm code inside a workspace, like follows:

[workspace]
resolver = "2"
members = [
  "my-lib",
  "my-cli",
  "my-wasm-pkg",
]

And in that situation, when I modify the workspace/my-wasm-pkg/Cargo.toml file with some profile config, wasm-pack is telling me that it's ignoring my config:

[INFO]: Checking for the Wasm target...
[INFO]: Compiling to Wasm...
warning: profiles for the non root package will be ignored, specify profiles at the workspace root

And in practice, it seems indeed to be ignored since there is no change whatsoever on the pkg/...wasm produced.

My problem is that some of that config, I don't want to put inside the workspace cargo.toml like the z profile that I want to avoid in my my-cli. And for some other config, like the

[package.metadata.wasm-pack.profile.release]
wasm-opt = ['-g', '-O']

that we are supposed to add to be able to use the twiggy profiler, is not possible to add to the workspace cargo.toml since it makes the build crash with:

Error: Error during execution of `cargo metadata`: error: failed to parse manifest at `.../workspace/Cargo.toml`

Caused by:
  missing field `name` for key `package`

So do you know how to shrink .wasm size when using a cargo workspace? Or do you think I should abandon my workspace and split my-wasm-pkg into another separate repo? That would be very annoying, but if I have no choice, I guess I have no choice.

This is unfortunately an unsolved limitation of Cargo.

https://github.com/rust-lang/cargo/issues/8264

https://github.com/rust-lang/cargo/issues/4897

1 Like

Thanks @kornel for the very relevant links!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.