Skip to content

cargo-stylus fails to find WASM file when package name contains hyphens (cdylib TargetKind check bug) #439

Description

@0xReLogic

Describe the bug

When a cargo package has a hyphen in its name (e.g., nimbus-contracts) and does not explicitly set the [lib] target name in Cargo.toml using underscores, cargo stylus check / cargo stylus deploy fails with the error:
error: build did not generate wasm file

Root Cause

In stylus-tools/src/core/project/contract.rs (under Contract::try_from), the library target name is extracted by looking for TargetKind::Lib inside the package targets:

let name = package
    .targets
    .iter()
    .find_map(|t| t.kind.contains(&TargetKind::Lib).then(|| t.name.clone()))
    // If that doesn't work, then we can use the package name, and break normally.
    .unwrap_or_else(|| package.name.to_string());

However:

  1. In a Stylus project, the crate type must be cdylib (e.g., crate-type = ["cdylib", "rlib"]).
  2. In cargo-metadata, the target kind of a cdylib target maps to TargetKind::CDylib, not TargetKind::Lib.
  3. Consequently, t.kind.contains(&TargetKind::Lib) evaluates to false for cdylib targets.
  4. The code falls back to package.name.to_string(), which preserves hyphens (e.g., nimbus-contracts).
  5. cargo-stylus then checks for a WASM file named nimbus-contracts.wasm inside target/wasm32-unknown-unknown/release/deps/.
  6. But Cargo always replaces hyphens with underscores for library outputs, producing nimbus_contracts.wasm.
  7. This causes a file-not-found mismatch, failing the build check.

Suggested Fix

Update the targets finder in stylus-tools/src/core/project/contract.rs to search for TargetKind::CDylib as well:

let name = package
    .targets
    .iter()
    .find_map(|t| {
        let is_lib = t.kind.contains(&TargetKind::Lib) || t.kind.contains(&TargetKind::CDylib);
        is_lib.then(|| t.name.clone())
    })
    .unwrap_or_else(|| package.name.to_string());

Alternatively, normalize hyphens to underscores when looking up the compiled library file path.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions