Skip to content

Build cargoArtifacts (craneLib.buildDepsOnly) with the wasm32-unknown-unknown target can fail due to incompatible libs #873

@kutu-dev

Description

@kutu-dev

Describe the bug
Build cargoArtifacts (craneLib.buildDepsOnly) with the wasm32-unknown-unknown can fail due to incompatible libs, if in the workspace Cargo.toml there is listed a dependency not available on the WASM32 target (cmd_lib for example) given that there is no official workspace.target.*.dependencies key Crane will try to build it and fail in the process.

Making a system to flag a workspace dependency as not available on the WASM32 target may be required.

Reproduction
flake.nix

{
  description = "Build a cargo workspace";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";

    crane.url = "github:ipetkov/crane";

    flake-utils.url = "github:numtide/flake-utils";

    advisory-db = {
      url = "github:rustsec/advisory-db";
      flake = false;
    };
  };

  outputs =
    {
      self,
      nixpkgs,
      crane,
      flake-utils,
      advisory-db,
      ...
    }:
    flake-utils.lib.eachDefaultSystem (
      system:
      let
        pkgs = nixpkgs.legacyPackages.${system};

        inherit (pkgs) lib;

        craneLib = crane.mkLib pkgs;
        src = craneLib.cleanCargoSource ./.;

        commonArgs = {
           # >>> Here is the cross compilation to WASM32 set
           CARGO_BUILD_TARGET = "wasm32-unknown-unknown";

          inherit src;
          strictDeps = true;

          buildInputs =
            [
              # Add additional build inputs here
            ]
            ++ lib.optionals pkgs.stdenv.isDarwin [
              # Additional darwin specific inputs can be set here
              pkgs.libiconv
            ];
        };

        cargoArtifacts = craneLib.buildDepsOnly commonArgs;

        individualCrateArgs = commonArgs // {
          inherit cargoArtifacts;
          inherit (craneLib.crateNameFromCargoToml { inherit src; }) version;
          doCheck = false;
        };

        fileSetForCrate =
          crate:
          lib.fileset.toSource {
            root = ./.;
            fileset = lib.fileset.unions [
              ./Cargo.toml
              ./Cargo.lock
              (craneLib.fileset.commonCargoSources ./crates/my-common)
              (craneLib.fileset.commonCargoSources ./crates/my-workspace-hack)
              (craneLib.fileset.commonCargoSources crate)
            ];
          };

        my-cli = craneLib.buildPackage (
          individualCrateArgs
          // {
            pname = "my-cli";
            cargoExtraArgs = "-p my-cli";
            src = fileSetForCrate ./crates/my-cli;
          }
        );
      in
      {
        packages = {
          inherit my-cli;
        };
      }
    );
}

Cargo.toml

[workspace]
resolver = "2"
members = ["crates/*"]

[workspace.package]
version = "0.1.0"
edition = "2021"

[workspace.metadata.crane]
name = "my-workspace"

[workspace.dependencies]
cmd_lib = "1.9.6"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions