mozilla / uniffi-rs

a multi-language bindings generator for rust
https://mozilla.github.io/uniffi-rs/
Mozilla Public License 2.0
2.9k stars 232 forks source link

Unable to find path to my udl file when generating a static library for swift #2253

Closed sarvesh-sharma-yral closed 1 month ago

sarvesh-sharma-yral commented 2 months ago

Configuration: Mac OS Sequoia 15.0 rustc 1.81.0

Here's my project structure:

Screenshot 2024-10-04 at 4 06 47 PM

My chain of commands:

cargo clean
cargo build
RUST_BACKTRACE=1 cargo run --bin uniffi-bindgen generate --library target/debug/libuniffitest.a --language swift --out-dir out (failing)

Cargo.toml:

[package]
name = "uniffi-test"
version = "0.1.0"
edition = "2021"

[dependencies]
uniffi = { version = "0.28.1", features = ["cli"] }
uniffi_macros = "0.28.1"
tokio = { version = "1.28", features = ["macros", "rt-multi-thread"] }
ic-agent = "0.38.0"
thiserror = "1.0"

[build-dependencies]
uniffi = { version = "0.28.1", features = [ "build", "cli" ] }
uniffi_build = "0.28.1"

[lib]
crate-type = ["staticlib"]
name = "uniffitest"

[[bin]]
name = "uniffi-bindgen"
path = "uniffi-bindgen.rs"

lib.rs:

use ic_agent::{Agent, AgentError, export::Principal};
use ic_agent::export::PrincipalError;

uniffi::include_scaffolding!("ic_agent_test");

#[derive(Debug, thiserror::Error)]
pub enum RequestError {
    #[error("Request failed")]
    RequestFailed
}

impl From<PrincipalError> for RequestError {
    fn from(_: PrincipalError) -> Self {
        RequestError::RequestFailed
    }
}

impl From<AgentError> for RequestError {
    fn from(_: AgentError) -> Self {
        RequestError::RequestFailed
    }
}

pub async fn create_agent() -> Result<Agent, RequestError> {
    let agent = Agent::builder()
    .with_url("https://ic0.app/")
    .build()?;
    agent.fetch_root_key().await?;
    Ok(agent)
}

pub async fn update_function(
    canister_id: String,
    method: String,
    arg: Vec<u8>
) -> Result<Vec<u8>, RequestError> {
    let agent = create_agent().await?;
    let principal = Principal::from_text(canister_id)?;
    let response = agent
    .update(&principal, &method)
    .with_arg(arg)
    .call_and_wait()
    .await?;
    Ok(response)    
}

pub async fn query_function(
    canister_id: String,
    method: String,
    arg: Vec<u8>,
) -> Result<Vec<u8>, RequestError> {
    let agent = create_agent().await?;
    let principal = Principal::from_text(canister_id)?;
    let response = agent
        .query(&principal, &method)
        .with_arg(arg)
        .call()
        .await?;
    Ok(response)
}

build.rs:

fn main() {
    uniffi_build::generate_scaffolding("src/ic_agent_test.udl").unwrap();
    println!("cargo:rerun-if-changed=src/ic_agent_test.udl");
}

Failed command output: _Finished dev profile [unoptimized + debuginfo] target(s) in 0.29s Running target/debug/uniffi-bindgen generate --library target/debug/libuniffitest.a --language swift --out-dir out thread 'main' panicked at /Users/sarveshsharma/.cargo/registry/src/index.crates.io-6f17d22bba15001f/uniffi-0.28.1/src/lib.rs:28:21: called Result::unwrap() on an Err value: No path known to UDL files for 'uniffitest'

Stack backtrace: 0: std::backtrace_rs::backtrace::libunwind::trace at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5 1: std::backtrace_rs::backtrace::trace_unsynchronized at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5 2: std::backtrace::Backtrace::create at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/backtrace.rs:331:13 3: anyhow::context::<impl anyhow::Context<T,core::convert::Infallible> for core::option::Option>::context at /Users/sarveshsharma/.cargo/registry/src/index.crates.io-6f17d22bba15001f/anyhow-1.0.89/src/backtrace.rs:27:14 4: ::get_udl at /Users/sarveshsharma/.cargo/registry/src/index.crates.io-6f17d22bba15001f/uniffi_bindgen-0.28.1/src/cargo_metadata.rs:27:20 5: uniffi_bindgen::library_mode::load_udl_metadata at /Users/sarveshsharma/.cargo/registry/src/index.crates.io-6f17d22bba15001f/uniffi_bindgen-0.28.1/src/library_mode.rs:170:23 6: uniffi_bindgen::library_mode::find_components at /Users/sarveshsharma/.cargo/registry/src/index.crates.io-6f17d22bba15001f/uniffi_bindgen-0.28.1/src/library_mode.rs:114:43 7: uniffi_bindgen::library_mode::generate_bindings at /Users/sarveshsharma/.cargo/registry/src/index.crates.io-6f17d22bba15001f/uniffi_bindgen-0.28.1/src/library_mode.rs:46:26 8: uniffi::cli::gen_library_mode at /Users/sarveshsharma/.cargo/registry/src/index.crates.io-6f17d22bba15001f/uniffi-0.28.1/src/cli.rs:207:38 9: uniffi::cli::run_main at /Users/sarveshsharma/.cargo/registry/src/index.crates.io-6f17d22bba15001f/uniffi-0.28.1/src/cli.rs:297:17 10: uniffi::uniffi_bindgen_main at /Users/sarveshsharma/.cargo/registry/src/index.crates.io-6f17d22bba15001f/uniffi-0.28.1/src/lib.rs:28:5 11: uniffi_bindgen::main at ./uniffi-bindgen.rs:2:5 12: core::ops::function::FnOnce::call_once at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/ops/function.rs:250:5 13: std::sys::backtrace::__rust_begin_short_backtrace at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/sys/backtrace.rs:152:18 14: std::rt::lang_start::{{closure}} at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/rt.rs:162:18 15: core::ops::function::impls::<impl core::ops::function::FnOnce for &F>::call_once at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/ops/function.rs:284:13 16: std::panicking::try::do_call at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:557:40 17: std::panicking::try at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:521:19 18: std::panic::catch_unwind at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panic.rs:350:14 19: std::rt::lang_start_internal::{{closure}} at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/rt.rs:141:48 20: std::panicking::try::do_call at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:557:40 21: std::panicking::try at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:521:19 22: std::panic::catch_unwind at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panic.rs:350:14 23: std::rt::lang_start_internal at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/rt.rs:141:20 24: std::rt::lang_start at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/rt.rs:161:17 25: _main stack backtrace: 0: rust_begin_unwind at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/std/src/panicking.rs:665:5 1: core::panicking::panic_fmt at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/panicking.rs:74:14 2: core::result::unwrap_failed at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/result.rs:1679:5 3: core::result::Result<T,E>::unwrap at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/result.rs:1102:23 4: uniffi::uniffi_bindgen_main at /Users/sarveshsharma/.cargo/registry/src/index.crates.io-6f17d22bba15001f/uniffi-0.28.1/src/lib.rs:28:5 5: uniffi_bindgen::main at ./uniffi-bindgen.rs:2:5 6: core::ops::function::FnOnce::call_once at /rustc/eeb90cda1969383f56a2637cbd3037bdf598841c/library/core/src/ops/function.rs:250:5 note: Some details are omitted, run with RUST_BACKTRACE=full for a verbose backtrace._

mhammond commented 2 months ago

This means we can't find a crate called uniffitest in your workspace so we can load its UDL.

Sorg666 commented 1 month ago

I have the same error. It is strange that uniffi can't find my crate with udl, despite it being present in the output of cargo metadata command. I have a minimal reproducible example in my repo here: https://github.com/Sorg666/uniffi-bindgen-error-example Can you take a look?

Sorg666 commented 1 month ago

Okay, I think I found a work around. In Cargo.toml add "lib" to crate-type (in the repo above it would look like this: crate-type = ["lib", "cdylib"]). I think it was fixed here https://github.com/mozilla/uniffi-rs/commit/d63ed664208c42e7e127103bdfd7dea555e938bd, but not released yet

sarvesh-sharma-yral commented 1 month ago

Worked for me as well! Thanks @Sorg666