Skip to content

Commit bd3db5b

Browse files
authored
fix(forge): provide better error messages for spurious cache failures in inspect (#11422)
1 parent 0d07e78 commit bd3db5b

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

crates/forge/src/cmd/inspect.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,7 @@ impl InspectArgs {
8282
// Match on ContractArtifactFields and pretty-print
8383
match field {
8484
ContractArtifactField::Abi => {
85-
let abi = artifact
86-
.abi
87-
.as_ref()
88-
.ok_or_else(|| eyre::eyre!("Failed to fetch lossless ABI"))?;
85+
let abi = artifact.abi.as_ref().ok_or_else(|| missing_error("ABI"))?;
8986
print_abi(abi)?;
9087
}
9188
ContractArtifactField::Bytecode => {
@@ -276,7 +273,7 @@ fn internal_ty(ty: &InternalType) -> String {
276273

277274
pub fn print_storage_layout(storage_layout: Option<&StorageLayout>) -> Result<()> {
278275
let Some(storage_layout) = storage_layout else {
279-
eyre::bail!("Could not get storage layout");
276+
return Err(missing_error("storage layout"));
280277
};
281278

282279
if shell::is_json() {
@@ -309,7 +306,7 @@ pub fn print_storage_layout(storage_layout: Option<&StorageLayout>) -> Result<()
309306

310307
fn print_method_identifiers(method_identifiers: &Option<BTreeMap<String, String>>) -> Result<()> {
311308
let Some(method_identifiers) = method_identifiers else {
312-
eyre::bail!("Could not get method identifiers");
309+
return Err(missing_error("method identifiers"));
313310
};
314311

315312
if shell::is_json() {
@@ -541,7 +538,7 @@ fn print_json_str(obj: &impl serde::Serialize, key: Option<&str>) -> Result<()>
541538

542539
fn print_yul(yul: Option<&str>, strip_comments: bool) -> Result<()> {
543540
let Some(yul) = yul else {
544-
eyre::bail!("Could not get IR output");
541+
return Err(missing_error("IR output"));
545542
};
546543

547544
static YUL_COMMENTS: LazyLock<Regex> =
@@ -558,17 +555,24 @@ fn print_yul(yul: Option<&str>, strip_comments: bool) -> Result<()> {
558555

559556
fn get_json_str(obj: &impl serde::Serialize, key: Option<&str>) -> Result<String> {
560557
let value = serde_json::to_value(obj)?;
561-
let mut value_ref = &value;
562-
if let Some(key) = key
563-
&& let Some(value2) = value.get(key)
558+
let value = if let Some(key) = key
559+
&& let Some(value) = value.get(key)
564560
{
565-
value_ref = value2;
566-
}
567-
let s = match value_ref.as_str() {
568-
Some(s) => s.to_string(),
569-
None => format!("{value_ref:#}"),
561+
value
562+
} else {
563+
&value
570564
};
571-
Ok(s)
565+
Ok(match value.as_str() {
566+
Some(s) => s.to_string(),
567+
None => format!("{value:#}"),
568+
})
569+
}
570+
571+
fn missing_error(field: &str) -> eyre::Error {
572+
eyre!(
573+
"{field} missing from artifact; \
574+
this could be a spurious caching issue, consider running `forge clean`"
575+
)
572576
}
573577

574578
#[cfg(test)]

0 commit comments

Comments
 (0)