Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added check to forc_abi to fail on non-contracts #1105

Merged
merged 30 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
de7977a
Produce an error rather than panicking on unsupported dependency type
mitchmindtree Mar 12, 2022
ae7f76f
Merge branch 'mitchmindtree/semver-dep-error' of github.com:FuelLabs/…
eureka-cpu Mar 12, 2022
49cca46
Merge branch 'master' of github.com:FuelLabs/sway
eureka-cpu Mar 12, 2022
2a85f9e
Merge branch 'master' of github.com:FuelLabs/sway
eureka-cpu Mar 14, 2022
839f2c2
Merge branch 'master' of github.com:FuelLabs/sway
eureka-cpu Mar 14, 2022
5a79849
Merge branch 'master' of github.com:FuelLabs/sway
eureka-cpu Mar 16, 2022
934debe
Merge branch 'master' of github.com:FuelLabs/sway
eureka-cpu Mar 18, 2022
c1f2001
Merge branch 'master' of github.com:FuelLabs/sway
eureka-cpu Mar 25, 2022
ba4c8b1
Merge branch 'master' of github.com:FuelLabs/sway
eureka-cpu Mar 28, 2022
c6a1163
Merge branch 'master' of github.com:FuelLabs/sway
eureka-cpu Mar 28, 2022
c18a6f2
Merge branch 'master' of github.com:FuelLabs/sway
eureka-cpu Mar 29, 2022
635f2b2
wip check forc non-contracts in forc abi
eureka-cpu Mar 30, 2022
cd5559b
small fixes
eureka-cpu Mar 30, 2022
168e850
fixed fmt display for CliError & fixed test in harness
eureka-cpu Mar 30, 2022
49223e7
cleared instances of CliError in favor of anyhow
eureka-cpu Mar 30, 2022
4f71308
mod test for json abi result now gives the actual error message
eureka-cpu Mar 31, 2022
96c8cbe
separated projects with abi
eureka-cpu Mar 31, 2022
41733c1
resolved conflict
eureka-cpu Mar 31, 2022
9d6e741
reduce dup code
eureka-cpu Mar 31, 2022
10a41a4
rmv oracle files from scripts
eureka-cpu Apr 1, 2022
f992293
fix some docstrings
eureka-cpu Apr 1, 2022
8a2a199
changed cli_error function name & condensed std use statement
eureka-cpu Apr 1, 2022
6c5b0ea
updated function names, added is_ok
eureka-cpu Apr 1, 2022
c4cbd3a
mvd functions into forc-pkg::pkg to avoid circular dep, updated funct…
eureka-cpu Apr 2, 2022
81f6f7a
replaced Err with bail
eureka-cpu Apr 2, 2022
5dc6f4e
removed implicit manifest return
eureka-cpu Apr 2, 2022
bbfd123
changed manifest var to self
eureka-cpu Apr 2, 2022
b3c1a6f
removed whitespace at the end of forc-abi-json
eureka-cpu Apr 2, 2022
bb35cc0
fmt
eureka-cpu Apr 2, 2022
2572b9f
merge conflict
eureka-cpu Apr 4, 2022
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
2 changes: 1 addition & 1 deletion forc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(dead_code)]
mod cli;
mod ops;
mod utils;
pub mod utils;

#[cfg(feature = "test")]
pub mod test {
Expand Down
94 changes: 72 additions & 22 deletions forc/src/ops/forc_abi_json.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,80 @@
use crate::cli::{BuildCommand, JsonAbiCommand};
use crate::utils::cli_error::CliError;
use anyhow::Result;
use forc_pkg::Manifest;
use forc_util::find_manifest_dir;
use serde_json::{json, Value};
use std::fs::File;
use std::path::PathBuf;
use sway_core::{parse, TreeType};
use sway_utils::constants::*;

pub fn build(command: JsonAbiCommand) -> Result<Value> {
let build_command = BuildCommand {
path: command.path,
offline_mode: command.offline_mode,
silent_mode: command.silent_mode,
minify_json_abi: command.minify,
..Default::default()
};
let compiled = crate::ops::forc_build::build(build_command)?;
let json_abi = json!(compiled.json_abi);
if let Some(outfile) = command.json_outfile {
let file = File::create(outfile).map_err(|e| e)?;
let res = if command.minify {
serde_json::to_writer(&file, &json_abi)
} else {
serde_json::to_writer_pretty(&file, &json_abi)
};
res.map_err(|e| e)?;
} else if command.minify {
println!("{}", json_abi);
pub fn build(command: JsonAbiCommand) -> Result<Value, CliError> {
let curr_dir = if let Some(ref path) = command.path {
PathBuf::from(path)
} else {
println!("{:#}", json_abi);
std::env::current_dir()?
};

// Parse manifest and return error if non-contract
// Note that this same code is in `forc_deploy` & `forc_run` and may be more useful as a helper function.
// Its only difference is in what functionality it performs once it finds a `Contract`.
match find_manifest_dir(&curr_dir) {
Some(manifest_dir) => {
let manifest = Manifest::from_dir(&manifest_dir)?;
let project_name = &manifest.project.name;
let entry_string = manifest.entry_string(&manifest_dir)?;

let parsed_result = parse(entry_string, None);
match parsed_result.value {
Some(parse_tree) => match parse_tree.tree_type {
TreeType::Contract => {
let build_command = BuildCommand {
path: command.path,
offline_mode: command.offline_mode,
silent_mode: command.silent_mode,
minify_json_abi: command.minify,
..Default::default()
};

eureka-cpu marked this conversation as resolved.
Show resolved Hide resolved
let compiled = crate::ops::forc_build::build(build_command)?;
let json_abi = json!(compiled.json_abi);

if let Some(outfile) = command.json_outfile {
let file = File::create(outfile).map_err(|e| e)?;
let res = if command.minify {
serde_json::to_writer(&file, &json_abi)
} else {
serde_json::to_writer_pretty(&file, &json_abi)
};
res.map_err(|e| e)?;
} else if command.minify {
println!("{}", json_abi);
} else {
println!("{:#}", json_abi);
}

Ok(json_abi)
}
TreeType::Script => Err(CliError::wrong_sway_type(
project_name,
SWAY_CONTRACT,
SWAY_SCRIPT,
)),
TreeType::Predicate => Err(CliError::wrong_sway_type(
project_name,
SWAY_CONTRACT,
SWAY_PREDICATE,
)),
TreeType::Library { .. } => Err(CliError::wrong_sway_type(
project_name,
SWAY_CONTRACT,
SWAY_LIBRARY,
)),
},
None => Err(CliError::parsing_failed(project_name, parsed_result.errors)),
}
}
None => Err(CliError::manifest_file_missing(curr_dir)),
}
eureka-cpu marked this conversation as resolved.
Show resolved Hide resolved
Ok(json_abi)
}
12 changes: 11 additions & 1 deletion forc/src/utils/cli_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl CliError {

impl fmt::Display for CliError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}", self)
write!(f, "{:?}", self)
eureka-cpu marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -89,3 +89,13 @@ impl From<anyhow::Error> for CliError {
}
}
}

impl From<serde_json::Error> for CliError {
fn from(e: serde_json::Error) -> Self {
CliError {
message: e.to_string(),
}
}
}

impl std::error::Error for CliError {}
5 changes: 3 additions & 2 deletions test/src/e2e_vm_tests/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use forc::test::{
forc_abi_json, forc_build, forc_deploy, forc_run, BuildCommand, DeployCommand, JsonAbiCommand,
RunCommand,
};
use forc::utils::cli_error::CliError;
use fuel_tx::Transaction;
use fuel_vm::interpreter::Interpreter;
use fuel_vm::prelude::*;
Expand Down Expand Up @@ -121,7 +122,7 @@ pub(crate) fn compile_to_bytes(file_name: &str) -> Result<Vec<u8>> {
}

pub(crate) fn test_json_abi(file_name: &str) -> Result<()> {
let _script = compile_to_json_abi(file_name)?;
let _compiled_res = compile_to_json_abi(file_name)?;
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let oracle_path = format!(
"{}/src/e2e_vm_tests/test_programs/{}/{}",
Expand All @@ -147,7 +148,7 @@ pub(crate) fn test_json_abi(file_name: &str) -> Result<()> {
Ok(())
}

fn compile_to_json_abi(file_name: &str) -> Result<Value> {
fn compile_to_json_abi(file_name: &str) -> Result<Value, CliError> {
println!(" ABI gen {}", file_name);
let manifest_dir = env!("CARGO_MANIFEST_DIR");
forc_abi_json::build(JsonAbiCommand {
Expand Down