How to automatic generate Candid from Rust Canister of ic-cdk="0.10.0"?

#[cfg(test)]
mod tests {
    
    #[test]
    fn did() {

      use candid::export_service;
      
      ic_cdk::export::candid::export_service!();
      [name = "__get_candid_interface_tmp_hack"]
      fn export_candid() -> String {
          export_service!();
          __export_service()
      }
      
        use std::env;
        use std::fs::write;
        use std::path::PathBuf;
    
        let dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap());
        println!("dir: {:?}", dir);
        // let dir = dir.parent().unwrap().parent().unwrap().join("candid");
        // println!("dir: {:?}", dir);
        write(dir.join("backend.did"), export_candid()).expect("Write failed.");
        println!("write finish");
    }
}

I cant generate did file from this code above .

it says:

WARNING!
An error occurred during Candid interface compatibility check for canister 'backend'.

Deployed did file should contain some service interface
Do you want to proceed? yes/No
yes

but i have no any hint of what exactly make it not compatible .

Do you really mean ic_cdk v0.1.0 or it’s a typo and you meant v0.10.0?

If the last, I shared a blog post about it.

the post is vip only .

You can open it in an incognito browser.
Otherwise there https://daviddalbusco.com/blog/automatic-candid-generation-in-rust-exploring-the-ic-cdk-v0-10-0-update/

very helpful! Mr."spider man " !

1 Like

After read the article above:
my project at the end of lib.rs file :

use ic_cdk::export_candid;
export_candid!();
btwl@btwl-virtual-machine:~/Desktop/ic/tax_lint$ dfx deploy backend
Please enter the passphrase for your identity: [hidden]
Decryption complete.
Deploying: backend
All canisters have already been created.
Building canisters...
WARN: Cannot check for vulnerabilities in rust canisters because cargo-audit is not installed. Please run 'cargo install cargo-audit' so that vulnerabilities can be detected.
Executing: cargo build --target wasm32-unknown-unknown --release -p backend --locked
    Finished release [optimized] target(s) in 1.78s
Installing canisters...
WARNING!
An error occurred during Candid interface compatibility check for canister 'backend'.

Generated did file should contain some service interface
Do you want to proceed? yes/No
yes
Module hash a6ec1bf689e6a0b4f058ff27196aa192cbe79822cf621982e32d45e959f85fd2 is already installed.
Deployed canisters.
URLs:
  Backend canister via Candid interface:
    backend: http://127.0.0.1:4943/?canisterId=bd3sg-teaaa-aaaaa-qaaba-cai&id=bkyz2-fmaaa-aaaaa-qaaaq-cai

The error says: An error occurred during Candid interface compatibility check for canister ‘backend’.

How do I find out that incompatibility?

Honestly don’t know unfortunately. I’m no rust and dfx expert.

its totally ok . you already give me a big help!

1 Like

And thanks for reply so in time ! Hope you have a good day. :grinning:

1 Like

here is my try:

  1. add this in project at the end of lib.rs file :
use ic_cdk::export_candid;
export_candid!();
  1. run cmd:
cargo build --target wasm32-unknown-unknown --release --package "backend"      --features "ic-cdk/wasi" && wasmtime "./target/wasm32-unknown-unknown/release/backend.wasm"  > ./backend/backend.did  --allow-precompiled 

this can work for me .
generate the right version of did.