Skip to content

Commit 349d547

Browse files
author
Eric Swanson
authored
refactor: remove output_idl_path from <type>CanisterInfo types (#3828)
1 parent b5e4391 commit 349d547

File tree

13 files changed

+88
-152
lines changed

13 files changed

+88
-152
lines changed

src/dfx/src/lib/builders/custom.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ struct CustomBuilderExtra {
2626
wasm: PathBuf,
2727
/// Where to download the candid from
2828
input_candid_url: Option<Url>,
29-
/// Where the candid output will be located.
30-
candid: PathBuf,
3129
/// A command to run to build this canister. This is optional if the canister
3230
/// only needs to exist.
3331
build: Vec<String>,
@@ -51,15 +49,13 @@ impl CustomBuilderExtra {
5149
let input_wasm_url = info.get_input_wasm_url().to_owned();
5250
let wasm = info.get_output_wasm_path().to_owned();
5351
let input_candid_url = info.get_input_candid_url().to_owned();
54-
let candid = info.get_output_idl_path().to_owned();
5552
let build = info.get_build_tasks().to_owned();
5653

5754
Ok(CustomBuilderExtra {
5855
dependencies,
5956
input_wasm_url,
6057
wasm,
6158
input_candid_url,
62-
candid,
6359
build,
6460
})
6561
}
@@ -103,7 +99,6 @@ impl CanisterBuilder for CustomBuilder {
10399
) -> DfxResult<BuildOutput> {
104100
let CustomBuilderExtra {
105101
input_candid_url: _,
106-
candid,
107102
input_wasm_url: _,
108103
wasm,
109104
build,
@@ -134,26 +129,25 @@ impl CanisterBuilder for CustomBuilder {
134129
Ok(BuildOutput {
135130
canister_id,
136131
wasm: WasmBuildOutput::File(wasm),
137-
idl: IdlBuildOutput::File(candid),
132+
idl: IdlBuildOutput::File(info.get_output_idl_path().to_path_buf()),
138133
})
139134
}
140135

141136
fn get_candid_path(
142137
&self,
143-
pool: &CanisterPool,
138+
_pool: &CanisterPool,
144139
info: &CanisterInfo,
145140
_config: &BuildConfig,
146141
) -> DfxResult<PathBuf> {
147142
// get the path to candid file
148-
let CustomBuilderExtra { candid, .. } = CustomBuilderExtra::try_from(info, pool)?;
149-
Ok(candid)
143+
Ok(info.get_output_idl_path().to_path_buf())
150144
}
151145
}
152146

153147
pub async fn custom_download(info: &CanisterInfo, pool: &CanisterPool) -> DfxResult {
148+
let candid = info.get_output_idl_path();
154149
let CustomBuilderExtra {
155150
input_candid_url,
156-
candid,
157151
input_wasm_url,
158152
wasm,
159153
build: _,
@@ -164,7 +158,7 @@ pub async fn custom_download(info: &CanisterInfo, pool: &CanisterPool) -> DfxRes
164158
download_file_to_path(&url, &wasm).await?;
165159
}
166160
if let Some(url) = input_candid_url {
167-
download_file_to_path(&url, &candid).await?;
161+
download_file_to_path(&url, candid).await?;
168162
}
169163

170164
Ok(())

src/dfx/src/lib/builders/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,10 @@ pub fn get_and_write_environment_variables<'a>(
433433
if let Ok(id) = info.get_canister_id() {
434434
vars.push((Borrowed("CANISTER_ID"), Owned(format!("{}", id).into())));
435435
}
436-
if let Some(path) = info.get_output_idl_path() {
437-
vars.push((Borrowed("CANISTER_CANDID_PATH"), Owned(path.into())))
438-
}
436+
vars.push((
437+
Borrowed("CANISTER_CANDID_PATH"),
438+
Owned(info.get_output_idl_path().into()),
439+
));
439440

440441
if let Some(write_path) = write_path {
441442
write_environment_variables(&vars, write_path)?;

src/dfx/src/lib/builders/motoko.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl CanisterBuilder for MotokoBuilder {
187187
.get_canister_id()
188188
.expect("Could not find canister ID."),
189189
wasm: WasmBuildOutput::File(motoko_info.get_output_wasm_path().to_path_buf()),
190-
idl: IdlBuildOutput::File(motoko_info.get_output_idl_path().to_path_buf()),
190+
idl: IdlBuildOutput::File(canister_info.get_output_idl_path().to_path_buf()),
191191
})
192192
}
193193

@@ -198,9 +198,7 @@ impl CanisterBuilder for MotokoBuilder {
198198
_config: &BuildConfig,
199199
) -> DfxResult<PathBuf> {
200200
// get the path to candid file from dfx build
201-
let motoko_info = info.as_info::<MotokoCanisterInfo>()?;
202-
let idl_from_build = motoko_info.get_output_idl_path().to_path_buf();
203-
Ok(idl_from_build)
201+
Ok(info.get_output_idl_path().to_path_buf())
204202
}
205203
}
206204

src/dfx/src/lib/builders/pull.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl CanisterBuilder for PullBuilder {
4848
canister_id: *pull_info.get_canister_id(),
4949
// It's impossible to know if the downloaded wasm is gzip or not with only the info in `dfx.json`.
5050
wasm: WasmBuildOutput::None,
51-
idl: IdlBuildOutput::File(pull_info.get_output_idl_path().to_path_buf()),
51+
idl: IdlBuildOutput::File(canister_info.get_output_idl_path().to_path_buf()),
5252
})
5353
}
5454

@@ -58,8 +58,6 @@ impl CanisterBuilder for PullBuilder {
5858
info: &CanisterInfo,
5959
_config: &BuildConfig,
6060
) -> DfxResult<PathBuf> {
61-
let pull_info = info.as_info::<PullCanisterInfo>()?;
62-
let output_idl_path = pull_info.get_output_idl_path();
63-
Ok(output_idl_path.to_path_buf())
61+
Ok(info.get_output_idl_path().to_path_buf())
6462
}
6563
}

src/dfx/src/lib/builders/rust.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ impl CanisterBuilder for RustBuilder {
102102
Ok(BuildOutput {
103103
canister_id,
104104
wasm: WasmBuildOutput::File(rust_info.get_output_wasm_path().to_path_buf()),
105-
idl: IdlBuildOutput::File(rust_info.get_output_idl_path().to_path_buf()),
105+
idl: IdlBuildOutput::File(canister_info.get_output_idl_path().to_path_buf()),
106106
})
107107
}
108108

@@ -112,8 +112,6 @@ impl CanisterBuilder for RustBuilder {
112112
info: &CanisterInfo,
113113
_config: &BuildConfig,
114114
) -> DfxResult<PathBuf> {
115-
let rust_info = info.as_info::<RustCanisterInfo>()?;
116-
let output_idl_path = rust_info.get_output_idl_path();
117-
Ok(output_idl_path.to_path_buf())
115+
Ok(info.get_output_idl_path().to_path_buf())
118116
}
119117
}

src/dfx/src/lib/canister_info.rs

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,14 @@ use dfx_core::network::provider::get_network_context;
1515
use dfx_core::util;
1616
use fn_error_context::context;
1717
use std::path::{Path, PathBuf};
18+
use url::Url;
1819

1920
pub mod assets;
2021
pub mod custom;
2122
pub mod motoko;
2223
pub mod pull;
2324
pub mod rust;
24-
use self::pull::PullCanisterInfo;
25-
use assets::AssetsCanisterInfo;
26-
use custom::CustomCanisterInfo;
27-
use motoko::MotokoCanisterInfo;
28-
use rust::RustCanisterInfo;
25+
use crate::lib::deps::get_candid_path_in_project;
2926

3027
pub trait CanisterInfoFactory {
3128
fn create(info: &CanisterInfo) -> DfxResult<Self>
@@ -63,6 +60,7 @@ pub struct CanisterInfo {
6360
gzip: bool,
6461
init_arg: Option<String>,
6562
init_arg_file: Option<String>,
63+
output_idl_path: PathBuf,
6664
}
6765

6866
impl CanisterInfo {
@@ -137,6 +135,32 @@ impl CanisterInfo {
137135

138136
let output_root = build_root.join(name);
139137

138+
let output_idl_path: PathBuf = if let (Some(_id), Some(candid)) =
139+
(&remote_id, &remote_candid)
140+
{
141+
workspace_root.join(candid)
142+
} else {
143+
match &canister_config.type_specific {
144+
CanisterTypeProperties::Rust { package: _, candid } => workspace_root.join(candid),
145+
CanisterTypeProperties::Assets { .. } => output_root.join("assetstorage.did"),
146+
CanisterTypeProperties::Custom {
147+
wasm: _,
148+
candid,
149+
build: _,
150+
} => {
151+
if Url::parse(candid).is_ok() {
152+
output_root.join(name).with_extension("did")
153+
} else {
154+
workspace_root.join(candid)
155+
}
156+
}
157+
CanisterTypeProperties::Motoko => output_root.join(name).with_extension("did"),
158+
CanisterTypeProperties::Pull { id } => {
159+
get_candid_path_in_project(workspace_root, id)
160+
}
161+
}
162+
};
163+
140164
let type_specific = canister_config.type_specific.clone();
141165

142166
let args = match &canister_config.args {
@@ -174,6 +198,7 @@ impl CanisterInfo {
174198
gzip,
175199
init_arg,
176200
init_arg_file,
201+
output_idl_path,
177202
};
178203

179204
Ok(canister_info)
@@ -300,26 +325,8 @@ impl CanisterInfo {
300325
/// Path to the candid file from canister builder which should contain init types.
301326
///
302327
/// To be separated into service.did and init_args.
303-
pub fn get_output_idl_path(&self) -> Option<PathBuf> {
304-
match &self.type_specific {
305-
CanisterTypeProperties::Motoko { .. } => self
306-
.as_info::<MotokoCanisterInfo>()
307-
.map(|x| x.get_output_idl_path().to_path_buf()),
308-
CanisterTypeProperties::Custom { .. } => self
309-
.as_info::<CustomCanisterInfo>()
310-
.map(|x| x.get_output_idl_path().to_path_buf()),
311-
CanisterTypeProperties::Assets { .. } => self
312-
.as_info::<AssetsCanisterInfo>()
313-
.map(|x| x.get_output_idl_path().to_path_buf()),
314-
CanisterTypeProperties::Rust { .. } => self
315-
.as_info::<RustCanisterInfo>()
316-
.map(|x| x.get_output_idl_path().to_path_buf()),
317-
CanisterTypeProperties::Pull { .. } => self
318-
.as_info::<PullCanisterInfo>()
319-
.map(|x| x.get_output_idl_path().to_path_buf()),
320-
}
321-
.ok()
322-
.or_else(|| self.remote_candid.clone())
328+
pub fn get_output_idl_path(&self) -> &Path {
329+
self.output_idl_path.as_path()
323330
}
324331

325332
#[context("Failed to create <Type>CanisterInfo for canister '{}'.", self.name, )]

src/dfx/src/lib/canister_info/assets.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ pub struct AssetsCanisterInfo {
1010
source_paths: Vec<PathBuf>,
1111

1212
output_wasm_path: PathBuf,
13-
output_idl_path: PathBuf,
1413
build: Vec<String>,
1514
workspace: Option<String>,
1615
}
@@ -22,9 +21,6 @@ impl AssetsCanisterInfo {
2221
.map(|sp| self.input_root.join(sp))
2322
.collect::<_>()
2423
}
25-
pub fn get_output_idl_path(&self) -> &Path {
26-
self.output_idl_path.as_path()
27-
}
2824
pub fn get_build_tasks(&self) -> &[String] {
2925
&self.build
3026
}
@@ -76,13 +72,11 @@ impl CanisterInfoFactory for AssetsCanisterInfo {
7672
let output_root = info.get_output_root();
7773

7874
let output_wasm_path = output_root.join(Path::new("assetstorage.wasm.gz"));
79-
let output_idl_path = output_wasm_path.with_extension("").with_extension("did");
8075

8176
Ok(AssetsCanisterInfo {
8277
input_root,
8378
source_paths,
8479
output_wasm_path,
85-
output_idl_path,
8680
build,
8781
workspace,
8882
})

src/dfx/src/lib/canister_info/custom.rs

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pub struct CustomCanisterInfo {
1414
input_wasm_url: Option<Url>,
1515
output_wasm_path: PathBuf,
1616
input_candid_url: Option<Url>,
17-
output_idl_path: PathBuf,
1817
build: Vec<String>,
1918
}
2019

@@ -28,9 +27,6 @@ impl CustomCanisterInfo {
2827
pub fn get_input_candid_url(&self) -> &Option<Url> {
2928
&self.input_candid_url
3029
}
31-
pub fn get_output_idl_path(&self) -> &Path {
32-
self.output_idl_path.as_path()
33-
}
3430
pub fn get_build_tasks(&self) -> &[String] {
3531
&self.build
3632
}
@@ -76,24 +72,18 @@ impl CanisterInfoFactory for CustomCanisterInfo {
7672
let output_wasm_path = workspace_root.join(wasm);
7773
(None, output_wasm_path)
7874
};
79-
let (input_candid_url, output_idl_path) =
80-
if let Some(remote_candid) = info.get_remote_candid_if_remote() {
81-
(None, workspace_root.join(remote_candid))
82-
} else if let Ok(input_candid_url) = Url::parse(&candid) {
83-
let output_candid_path = info
84-
.get_output_root()
85-
.join(info.get_name())
86-
.with_extension("did");
87-
(Some(input_candid_url), output_candid_path)
88-
} else {
89-
(None, workspace_root.join(candid))
90-
};
75+
let input_candid_url = if info.get_remote_candid_if_remote().is_some() {
76+
None
77+
} else if let Ok(input_candid_url) = Url::parse(&candid) {
78+
Some(input_candid_url)
79+
} else {
80+
None
81+
};
9182

9283
Ok(Self {
9384
input_wasm_url,
9485
output_wasm_path,
9586
input_candid_url,
96-
output_idl_path,
9787
build,
9888
})
9989
}

src/dfx/src/lib/canister_info/motoko.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ pub struct MotokoCanisterInfo {
99
output_root: PathBuf,
1010

1111
output_wasm_path: PathBuf,
12-
output_idl_path: PathBuf,
1312
output_stable_path: PathBuf,
1413
output_did_js_path: PathBuf,
1514
output_canister_js_path: PathBuf,
@@ -26,9 +25,6 @@ impl MotokoCanisterInfo {
2625
pub fn get_output_wasm_path(&self) -> &Path {
2726
self.output_wasm_path.as_path()
2827
}
29-
pub fn get_output_idl_path(&self) -> &Path {
30-
self.output_idl_path.as_path()
31-
}
3228
pub fn get_output_stable_path(&self) -> &Path {
3329
self.output_stable_path.as_path()
3430
}
@@ -67,11 +63,6 @@ impl CanisterInfoFactory for MotokoCanisterInfo {
6763
let input_path = workspace_root.join(main_path);
6864
let output_root = info.get_output_root().to_path_buf();
6965
let output_wasm_path = output_root.join(name).with_extension("wasm");
70-
let output_idl_path = if let Some(remote_candid) = info.get_remote_candid_if_remote() {
71-
workspace_root.join(remote_candid)
72-
} else {
73-
output_wasm_path.with_extension("did")
74-
};
7566
let output_stable_path = output_wasm_path.with_extension("most");
7667
let output_did_js_path = output_wasm_path.with_extension("did.js");
7768
let output_canister_js_path = output_wasm_path.with_extension("js");
@@ -81,7 +72,6 @@ impl CanisterInfoFactory for MotokoCanisterInfo {
8172
input_path,
8273
output_root,
8374
output_wasm_path,
84-
output_idl_path,
8575
output_stable_path,
8676
output_did_js_path,
8777
output_canister_js_path,

0 commit comments

Comments
 (0)