Skip to content

Commit 55f23c2

Browse files
chore: warn when the 'canister_ids.json' file is first generated for persistent networks. (#4058)
* Warn when the 'canister_ids.json' file is first generated for persistent networks. * Add e2e tests. * Update changelog.
1 parent 72f0cde commit 55f23c2

File tree

8 files changed

+51
-8
lines changed

8 files changed

+51
-8
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ Your principal for ICP wallets and decentralized exchanges: ueuar-wxbnk-bdcsr-dn
111111

112112
Add pre-install tasks, which can be defined by the new `pre-install` key for canister objects in `dfx.json` with a command or list of commands.
113113

114+
### chore: Warn when the 'canister_ids.json' file is first generated for persistent networks.
115+
116+
Warn when the 'canister_ids.json' file is first generated for persistent networks.
117+
118+
```
119+
dfx deploy --network ic
120+
...
121+
test_backend canister created on network ic with canister id: j36qm-pqaaa-aaaan-qzqya-cai
122+
WARN: The "/home/sdk/repos/test/canister_ids.json" file has been generated. Please make sure you store it correctly, e.g., submitting it to a GitHub repository.
123+
Building canisters...
124+
...
125+
```
126+
114127
## Dependencies
115128

116129
### Frontend canister

e2e/tests-dfx/network.bash

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ teardown() {
4747
dfx_start
4848

4949
assert_command dfx canister create --all --network local
50+
assert_not_contains "canister_ids.json\" file has been generated. Please make sure you store it correctly"
5051

5152
# canister creates writes to a spinner (stderr), not stdout
5253
assert_command dfx canister id e2e_project_backend --network local
@@ -64,6 +65,7 @@ teardown() {
6465
jq '.local.type="persistent"' "$E2E_NETWORKS_JSON" | sponge "$E2E_NETWORKS_JSON"
6566

6667
assert_command dfx canister create --all --network local
68+
assert_contains "canister_ids.json\" file has been generated. Please make sure you store it correctly"
6769

6870
# canister creates writes to a spinner (stderr), not stdout
6971
assert_command dfx canister id e2e_project_backend --network local

src/dfx-core/src/config/model/canister_id_store.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,37 @@ impl CanisterIdStore {
215215
.map(|(canister_name, _)| canister_name)
216216
}
217217

218-
pub fn save_ids(&self) -> Result<(), SaveIdsError> {
218+
fn warn_for_canister_ids_path(&self) -> bool {
219+
// Only warn when the 'canister_ids.json' file is first generated under the project root directory for persistent networks.
220+
if let NetworkDescriptor {
221+
r#type: NetworkTypeDescriptor::Persistent,
222+
..
223+
} = self.network_descriptor
224+
{
225+
if let Some(path) = self.canister_ids_path.as_ref() {
226+
if !path.exists() {
227+
return true;
228+
}
229+
}
230+
};
231+
232+
false
233+
}
234+
235+
pub fn save_ids(&self, log: &Logger) -> Result<(), SaveIdsError> {
219236
let path = self
220237
.canister_ids_path
221238
.as_ref()
222239
.unwrap_or_else(|| {
223240
// the only callers of this method have already called Environment::get_config_or_anyhow
224241
unreachable!("Must be in a project (call Environment::get_config_or_anyhow()) to save canister ids")
225242
});
243+
let to_warn = self.warn_for_canister_ids_path();
226244
crate::fs::composite::ensure_parent_dir_exists(path)?;
227245
crate::json::save_json_file(path, &self.ids)?;
246+
if to_warn {
247+
warn!(log, "The {:?} file has been generated. Please make sure you store it correctly, e.g., submitting it to a GitHub repository.", path);
248+
}
228249
Ok(())
229250
}
230251

@@ -309,6 +330,7 @@ impl CanisterIdStore {
309330

310331
pub fn add(
311332
&mut self,
333+
log: &Logger,
312334
canister_name: &str,
313335
canister_id: &str,
314336
timestamp: Option<AcquisitionDateTime>,
@@ -327,7 +349,7 @@ impl CanisterIdStore {
327349
.insert(canister_name.to_string(), network_name_to_canister_id);
328350
}
329351
}
330-
self.save_ids()
352+
self.save_ids(log)
331353
.map_err(|source| AddCanisterIdError::SaveIds {
332354
canister_name: canister_name.to_string(),
333355
canister_id: canister_id.to_string(),
@@ -350,11 +372,15 @@ impl CanisterIdStore {
350372
Ok(())
351373
}
352374

353-
pub fn remove(&mut self, canister_name: &str) -> Result<(), RemoveCanisterIdError> {
375+
pub fn remove(
376+
&mut self,
377+
log: &Logger,
378+
canister_name: &str,
379+
) -> Result<(), RemoveCanisterIdError> {
354380
let network_name = &self.network_descriptor.name;
355381
if let Some(network_name_to_canister_id) = self.ids.get_mut(canister_name) {
356382
network_name_to_canister_id.remove(network_name);
357-
self.save_ids()
383+
self.save_ids(log)
358384
.map_err(|e| RemoveCanisterIdError::SaveIds {
359385
canister_name: canister_name.to_string(),
360386
source: e,
@@ -392,7 +418,7 @@ impl CanisterIdStore {
392418

393419
for canister in canisters_to_prune {
394420
warn!(log, "Canister '{}' has timed out.", &canister);
395-
self.remove(&canister)?;
421+
self.remove(log, &canister)?;
396422
}
397423

398424
Ok(())

src/dfx/src/commands/canister/delete.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ async fn delete_canister(
326326
}
327327

328328
if let Some(canister_name) = canister_name_to_delete {
329-
canister_id_store.remove(&canister_name)?;
329+
canister_id_store.remove(log, &canister_name)?;
330330
}
331331

332332
Ok(())

src/dfx/src/lib/named_canister.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub async fn install_ui_canister(
7070
.with_mode(InstallMode::Install)
7171
.await
7272
.context("Install wasm call failed.")?;
73-
id_store.add(UI_CANISTER, &canister_id.to_text(), None)?;
73+
id_store.add(env.get_logger(), UI_CANISTER, &canister_id.to_text(), None)?;
7474
info!(
7575
env.get_logger(),
7676
"The UI canister on the \"{}\" network is \"{}\"",

src/dfx/src/lib/operations/canister/create_canister.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ The command line value will be used.",
188188
non_default_network,
189189
canister_id
190190
);
191-
canister_id_store.add(canister_name, &canister_id, None)?;
191+
canister_id_store.add(log, canister_name, &canister_id, None)?;
192192

193193
Ok(())
194194
}

src/dfx/src/lib/operations/canister/install_canister.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ The command line value will be used.",
224224
)
225225
.await?;
226226
canister_id_store.add(
227+
log,
227228
canister_info.get_name(),
228229
&canister_id.to_string(),
229230
Some(new_timestamp),

src/dfx/src/lib/operations/canister/motoko_playground.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ pub async fn reserve_canister_with_playground(
132132
.context("Failed to reserve canister at the playground.")?;
133133
let reserved_canister = Decode!(&result, CanisterInfo)?;
134134
canister_id_store.add(
135+
log,
135136
canister_name,
136137
&reserved_canister.id.to_string(),
137138
Some(reserved_canister.get_timestamp()?),

0 commit comments

Comments
 (0)