Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove PullCanisterInfo type #3679

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on:
push:
branches:
- master
- ens/poc-pull-info-unreachable
pull_request:

concurrency:
Expand Down
7 changes: 6 additions & 1 deletion src/dfx/src/lib/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ mod pull;
mod rust;

pub use custom::custom_download;
pub use pull::get_pull_build_output;

#[derive(Debug)]
pub enum WasmBuildOutput {
Expand Down Expand Up @@ -148,7 +149,11 @@ pub trait CanisterBuilder {
)
})?;

let did_from_build = self.get_candid_path(pool, info, config)?;
let did_from_build = match info.get_common_output_idl_path() {
Some(p) => p,
None => self.get_candid_path(pool, info, config)?,
};

if !did_from_build.exists() {
bail!(
"Candid file: {} doesn't exist.",
Expand Down
34 changes: 22 additions & 12 deletions src/dfx/src/lib/builders/pull.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::lib::builders::{
BuildConfig, BuildOutput, CanisterBuilder, IdlBuildOutput, WasmBuildOutput,
};
use crate::lib::canister_info::pull::PullCanisterInfo;
use crate::lib::canister_info::CanisterInfo;
use crate::lib::canister_info::{CanisterInfo, PullInfo};
use crate::lib::environment::Environment;
use crate::lib::error::DfxResult;
use crate::lib::models::canister::CanisterPool;
use anyhow::anyhow;
use candid::Principal as CanisterId;
use fn_error_context::context;
use slog::o;
Expand Down Expand Up @@ -43,23 +43,33 @@ impl CanisterBuilder for PullBuilder {
canister_info: &CanisterInfo,
_config: &BuildConfig,
) -> DfxResult<BuildOutput> {
let pull_info = canister_info.as_info::<PullCanisterInfo>()?;
Ok(BuildOutput {
canister_id: *pull_info.get_canister_id(),
// It's impossible to know if the downloaded wasm is gzip or not with only the info in `dfx.json`.
wasm: WasmBuildOutput::None,
idl: IdlBuildOutput::File(pull_info.get_output_idl_path().to_path_buf()),
})
unreachable!("call get_pull_build_output directly");
}

#[context("Failed to get candid path for pull canister '{}'.", info.get_name())]
fn get_candid_path(
&self,
_pool: &CanisterPool,
info: &CanisterInfo,
_config: &BuildConfig,
) -> DfxResult<PathBuf> {
let pull_info = info.as_info::<PullCanisterInfo>()?;
let output_idl_path = pull_info.get_output_idl_path();
Ok(output_idl_path.to_path_buf())
unreachable!("pull canister must provide common_output_idl_path")
}
}

pub fn get_pull_build_output(
canister_info: &CanisterInfo,
pull_info: &PullInfo,
) -> DfxResult<BuildOutput> {
let canister_id = *pull_info.get_canister_id();
let output_idl_path = canister_info
.get_common_output_idl_path()
.ok_or_else(|| anyhow!("no common output idl path"))?;

Ok(BuildOutput {
canister_id,
// It's impossible to know if the downloaded wasm is gzip or not with only the info in `dfx.json`.
wasm: WasmBuildOutput::None,
idl: IdlBuildOutput::File(output_idl_path),
})
}
43 changes: 38 additions & 5 deletions src/dfx/src/lib/canister_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ use std::path::{Path, PathBuf};
pub mod assets;
pub mod custom;
pub mod motoko;
pub mod pull;
pub mod rust;
use self::pull::PullCanisterInfo;
use crate::lib::deps::get_candid_path_in_project;
use assets::AssetsCanisterInfo;
use custom::CustomCanisterInfo;
use motoko::MotokoCanisterInfo;
Expand Down Expand Up @@ -59,6 +58,14 @@ pub struct CanisterInfo {
pull_dependencies: Vec<(String, CanisterId)>,
gzip: bool,
init_arg: Option<String>,

pull: Option<PullInfo>,
common_output_idl_path: Option<PathBuf>,
}

#[derive(Debug)]
pub struct PullInfo {
id: Principal,
}

impl CanisterInfo {
Expand Down Expand Up @@ -133,7 +140,15 @@ impl CanisterInfo {

let output_root = build_root.join(name);

let mut pull = None;
let mut common_output_idl_path = None;

let type_specific = canister_config.type_specific.clone();
if let CanisterTypeProperties::Pull { id } = type_specific {
common_output_idl_path = Some(get_candid_path_in_project(workspace_root, &id));

pull = Some(PullInfo { id });
}

let args = match &canister_config.args {
Some(args) if !args.is_empty() => canister_config.args.clone(),
Expand Down Expand Up @@ -167,6 +182,8 @@ impl CanisterInfo {
pull_dependencies,
gzip,
init_arg,
pull,
common_output_idl_path,
};

Ok(canister_info)
Expand Down Expand Up @@ -294,6 +311,10 @@ impl CanisterInfo {
///
/// To be separated into service.did and init_args.
pub fn get_output_idl_path(&self) -> Option<PathBuf> {
if let Some(common_output_idl_path) = &self.common_output_idl_path {
return Some(common_output_idl_path.clone());
}

match &self.type_specific {
CanisterTypeProperties::Motoko { .. } => self
.as_info::<MotokoCanisterInfo>()
Expand All @@ -307,9 +328,7 @@ impl CanisterInfo {
CanisterTypeProperties::Rust { .. } => self
.as_info::<RustCanisterInfo>()
.map(|x| x.get_output_idl_path().to_path_buf()),
CanisterTypeProperties::Pull { .. } => self
.as_info::<PullCanisterInfo>()
.map(|x| x.get_output_idl_path().to_path_buf()),
CanisterTypeProperties::Pull { .. } => unreachable!(),
}
.ok()
.or_else(|| self.remote_candid.clone())
Expand Down Expand Up @@ -367,4 +386,18 @@ impl CanisterInfo {
pub fn get_init_arg(&self) -> Option<&str> {
self.init_arg.as_deref()
}

pub fn get_pull_info(&self) -> Option<&PullInfo> {
self.pull.as_ref()
}

pub fn get_common_output_idl_path(&self) -> Option<PathBuf> {
self.common_output_idl_path.as_ref().cloned()
}
}

impl PullInfo {
pub fn get_canister_id(&self) -> &Principal {
&self.id
}
}
52 changes: 0 additions & 52 deletions src/dfx/src/lib/canister_info/pull.rs

This file was deleted.

10 changes: 7 additions & 3 deletions src/dfx/src/lib/models/canister.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::lib::builders::{
custom_download, BuildConfig, BuildOutput, BuilderPool, CanisterBuilder, IdlBuildOutput,
WasmBuildOutput,
custom_download, get_pull_build_output, BuildConfig, BuildOutput, BuilderPool, CanisterBuilder,
IdlBuildOutput, WasmBuildOutput,
};
use crate::lib::canister_info::CanisterInfo;
use crate::lib::environment::Environment;
Expand Down Expand Up @@ -65,7 +65,11 @@ impl Canister {
pool: &CanisterPool,
build_config: &BuildConfig,
) -> DfxResult<&BuildOutput> {
let output = self.builder.build(pool, &self.info, build_config)?;
let output = if let Some(pull_info) = self.info.get_pull_info() {
get_pull_build_output(&self.info, pull_info)
} else {
self.builder.build(pool, &self.info, build_config)
}?;

// Ignore the old output, and return a reference.
let _ = self.output.replace(Some(output));
Expand Down
Loading