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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,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.<canister name>.candid` of dfx.json, or if that not exists `canisters.<canister name>.remote.candid`, is now used when running `dfx canister call`, even when dfx fails to determine the canister type.
Expand Down
5 changes: 5 additions & 0 deletions e2e/tests-dfx/schema.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 9 additions & 1 deletion src/dfx/src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::lib::environment::Environment;
use crate::lib::error::DfxResult;

use anyhow::bail;
use clap::Subcommand;

mod beta;
Expand Down Expand Up @@ -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),
Expand All @@ -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."),
}
}
3 changes: 1 addition & 2 deletions src/dfx/src/commands/schema.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,7 +25,7 @@ pub struct SchemaOpts {
outfile: Option<PathBuf>,
}

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),
Expand Down
5 changes: 4 additions & 1 deletion src/dfx/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
Comment on lines +204 to +207
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the failure of the schema command can't depend on the failure to construct an Environment, what's the purpose of displaying the earlier error?

Suggested change
Err(e) => match command {
commands::Command::Schema(_) => commands::exec_without_env(command),
_ => Err(e),
},
Err(_) => match command {
commands::Command::Schema(_) => commands::exec_without_env(command),
Err(e) => Err(e),
},

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're misreading the code (meaning I have to clarify it somehow). The match doesn't match on the result of the schema command - it figures out if the command being run can be executed without having an Environment available.

A different way of writing a similar thing would be:

Suggested change
Err(e) => match command {
commands::Command::Schema(_) => commands::exec_without_env(command),
_ => Err(e),
},
Err(original_error) => commands::exec_without_env(command).or_else( || Err(original_error)),
},

};
if let Err(err) = result {
print_error_and_diagnosis(err, error_diagnosis);
Expand Down