Dioxus: Difference between revisions

Document why wasm-bindgen version mismatch occurs
mNo edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 4: Line 4:


==== ''wasm-bindgen'' mismatch ====
==== ''wasm-bindgen'' mismatch ====
'''Note:''' As of Nixpkgs 25.11, the ''wasm-bindgen-cli'' is automatically bundled with the correct version when using ''dioxus-cli'', so the workarounds described below are no longer necessary.
A common issue when using the ''dioxus-cli'' version from Nixpkgs is encountering the following error:<syntaxhighlight lang="text">
A common issue when using the ''dioxus-cli'' version from Nixpkgs is encountering the following error:<syntaxhighlight lang="text">
it looks like the Rust project used to create this Wasm file was linked against
it looks like the Rust project used to create this Wasm file was linked against
Line 12: Line 14:
</syntaxhighlight>
</syntaxhighlight>


==== Why does this happen? ====
===== Why does this happen? =====
Unlike most Rust crates, ''wasm-bindgen'' doesn't follow [https://semver.org semantic versioning (SemVer)]. This means even a patch version difference (like 0.2.97 vs 0.2.98) can contain breaking changes or incompatibilities.
Unlike most Rust crates, ''wasm-bindgen'' doesn't follow [https://semver.org semantic versioning (SemVer)]. This means even a patch version difference (like 0.2.97 vs 0.2.98) can contain breaking changes or incompatibilities.


Line 56: Line 58:


==== Choosing ''wasm-bindgen-cli'' version ====
==== Choosing ''wasm-bindgen-cli'' version ====
You will also want to ensure the version of ''wasm-bindgen-cli'' matches whatever is in your lock file.<syntaxhighlight lang="nix">
You will also want to ensure the version of ''wasm-bindgen-cli'' matches whatever is in your lock file.<syntaxhighlight lang="nix">cargoLock = builtins.fromTOML (builtins.readFile ./Cargo.lock);
cargoLock = builtins.fromTOML (builtins.readFile ./Cargo.lock);


wasmBindgen = pkgs.lib.findFirst
wasmBindgen = pkgs.lib.findFirst
Line 64: Line 65:
   cargoLock.package;
   cargoLock.package;


wasm-bindgen-cli = pkgs.wasm-bindgen-cli.override {
wasm-bindgen-cli = pkgs.buildWasmBindgenCli rec {
  version = wasmBindgen.version;
  src = pkgs.fetchCrate {
   hash = pkgs.lib.fakeHash;
    pname = "wasm-bindgen-cli";
   cargoHash = pkgs.lib.fakeHash;
    version = wasmBindgen.version;
};
    hash = pkgs.lib.fakeHash;
  };
 
   cargoDeps = pkgs.rustPlatform.fetchCargoVendor {
    inherit src;
    inherit (src) pname version;
    hash = pkgs.lib.fakeHash;
   };
};</syntaxhighlight>'''Example flake.nix'''<syntaxhighlight lang="nix">
{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
 
    # For installing non-standard rustc versions
    rust-overlay.url = "github:oxalica/rust-overlay";
    rust-overlay.inputs.nixpkgs.follows = "nixpkgs";
  };
 
  outputs = {
    self,
    rust-overlay,
    nixpkgs,
  }:
  {
    devShells.x86_64-linux.default = let
      pkgs = import nixpkgs {
        system = "x86_64-linux";
        overlays = [
          rust-overlay.overlays.default
        ];
      };
 
      rustShellToolchain = (pkgs.rust-bin.selectLatestNightlyWith (t: t.default)).override {
        extensions = ["rust-src" "rust-analyzer"];
        targets = [ "wasm32-unknown-unknown" ];
      };
 
      dioxus-cli = pkgs.dioxus-cli.overrideAttrs (_: {
        postPatch = ''
          rm Cargo.lock
          cp ${./Dioxus.lock} Cargo.lock
        '';
 
        cargoDeps = pkgs.rustPlatform.importCargoLock {
          lockFile = ./Dioxus.lock;
        };
      });
 
      cargoLock = builtins.fromTOML (builtins.readFile ./Cargo.lock);
 
      wasmBindgen = pkgs.lib.findFirst
        (pkg: pkg.name == "wasm-bindgen")
        (throw "Could not find wasm-bindgen package")
        cargoLock.package;
 
      wasm-bindgen-cli = pkgs.buildWasmBindgenCli rec {
        src = pkgs.fetchCrate {
          pname = "wasm-bindgen-cli";
          version = wasmBindgen.version;
          hash = "sha256-txpbTzlrPSEktyT9kSpw4RXQoiSZHm9t3VxeRn//9JI=";
        };
 
        cargoDeps = pkgs.rustPlatform.fetchCargoVendor {
          inherit src;
          inherit (src) pname version;
          hash = "sha256-J+F9SqTpH3T0MbvlNKVyKnMachgn8UXeoTF0Pk3Xtnc=";
        };
      };
    in
      pkgs.mkShell {
        name = "dioxus";
        packages = [ rustShellToolchain dioxus-cli wasm-bindgen-cli ];
    };
  };
}
</syntaxhighlight>
</syntaxhighlight>
[[Category:Rust]]
[[Category:Rust]]