Skip to content

Does not link env module when compiling libs with [no_std] #333

Open

Description

@Loara

cargo component build fails to compile Rust libraries with [no_std] flags because it cannot resolve the env import interface:

error: failed to decode world from module

Caused by:
    0: module was not valid
    1: module requires an import interface named `env`

Minimum reproducible example:

src/lib.rs (it uses an allocator from the lol_alloc crate)

#![no_std]

extern crate alloc;
extern crate lol_alloc;

#[allow(warnings)]
mod bindings;

#[global_allocator]
static ALLOCATOR : lol_alloc::FailAllocator = lol_alloc::FailAllocator;

#[panic_handler]
fn panic(_i : &core::panic::PanicInfo) -> ! {
    core::arch::wasm32::unreachable()
}

use bindings::Guest;
use alloc::string::{String, ToString};

struct Component;

impl Guest for Component {
    /// Say hello!
    fn hello_world() -> String {
        "Hello, World!".to_string()
    }
}

bindings::export!(Component with_types_in bindings);

wit/world.wit

package component:nostd;

/// An example world for the component to target.
world example {
    export hello-world: func() -> string;
}

Activity

  1. primoly commented on Sep 11, 2024

    @primoly
    Contributor

    For what it’s worth, the compilation succeeds but the final output wasm imports the symbols "env" "memset" and "env" "memcpy".

    To avoid this you can use RUSTFLAGS='-C target-feature=+bulk-memory' cargo component build to generate a wasip1 component which depends on the widely supported1 bulk memory operations.

    Alternatively you can use cargo component build --target wasm32-unknown-unknown to generate a non Wasi component instead.

    Footnotes

    1. https://webassembly.org/features/ ↩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions