Skip to content

feat: route all prints through common::shell #5746

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

Closed
wants to merge 17 commits into from
Closed
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
msrv = "1.72"

disallowed-macros = [
# See `foundry_common::shell`
{ path = "std::print", reason = "use `sh_print` or similar macros instead" },
{ path = "std::eprint", reason = "use `sh_eprint` or similar macros instead" },
{ path = "std::println", reason = "use `sh_println` or similar macros instead" },
{ path = "std::eprintln", reason = "use `sh_eprintln` or similar macros instead" },
]
2 changes: 2 additions & 0 deletions crates/abi/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::disallowed_macros)]

use ethers_contract_abigen::MultiAbigen;

/// Includes a JSON ABI as a string literal.
Expand Down
2 changes: 2 additions & 0 deletions crates/anvil/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::disallowed_macros)]

/// Various Ethereum types
pub mod eth;

Expand Down
2 changes: 2 additions & 0 deletions crates/anvil/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::disallowed_macros)]

/// JSON-RPC request bindings
pub mod request;

Expand Down
1 change: 1 addition & 0 deletions crates/anvil/server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Bootstrap [axum] RPC servers

#![allow(clippy::disallowed_macros)]
#![deny(missing_docs, unsafe_code, unused_crate_dependencies)]

use anvil_rpc::{
Expand Down
2 changes: 2 additions & 0 deletions crates/anvil/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::disallowed_macros)]

use crate::{
eth::{
backend::{info::StorageInfo, mem},
Expand Down
2 changes: 2 additions & 0 deletions crates/anvil/tests/it/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(clippy::disallowed_macros)]

mod abi;
mod anvil;
mod anvil_api;
Expand Down
5 changes: 4 additions & 1 deletion crates/binder/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//! Generate [ethers-rs]("https://github.com/gakonst/ethers-rs") bindings for solidity projects in a build script.
//! Generate [ethers-rs](https://github.com/gakonst/ethers-rs) bindings for solidity projects in a
//! build script.

#![allow(clippy::disallowed_macros)]

use crate::utils::{GitReference, GitRemote};
use ethers_contract::MultiAbigen;
Expand Down
5 changes: 2 additions & 3 deletions crates/binder/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,12 @@ impl Retry {

pub fn r#try<T>(&mut self, f: impl FnOnce() -> eyre::Result<T>) -> eyre::Result<Option<T>> {
match f() {
Err(ref e) if maybe_spurious(e) && self.remaining > 0 => {
let msg = format!(
Err(e) if maybe_spurious(&e) && self.remaining > 0 => {
println!(
"spurious network error ({} tries remaining): {}",
self.remaining,
e.root_cause(),
);
println!("{msg}");
self.remaining -= 1;
Ok(None)
}
Expand Down
1 change: 1 addition & 0 deletions crates/cast/benches/vanity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{hint::black_box, time::Duration};

#[path = "../bin/cmd/wallet/mod.rs"]
#[allow(unused)]
#[allow(clippy::all)]
mod wallet;
use wallet::vanity::*;

Expand Down
1 change: 0 additions & 1 deletion crates/cast/bin/cmd/access_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub struct AccessListArgs {
#[clap(
long,
value_name = "DATA",
value_parser = foundry_common::clap_helpers::strip_0x_prefix,
conflicts_with_all = &["sig", "args"]
)]
data: Option<String>,
Expand Down
1 change: 0 additions & 1 deletion crates/cast/bin/cmd/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ pub struct CallArgs {
/// Data for the transaction.
#[clap(
long,
value_parser = foundry_common::clap_helpers::strip_0x_prefix,
conflicts_with_all = &["sig", "args"]
)]
data: Option<String>,
Expand Down
3 changes: 1 addition & 2 deletions crates/cast/bin/cmd/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use foundry_cli::{
opts::{EthereumOpts, TransactionOpts},
utils,
};
use foundry_common::cli_warn;
use foundry_config::{Chain, Config};
use std::str::FromStr;

Expand Down Expand Up @@ -119,7 +118,7 @@ impl SendTxArgs {
// switch chain if current chain id is not the same as the one specified in the
// config
if config_chain_id != current_chain_id {
cli_warn!("Switching to chain {}", config_chain);
sh_warn!("Switching to chain {config_chain}")?;
provider
.request(
"wallet_switchEthereumChain",
Expand Down
15 changes: 7 additions & 8 deletions crates/cast/bin/cmd/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use foundry_cli::{
};
use foundry_common::{
abi::find_source,
compile::{compile, etherscan_project, suppress_compile},
compile::{etherscan_project, ProjectCompiler},
RetryProvider,
};
use foundry_config::{
Expand Down Expand Up @@ -103,7 +103,7 @@ impl StorageArgs {
if project.paths.has_input_files() {
// Find in artifacts and pretty print
add_storage_layout_output(&mut project);
let out = compile(&project, false, false)?;
let out = ProjectCompiler::new().compile(&project)?;
let match_code = |artifact: &ConfigurableContractArtifact| -> Option<bool> {
let bytes =
artifact.deployed_bytecode.as_ref()?.bytecode.as_ref()?.object.as_bytes()?;
Expand All @@ -118,7 +118,7 @@ impl StorageArgs {

// Not a forge project or artifact not found
// Get code from Etherscan
eprintln!("No matching artifacts found, fetching source code from Etherscan...");
sh_note!("No matching artifacts found, fetching source code from Etherscan...")?;

let chain = utils::get_chain(config.chain_id, &provider).await?;
let api_key = config.get_etherscan_api_key(Some(chain)).unwrap_or_default();
Expand All @@ -141,7 +141,7 @@ impl StorageArgs {
project.auto_detect = auto_detect;

// Compile
let mut out = suppress_compile(&project)?;
let mut out = ProjectCompiler::new().quiet(true).compile(&project)?;
let artifact = {
let (_, mut artifact) = out
.artifacts()
Expand All @@ -150,11 +150,11 @@ impl StorageArgs {

if is_storage_layout_empty(&artifact.storage_layout) && auto_detect {
// try recompiling with the minimum version
eprintln!("The requested contract was compiled with {version} while the minimum version for storage layouts is {MIN_SOLC} and as a result the output may be empty.");
sh_warn!("The requested contract was compiled with {version} while the minimum version for storage layouts is {MIN_SOLC} and as a result the output may be empty.")?;
let solc = Solc::find_or_install_svm_version(MIN_SOLC.to_string())?;
project.solc = solc;
project.auto_detect = false;
if let Ok(output) = suppress_compile(&project) {
if let Ok(output) = ProjectCompiler::new().quiet(true).compile(&project) {
out = output;
let (_, new_artifact) = out
.artifacts()
Expand All @@ -181,8 +181,7 @@ async fn fetch_and_print_storage(
pretty: bool,
) -> Result<()> {
if is_storage_layout_empty(&artifact.storage_layout) {
eprintln!("Storage layout is empty.");
Ok(())
sh_note!("Storage layout is empty.")
} else {
let layout = artifact.storage_layout.as_ref().unwrap().clone();
let values = fetch_storage_values(provider, address, &layout).await?;
Expand Down
5 changes: 1 addition & 4 deletions crates/cast/bin/cmd/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ pub enum WalletSubcommands {
#[clap(visible_aliases = &["a", "addr"])]
Address {
/// If provided, the address will be derived from the specified private key.
#[clap(
value_name = "PRIVATE_KEY",
value_parser = foundry_common::clap_helpers::strip_0x_prefix,
)]
#[clap(value_name = "PRIVATE_KEY")]
private_key_override: Option<String>,

#[clap(flatten)]
Expand Down
26 changes: 21 additions & 5 deletions crates/cast/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// TODO
#![allow(clippy::disallowed_macros)]

use cast::{Cast, SimpleCast};
use clap::{CommandFactory, Parser};
use clap_complete::generate;
Expand All @@ -8,18 +11,22 @@ use ethers::{
utils::keccak256,
};
use eyre::Result;
use foundry_cli::{handler, prompt, stdin, utils};
use foundry_cli::{handler, utils};
use foundry_common::{
abi::{format_tokens, get_event},
fs,
selectors::{
decode_calldata, decode_event_topic, decode_function_selector, import_selectors,
parse_signatures, pretty_calldata, ParsedSignatures, SelectorImportData,
},
stdin,
};
use foundry_config::Config;
use std::time::Instant;

#[macro_use]
extern crate foundry_common;

pub mod cmd;
pub mod opts;

Expand All @@ -30,13 +37,22 @@ use opts::{Opts, Subcommands, ToBaseArgs};
static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;

#[tokio::main]
async fn main() -> Result<()> {
async fn main() {
if let Err(err) = run().await {
let _ = foundry_common::Shell::get().error(&err);
std::process::exit(1);
}
}

async fn run() -> Result<()> {
utils::load_dotenv();
handler::install()?;
handler::install();
utils::subscriber();
utils::enable_paint();

let opts = Opts::parse();
// SAFETY: See [foundry_common::Shell::set].
unsafe { opts.shell.shell().set() };
match opts.sub {
// Constants
Subcommands::MaxInt { r#type } => {
Expand Down Expand Up @@ -388,10 +404,10 @@ async fn main() -> Result<()> {
let signatures = stdin::unwrap_vec(signatures)?;
let ParsedSignatures { signatures, abis } = parse_signatures(signatures);
if !abis.is_empty() {
import_selectors(SelectorImportData::Abi(abis)).await?.describe();
import_selectors(SelectorImportData::Abi(abis)).await?.describe()?;
}
if !signatures.is_empty() {
import_selectors(SelectorImportData::Raw(signatures)).await?.describe();
import_selectors(SelectorImportData::Raw(signatures)).await?.describe()?;
}
}

Expand Down
21 changes: 12 additions & 9 deletions crates/cast/bin/opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ethers::{
};
use eyre::Result;
use foundry_cli::{
opts::{EtherscanOpts, RpcOpts},
opts::{EtherscanOpts, RpcOpts, ShellOptions},
utils::parse_u256,
};
use std::{path::PathBuf, str::FromStr};
Expand All @@ -24,19 +24,22 @@ const VERSION_MESSAGE: &str = concat!(
")"
);

#[derive(Debug, Parser)]
#[clap(name = "cast", version = VERSION_MESSAGE)]
/// Perform Ethereum RPC calls from the comfort of your command line.
#[derive(Parser)]
#[clap(
name = "cast",
version = VERSION_MESSAGE,
after_help = "Find more information in the book: http://book.getfoundry.sh/reference/cast/cast.html",
next_display_order = None,
)]
pub struct Opts {
#[clap(subcommand)]
pub sub: Subcommands,
#[clap(flatten)]
pub shell: ShellOptions,
}

/// Perform Ethereum RPC calls from the comfort of your command line.
#[derive(Debug, Subcommand)]
#[clap(
after_help = "Find more information in the book: http://book.getfoundry.sh/reference/cast/cast.html",
next_display_order = None
)]
#[derive(Subcommand)]
pub enum Subcommands {
/// Prints the maximum value of the given integer type.
#[clap(visible_aliases = &["--max-int", "maxi"])]
Expand Down
10 changes: 5 additions & 5 deletions crates/cast/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ impl FromStr for Base {
"10" | "d" | "dec" | "decimal" => Ok(Self::Decimal),
"16" | "h" | "hex" | "hexadecimal" => Ok(Self::Hexadecimal),
s => Err(eyre::eyre!(
r#"Invalid base "{}". Possible values:
2, b, bin, binary
8, o, oct, octal
"\
Invalid base \"{s}\". Possible values:
2, b, bin, binary
8, o, oct, octal
10, d, dec, decimal
16, h, hex, hexadecimal"#,
s
16, h, hex, hexadecimal"
)),
}
}
Expand Down
2 changes: 0 additions & 2 deletions crates/cast/src/rlp_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ mod test {
assert_eq!(rlp::decode::<Item>(&encoded)?, params.2);
let decoded = rlp::decode::<Item>(&params.1);
assert_eq!(rlp::encode::<Item>(&decoded?), params.1);
println!("case {} validated", params.0)
}

Ok(())
Expand Down Expand Up @@ -164,7 +163,6 @@ mod test {
let val = serde_json::from_str(params.1)?;
let item = Item::value_to_item(&val).unwrap();
assert_eq!(item, params.2);
println!("case {} validated", params.0);
}

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions crates/cast/tests/cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Contains various tests for checking cast commands

#![allow(clippy::disallowed_macros)]

use foundry_test_utils::{
casttest,
util::{OutputExt, TestCommand, TestProject},
Expand Down
6 changes: 4 additions & 2 deletions crates/chisel/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Chisel CLI
//!
//! This module contains the core readline loop for the Chisel CLI as well as the
//! executable's `main` function.
//! This module contains the core readline loop for the Chisel CLI as well as the executable's
//! `main` function.

#![allow(clippy::disallowed_macros)]

use chisel::{
history::chisel_history_file,
Expand Down
Loading