wasm-pack
wasm-pack copied to clipboard
Wasm run command in build script freezes
🐛 Bug description
I'm unsure if this is officially supported or not, I see that the documentation is scarce for build scripts.
I was hoping to try use the wasm-pack crate as a build dependency and use it in the build script for some local automation.
I tried using the build options and commands to run the wasm build command like so.
let options = BuildOptions {
path: Some(std::env::current_dir()?),
scope: None,
disable_dts: true,
target: wasm_pack::command::build::Target::NoModules,
debug: true,
dev: true,
out_dir: get_workspace_path()?.join("tms-client").join("web").join("pkg").to_string_lossy().to_string(),
out_name: Some("rust_lib_my_app".to_string()),
extra_options: vec![],
..Default::default()
};
let command = wasm_pack::command::Command::Build(options);
wasm_pack::command::run_wasm_pack(command)?;
Ok(())
But I came into an issue where the command would seemingly execute, but freeze waiting for the file lock on the build directory
Running `CARGO=/home/cj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo CARGO_CFG_OVERFLOW_CHECKS='' CARGO_CFG_PANIC=unwind CARGO_CFG_RELOCATION_MODEL=pic CARGO_CFG_TARGET_ABI='' CARGO_CFG_TARGET_ARCH=x86_64 CARGO_CFG_TARGET_ENDIAN=little CARGO_CFG_TARGET_ENV=gnu CARGO_CFG_TARGET_FAMILY=unix CARGO_CFG_TARGET_FEATURE=fxsr,sse,sse2 CARGO_CFG_TARGET_HAS_ATOMIC=16,32,64,8,ptr CARGO_CFG_TARGET_HAS_ATOMIC_EQUAL_ALIGNMENT=16,32,64,8,ptr CARGO_CFG_TARGET_HAS_ATOMIC_LOAD_STORE=16,32,64,8,ptr CARGO_CFG_TARGET_OS=linux CARGO_CFG_TARGET_POINTER_WIDTH=64 CARGO_CFG_TARGET_THREAD_LOCAL='' CARGO_CFG_TARGET_VENDOR=unknown CARGO_CFG_UB_CHECKS='' CARGO_CFG_UNIX='' CARGO_ENCODED_RUSTFLAGS='' CARGO_MANIFEST_DIR=/home/cj/Documents/code/TMS/tms-infra CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=tms-infra CARGO_PKG_README='' CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' DEBUG=true HOST=x86_64-unknown-linux-gnu LD_LIBRARY_PATH='/home/cj/Documents/code/TMS/target/debug/deps:/home/cj/Documents/code/TMS/target/debug:/home/cj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib:/home/cj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib:/home/cj/Documents/code/TMS/target/debug/build/bzip2-sys-683c5c1a62d34001/out/lib:/home/cj/Documents/code/TMS/target/debug/build/ring-b1483ee963101adc/out:/home/cj/Documents/code/TMS/target/debug/build/zstd-sys-cdfe71eec8a6e140/out' NUM_JOBS=20 OPT_LEVEL=0 OUT_DIR=/home/cj/Documents/code/TMS/target/debug/build/tms-infra-9c4e3f84f30cf6ab/out PROFILE=debug RUSTC=/home/cj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustc RUSTDOC=/home/cj/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/rustdoc TARGET=x86_64-unknown-linux-gnu /home/cj/Documents/code/TMS/target/debug/build/tms-infra-ad9ac73092db3bd4/build-script-build`
[tms-infra 0.1.0] cargo:rerun-if-changed=src
[tms-infra 0.1.0] Done!
[tms-infra 0.1.0] [INFO]: 🎯 Checking for the Wasm target...
[tms-infra 0.1.0] [INFO]: 🌀 Compiling to Wasm...
[tms-infra 0.1.0] Blocking waiting for file lock on build directory
Building [=======================> ] 312/314: tms-infra(build)
And it seems to get stuck waiting for the file lock forever.
🤔 Expected Behavior
Build script generates the wasm outputs
👟 Steps to reproduce
- Create project with build script
- Add wasm-pack as build-dependency
- Use wasm-pack to compile the rust code
🌍 Your environment
Include the relevant details of your environment. wasm-pack version: 0.12.1 rustc version: 1.79.0
this is also something I'm wanting to do, build a WASM crate as part of the build script of my Rust project. Is that an officially supported use case? Did you get anywhere with this @CJBuchel ?
I wasn't able to find a clean solution sadly. If I recall this issue came up while I was testing a different library designed for FFI between rust and flutter https://github.com/fzyzcjy/flutter_rust_bridge/discussions/2159 And the method for web FFI used wasm-pack.
If memory serves me right, I think it is technically possible to use wasm-pack in the buildscript, but you have to start nesting projects. I think what I tested was a low level crate which contained my library code that I actually wanted to compile to wasm. Then I had a higher level crate which was empty, but had a build script that depended on the lower level crate and compiled it.
I hated this method though, having a sacrificial crate for compiling was a bit ugly, and if I recall correctly. It didn't even work that well in my case, because I was trying to use a workspace to have all my stuff built and generated through a single cargo build command. But sadly, as soon as the WASM project comes into contact (is in the same build command) with the WASM generator project. It locks up. I'm not too sure why, I never looked that deeply into the wasm-pack code/CLI. My assumption is some kind of file lock but idk.
The two options in my case was to either have two separate projects that have separate build commands. One cargo build for my actual code. Then another cargo build for my wasm-generator. Or use a small shell script and just use the wasm-pack cli. Which is what I opted for.
A bit messy and annoying, I would have preferred to have an all in one solution, especially because it helps a lot for developers when doing a first time setup of the code. Having everything tied to the build would have been great. Instead of relying on scripts and such for all the little loose ends. But, I couldn't figure out a way to solve it sadly.
The following build script works for this case:
use std::error::Error;
use wasm_pack::command::{
Command,
build::{BuildOptions, Target},
run_wasm_pack,
};
fn main() -> Result<(), Box<dyn Error>> {
// Check "TARGET" in env to avoid trying to call `wasm-pack` from
// the `cargo build` process launched by `wasm-pack`
Ok(if std::env::var("TARGET")? != "wasm32-unknown-unknown" {
run_wasm_pack(Command::Build(BuildOptions {
extra_options: vec![
"-vv".to_owned(),
"--target-dir".to_owned(),
"target/wasm".to_owned(),
],
out_dir: "target/wasm".to_owned(),
target: Target::Web,
..Default::default()
}))?
} else {
()
})
}
(Though it should be noted that all your dependencies and your package will be built twice using this method -- once for your native platform, and then again for Wasm).