diff --git a/dfx/src/commands/build.rs b/dfx/src/commands/build.rs index 1147dbfb06..c275554d98 100644 --- a/dfx/src/commands/build.rs +++ b/dfx/src/commands/build.rs @@ -1,6 +1,7 @@ use crate::config::dfinity::{Config, Profile}; use crate::lib::canister_info::CanisterInfo; use crate::lib::env::{BinaryResolverEnv, ProjectConfigEnv}; +use crate::lib::error::DfxError::BuildError; use crate::lib::error::{BuildErrorKind, DfxError, DfxResult}; use crate::lib::message::UserMessage; use clap::{App, Arg, ArgMatches, SubCommand}; @@ -106,7 +107,11 @@ fn build_file(env: &T, config: &Config, name: &str) -> DfxResult where T: BinaryResolverEnv, { - let canister_info = CanisterInfo::load(config, name)?; + let canister_info = CanisterInfo::load(config, name).map_err(|_| { + BuildError(BuildErrorKind::CanisterNameIsNotInConfigError( + name.to_owned(), + )) + })?; let config = config.get_config(); let profile = config.profile.clone(); let input_path = canister_info.get_main_path(); @@ -154,7 +159,7 @@ where Ok(()) } -pub fn exec(env: &T, _args: &ArgMatches<'_>) -> DfxResult +pub fn exec(env: &T, args: &ArgMatches<'_>) -> DfxResult where T: BinaryResolverEnv + ProjectConfigEnv, { @@ -163,7 +168,11 @@ where .get_config() .ok_or(DfxError::CommandMustBeRunInAProject)?; - if let Some(canisters) = &config.get_config().canisters { + // Get the canister name (if any). + if let Some(canister_name) = args.value_of("canister") { + println!("Building {}...", canister_name); + build_file(env, &config, &canister_name)?; + } else if let Some(canisters) = &config.get_config().canisters { for k in canisters.keys() { println!("Building {}...", k); build_file(env, &config, &k)?; diff --git a/dfx/src/lib/error/build.rs b/dfx/src/lib/error/build.rs index 757ddb63e9..3a8d9dcde6 100644 --- a/dfx/src/lib/error/build.rs +++ b/dfx/src/lib/error/build.rs @@ -17,6 +17,9 @@ pub enum BuildErrorKind { /// An error happened while compiling WAT to WASM. WatCompileError(wabt::Error), + + /// Could not find the canister to build in the config. + CanisterNameIsNotInConfigError(String), } impl fmt::Display for BuildErrorKind { @@ -39,6 +42,10 @@ impl fmt::Display for BuildErrorKind { WatCompileError(e) => { f.write_fmt(format_args!("Error while compiling WAT to WASM: {}", e)) } + CanisterNameIsNotInConfigError(name) => f.write_fmt(format_args!( + r#"Could not find the canister named "{}" in the dfx configuration."#, + name, + )), } } } diff --git a/e2e/build.bash b/e2e/build.bash index 1080d10c52..7654cb4c16 100644 --- a/e2e/build.bash +++ b/e2e/build.bash @@ -31,3 +31,11 @@ teardown() { assert_command dfx build [[ -f canisters/hello/_canister.id ]] } + +@test "build can take a single argument" { + assert_command dfx build hello + assert_match "Building hello..." + + assert_command_fail dfx build unknown_canister + assert_match "Could not find.*unknown_canister.*" +} diff --git a/e2e/utils/assertions.bash b/e2e/utils/assertions.bash index 88f25fc14e..cba2dd3f68 100644 --- a/e2e/utils/assertions.bash +++ b/e2e/utils/assertions.bash @@ -40,7 +40,7 @@ assert_match() { else text="$2" fi - [[ "$text" =~ "$regex" ]] || \ + [[ "$text" =~ $regex ]] || \ (batslib_print_kv_single_or_multi 10 "regex" "$regex" "actual" "$text" \ | batslib_decorate "output does not match" \ | fail)