Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions e2e/bats/build.bash
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,10 @@ teardown() {
assert_command_fail dfx build
assert_match 'import error, canister alias "random" not defined'
}

@test "build generates IDs every rebuild" {
assert_command dfx build
cp canisters/e2e_project/_canister.id previous_cid
assert_command dfx build
assert_command_fail diff canisters/e2e_project/_canister.id previous_cid
}
5 changes: 4 additions & 1 deletion src/dfx/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ pub fn exec(env: &dyn Environment, args: &ArgMatches<'_>) -> DfxResult {
let canister_pool = CanisterPool::load(env)?;
// First build.
slog::info!(logger, "Building canisters...");
canister_pool.build_or_fail(BuildConfig::from_config(config.get_config()))?;

// TODO: remove the forcing of generating canister id once we have an update flow.
canister_pool
.build_or_fail(BuildConfig::from_config(config.get_config()).with_generate_id(true))?;

// If there is not a package.json, we don't have a frontend and can quit early.
if !config.get_project_root().join("package.json").exists() || args.is_present("skip-frontend")
Expand Down
9 changes: 9 additions & 0 deletions src/dfx/src/lib/builders/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub trait CanisterBuilder {
pub struct BuildConfig {
profile: Profile,
assets: bool,
pub generate_id: bool,
metadata: BTreeMap<String, serde_json::Value>,
}

Expand All @@ -66,13 +67,21 @@ impl BuildConfig {
BuildConfig {
profile: config.profile.unwrap_or(Profile::Debug),
assets: false,
generate_id: false,
metadata: BTreeMap::new(),
}
}

pub fn with_assets(self, assets: bool) -> Self {
Self { assets, ..self }
}

pub fn with_generate_id(self, generate_id: bool) -> Self {
Self {
generate_id,
..self
}
}
}

pub struct BuilderPool {
Expand Down
56 changes: 38 additions & 18 deletions src/dfx/src/lib/models/canister.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,42 @@ impl CanisterPool {
None
}

pub fn generate_canister_id(&self, force: bool) -> DfxResult {
// Write all canister IDs if needed.
for canister in &self.canisters {
let canister_info = &canister.info;

let canister_id = if force {
None
} else {
canister_info.get_canister_id()
};
let canister_id = match canister_id {
Some(cid) => cid,
None => {
std::fs::create_dir_all(
canister_info
.get_canister_id_path()
.parent()
.expect("Cannot use root."),
)?;
let cid = canister_info.generate_canister_id()?;
std::fs::write(
canister_info.get_canister_id_path(),
cid.clone().into_blob().0,
)
.map_err(DfxError::from)?;

cid
}
};

slog::debug!(self.logger, " {} => {}", canister.get_name(), canister_id);
}

Ok(())
}

fn build_dependencies_graph(&self) -> DfxResult<DiGraph<CanisterId, ()>> {
let mut graph: DiGraph<CanisterId, ()> = DiGraph::new();
let mut id_set: BTreeMap<CanisterId, NodeIndex<u32>> = BTreeMap::new();
Expand Down Expand Up @@ -146,24 +182,8 @@ impl CanisterPool {

/// Build all canisters, returning a vector of results of each builds.
pub fn build(&self, build_config: BuildConfig) -> DfxResult<Vec<DfxResult<BuildOutput>>> {
// Write all canister IDs if needed.
for canister in &self.canisters {
slog::debug!(self.logger, " '{}'", canister.get_name());
let canister_info = &canister.info;

if canister_info.get_canister_id().is_none() {
std::fs::create_dir_all(
canister_info
.get_canister_id_path()
.parent()
.expect("Cannot use root."),
)?;
std::fs::write(
canister_info.get_canister_id_path(),
canister_info.generate_canister_id()?.into_blob().0,
)
.map_err(DfxError::from)?;
}
if build_config.generate_id {
self.generate_canister_id(true)?;
}

let graph = self.build_dependencies_graph()?;
Expand Down