diff --git a/CHANGELOG.md b/CHANGELOG.md index 551d4a36bf..4aa10c2d5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -736,6 +736,10 @@ Calls made to retrieve the help output for `canister update-settings` was missin `WARN` and `ERROR` messages are now clearly labelled as such, and the labels are colored accordingly. This is now included when running `dfx canister update-settings -h`. +### fix: `dfx schema` does not require valid dfx.json + +There is no real reason for `dfx schema` to not work when a broken dfx.json is in the current folder - this is actually a very common scenario when `dfx schema` gets used. + ### fix: canister call uses candid file if canister type cannot be determined The candid file specified in the field `canisters..candid` of dfx.json, or if that not exists `canisters..remote.candid`, is now used when running `dfx canister call`, even when dfx fails to determine the canister type. diff --git a/e2e/tests-dfx/schema.bash b/e2e/tests-dfx/schema.bash index 29ec084bdf..b3433050ee 100644 --- a/e2e/tests-dfx/schema.bash +++ b/e2e/tests-dfx/schema.bash @@ -17,3 +17,8 @@ teardown() { assert_command jq type out.json assert_eq '"object"' } + +@test "dfx schema still works with broken dfx.json" { + jq '.broken_key="blahblahblah"' dfx.json | sponge dfx.json + assert_command dfx schema +} diff --git a/src/dfx/src/commands/mod.rs b/src/dfx/src/commands/mod.rs index 61da68d549..a23679387d 100644 --- a/src/dfx/src/commands/mod.rs +++ b/src/dfx/src/commands/mod.rs @@ -1,6 +1,7 @@ use crate::lib::environment::Environment; use crate::lib::error::DfxResult; +use anyhow::bail; use clap::Subcommand; mod beta; @@ -84,7 +85,7 @@ pub fn exec(env: &dyn Environment, cmd: Command) -> DfxResult { Command::Quickstart => quickstart::exec(env), Command::Remote(v) => remote::exec(env, v), Command::Replica(v) => replica::exec(env, v), - Command::Schema(v) => schema::exec(env, v), + Command::Schema(v) => schema::exec(v), Command::Sns(v) => sns::exec(env, v), Command::Start(v) => start::exec(env, v), Command::Stop(v) => stop::exec(env, v), @@ -93,3 +94,10 @@ pub fn exec(env: &dyn Environment, cmd: Command) -> DfxResult { Command::Wallet(v) => wallet::exec(env, v), } } + +pub fn exec_without_env(cmd: Command) -> DfxResult { + match cmd { + Command::Schema(v) => schema::exec(v), + _ => bail!("Cannot execute this command without environment."), + } +} diff --git a/src/dfx/src/commands/schema.rs b/src/dfx/src/commands/schema.rs index b135a550ee..8998ad931d 100644 --- a/src/dfx/src/commands/schema.rs +++ b/src/dfx/src/commands/schema.rs @@ -1,7 +1,6 @@ use std::path::PathBuf; use crate::config::dfinity::{ConfigInterface, TopLevelConfigNetworks}; -use crate::lib::environment::Environment; use crate::lib::error::DfxResult; use anyhow::Context; @@ -26,7 +25,7 @@ pub struct SchemaOpts { outfile: Option, } -pub fn exec(_env: &dyn Environment, opts: SchemaOpts) -> DfxResult { +pub fn exec(opts: SchemaOpts) -> DfxResult { let schema = match opts.r#for { Some(ForFile::Networks) => schema_for!(TopLevelConfigNetworks), _ => schema_for!(ConfigInterface), diff --git a/src/dfx/src/main.rs b/src/dfx/src/main.rs index fc0767411a..9b973cf565 100644 --- a/src/dfx/src/main.rs +++ b/src/dfx/src/main.rs @@ -201,7 +201,10 @@ fn main() { Err(e) => Err(e), } } - Err(e) => Err(e), + Err(e) => match command { + commands::Command::Schema(_) => commands::exec_without_env(command), + _ => Err(e), + }, }; if let Err(err) = result { print_error_and_diagnosis(err, error_diagnosis);