Skip to content

Commit

Permalink
fix: dfx schema should not break on invalid dfx.json (#2670)
Browse files Browse the repository at this point in the history
# Description

`dfx schema` should not refuse to work when a broken dfx.json is present.

Fixes #2437, [SDK-667](https://dfinity.atlassian.net/browse/SDK-667)

# How Has This Been Tested?

Added e2e.

# Checklist:

- [x] The title of this PR complies with [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/).
- [x] I have edited the CHANGELOG accordingly.
- [ ] I have made corresponding changes to the documentation.
  • Loading branch information
sesi200 committed Oct 11, 2022
1 parent 9de1104 commit 39dc5f3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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.<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
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
@@ -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
@@ -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
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),
},
};
if let Err(err) = result {
print_error_and_diagnosis(err, error_diagnosis);
Expand Down

0 comments on commit 39dc5f3

Please sign in to comment.