From 27a4454ac00020374033fd754817a2ffd5d2c844 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sun, 13 Feb 2022 00:05:43 +0100 Subject: [PATCH] feat(solc): report on unresolved imports (#905) * chore: fix unused import warning * feat: add additional report message --- ethers-solc/src/compile/mod.rs | 5 +---- ethers-solc/src/report.rs | 12 ++++++++++++ ethers-solc/src/resolver.rs | 5 ++++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/ethers-solc/src/compile/mod.rs b/ethers-solc/src/compile/mod.rs index 2ced3f05..0fb12ee8 100644 --- a/ethers-solc/src/compile/mod.rs +++ b/ethers-solc/src/compile/mod.rs @@ -42,15 +42,12 @@ pub const BERLIN_SOLC: Version = Version::new(0, 8, 5); /// https://blog.soliditylang.org/2021/08/11/solidity-0.8.7-release-announcement/ pub const LONDON_SOLC: Version = Version::new(0, 8, 7); -#[cfg(any(test, all(feature = "svm", feature = "async")))] -use once_cell::sync::Lazy; - #[cfg(any(test, feature = "tests"))] use std::sync::Mutex; #[cfg(any(test, feature = "tests"))] #[allow(unused)] -static LOCK: Lazy> = Lazy::new(|| Mutex::new(())); +static LOCK: once_cell::sync::Lazy> = once_cell::sync::Lazy::new(|| Mutex::new(())); /// take the lock in tests, we use this to enforce that /// a test does not run while a compiler version is being installed diff --git a/ethers-solc/src/report.rs b/ethers-solc/src/report.rs index 22440a76..af6ff608 100644 --- a/ethers-solc/src/report.rs +++ b/ethers-solc/src/report.rs @@ -5,6 +5,7 @@ use semver::Version; use std::{ error::Error, fmt, + path::Path, sync::{ atomic::{AtomicUsize, Ordering}, Arc, @@ -65,6 +66,9 @@ pub trait Reporter: 'static { /// Invoked before a new [`Solc`] bin was successfully installed fn on_solc_installation_success(&self, _version: &Version) {} + + /// Invoked if the import couldn't be resolved + fn on_unresolved_import(&self, _import: &Path) {} } pub(crate) fn solc_spawn(solc: &Solc, version: &Version, input: &CompilerInput) { @@ -85,6 +89,10 @@ pub(crate) fn solc_installation_success(version: &Version) { with_global(|r| r.reporter.on_solc_installation_success(version)); } +pub(crate) fn unresolved_import(import: &Path) { + with_global(|r| r.reporter.on_unresolved_import(import)); +} + fn get_global() -> Option<&'static Report> { if GLOBAL_REPORTER_STATE.load(Ordering::SeqCst) != SET { return None @@ -139,6 +147,10 @@ impl Reporter for BasicStdoutReporter { fn on_solc_installation_success(&self, version: &Version) { println!("Successfully installed solc {}", version); } + + fn on_unresolved_import(&self, import: &Path) { + println!("Unable to resolve imported file: \"{}\"", import.display()); + } } /// Returned if setting the global reporter fails. diff --git a/ethers-solc/src/resolver.rs b/ethers-solc/src/resolver.rs index 2a295baa..1d9a515a 100644 --- a/ethers-solc/src/resolver.rs +++ b/ethers-solc/src/resolver.rs @@ -239,7 +239,10 @@ impl Graph { Ok(import) => { add_node(&mut unresolved, &mut index, &mut resolved_imports, import)?; } - Err(err) => tracing::trace!("failed to resolve import component \"{:?}\"", err), + Err(err) => { + crate::report::unresolved_import(import.data()); + tracing::trace!("failed to resolve import component \"{:?}\"", err) + } }; } nodes.push(node);