Skip to content

Commit

Permalink
fix: Rust canister resolve dependency (dfinity#1949)
Browse files Browse the repository at this point in the history
* Rust canister resolve dependency

* e2e

* skip shellcheck

* Fix cargo install

* Remove cargo install

* Inter-canister call from update method

* Install ic-cdk-optimizer before e2e

* Install ic-cdk-optimizer during provision-{os}

* Increase timout to 1800s
  • Loading branch information
lwshang authored Dec 8, 2021
1 parent 9b53f22 commit 09c25ba
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ jobs:
fi
export
- name: Run e2e test
run: timeout 1200 bats "e2e/$E2E_TEST"
run: timeout 1800 bats "e2e/$E2E_TEST"

aggregate:
name: e2e:required
Expand Down
4 changes: 4 additions & 0 deletions e2e/assets/rust_deps/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[workspace]
members = [
"src/rust_deps",
]
23 changes: 23 additions & 0 deletions e2e/assets/rust_deps/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"canisters": {
"multiply_deps": {
"main": "src/multiply_deps/main.mo",
"type": "motoko"
},
"rust_deps": {
"candid": "src/rust_deps/rust_deps.did",
"package": "rust_deps",
"type": "rust",
"dependencies": [
"multiply_deps"
]
}
},
"networks": {
"local": {
"bind": "127.0.0.1:8000",
"type": "ephemeral"
}
},
"version": 1
}
1 change: 1 addition & 0 deletions e2e/assets/rust_deps/patch.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# nothing to do
11 changes: 11 additions & 0 deletions e2e/assets/rust_deps/src/multiply_deps/main.mo
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
actor Multiply {

var cell : Nat = 1;

public func mul(n:Nat) : async Nat { cell *= n*3; cell };

public query func read() : async Nat {
cell
};
}

14 changes: 14 additions & 0 deletions e2e/assets/rust_deps/src/rust_deps/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "rust_deps"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
path = "lib.rs"
crate-type = ["cdylib"]

[dependencies]
ic-cdk = "0.3"
ic-cdk-macros = "0.3"
11 changes: 11 additions & 0 deletions e2e/assets/rust_deps/src/rust_deps/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use ic_cdk_macros::*;
use ic_cdk::export::candid;

#[import(canister = "multiply_deps")]
struct CounterCanister;

// Inter-canister call can only be from a update call
#[update]
async fn read() -> candid::Nat {
CounterCanister::read().await.0
}
3 changes: 3 additions & 0 deletions e2e/assets/rust_deps/src/rust_deps/rust_deps.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
service : {
"read": () -> (nat);
}
15 changes: 15 additions & 0 deletions e2e/tests-dfx/rust.bash
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,25 @@ teardown() {
assert_command dfx build hello
assert_match "ic-cdk-optimizer not installed"
cargo install ic-cdk-optimizer
# shellcheck disable=SC2030
export PATH="$HOME/.cargo/bin/:$PATH"
assert_command dfx build hello
assert_match "Executing: ic-cdk-optimizer"
assert_command dfx canister --no-wallet install hello
assert_command dfx canister --no-wallet call hello greet dfinity
assert_match '("Hello, dfinity!")'
}

@test "rust canister can resolve dependencies" {
dfx_new_rust rust_deps
install_asset rust_deps

dfx_start
assert_command dfx deploy
assert_command dfx canister call multiply_deps read
assert_match '(1 : nat)'
assert_command dfx canister call multiply_deps mul '(3)'
assert_match '(9 : nat)'
assert_command dfx canister call rust_deps read
assert_match '(9 : nat)'
}
23 changes: 22 additions & 1 deletion src/dfx/src/lib/builders/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl CanisterBuilder for RustBuilder {

fn build(
&self,
_pool: &CanisterPool,
pool: &CanisterPool,
canister_info: &CanisterInfo,
_config: &BuildConfig,
) -> DfxResult<BuildOutput> {
Expand All @@ -78,6 +78,27 @@ impl CanisterBuilder for RustBuilder {
.arg("--release")
.arg("-p")
.arg(package);

if let Ok(dependencies) = self.get_dependencies(pool, canister_info) {
for deps in dependencies {
let canister = pool.get_canister(&deps).unwrap();
cargo.env(
format!("CANISTER_ID_{}", canister.get_name()),
deps.to_text(),
);
if let Some(output) = canister.get_build_output() {
let candid_path = match &output.idl {
IdlBuildOutput::File(p) => p.as_os_str(),
};

cargo.env(
format!("CANISTER_CANDID_{}", canister.get_name()),
candid_path,
);
}
}
}

info!(
self.logger,
"Executing: cargo build --target wasm32-unknown-unknown --release -p {}", package
Expand Down

0 comments on commit 09c25ba

Please sign in to comment.