foundry_compilers/report/
mod.rs

1//! Subscribe to events in the compiler pipeline
2//!
3//! The _reporter_ is the component of the [`crate::Project::compile()`] pipeline which is
4//! responsible for reporting on specific steps in the process.
5//!
6//! By default, the current reporter is a noop that does
7//! nothing.
8//!
9//! To use another report implementation, it must be set as the current reporter.
10//! There are two methods for doing so: [`with_scoped`] and
11//! [`try_init`]. `with_scoped` sets the reporter for the
12//! duration of a scope, while `set_global` sets a global default report
13//! for the entire process.
14
15// <https://github.com/tokio-rs/tracing/blob/master/tracing-core/src/dispatch.rs>
16
17#![allow(static_mut_refs)] // TODO
18
19use foundry_compilers_artifacts::remappings::Remapping;
20use semver::Version;
21use std::{
22    any::{Any, TypeId},
23    cell::RefCell,
24    error::Error,
25    fmt,
26    path::{Path, PathBuf},
27    ptr::NonNull,
28    sync::{
29        atomic::{AtomicBool, AtomicUsize, Ordering},
30        Arc,
31    },
32    time::Duration,
33};
34
35mod compiler;
36pub use compiler::SolcCompilerIoReporter;
37
38thread_local! {
39    static CURRENT_STATE: State = State {
40        scoped: RefCell::new(Report::none()),
41    };
42}
43
44static EXISTS: AtomicBool = AtomicBool::new(false);
45static SCOPED_COUNT: AtomicUsize = AtomicUsize::new(0);
46
47// tracks the state of `GLOBAL_REPORTER`
48static GLOBAL_REPORTER_STATE: AtomicUsize = AtomicUsize::new(UN_SET);
49
50const UN_SET: usize = 0;
51const SETTING: usize = 1;
52const SET: usize = 2;
53
54static mut GLOBAL_REPORTER: Option<Report> = None;
55
56/// Install this `Reporter` as the global default if one is
57/// not already set.
58///
59/// # Errors
60/// Returns an Error if the initialization was unsuccessful, likely
61/// because a global reporter was already installed by another
62/// call to `try_init`.
63pub fn try_init<T>(reporter: T) -> Result<(), Box<dyn Error + Send + Sync + 'static>>
64where
65    T: Reporter + Send + Sync + 'static,
66{
67    set_global_reporter(Report::new(reporter))?;
68    Ok(())
69}
70
71/// Install this `Reporter` as the global default.
72///
73/// # Panics
74///
75/// Panics if the initialization was unsuccessful, likely because a
76/// global reporter was already installed by another call to `try_init`.
77/// ```
78/// use foundry_compilers::report::BasicStdoutReporter;
79/// let subscriber = foundry_compilers::report::init(BasicStdoutReporter::default());
80/// ```
81pub fn init<T>(reporter: T)
82where
83    T: Reporter + Send + Sync + 'static,
84{
85    try_init(reporter).expect("Failed to install global reporter")
86}
87
88/// Trait representing the functions required to emit information about various steps in the
89/// compiler pipeline.
90///
91/// This trait provides a series of callbacks that are invoked at certain parts of the
92/// [`crate::Project::compile()`] process.
93///
94/// Implementers of this trait can use these callbacks to emit additional information, for example
95/// print custom messages to `stdout`.
96///
97/// A `Reporter` is entirely passive and only listens to incoming "events".
98pub trait Reporter: 'static + std::fmt::Debug {
99    /// Callback invoked right before [Compiler::compile] is called
100    ///
101    /// This contains the [Compiler] its [Version] and all files that triggered the compile job. The
102    /// dirty files are only provided to give a better feedback what was actually compiled.
103    ///
104    /// [Compiler]: crate::compilers::Compiler
105    /// [Compiler::compile]: crate::compilers::Compiler::compile
106    fn on_compiler_spawn(
107        &self,
108        _compiler_name: &str,
109        _version: &Version,
110        _dirty_files: &[PathBuf],
111    ) {
112    }
113
114    /// Invoked with the `CompilerOutput` if [`Compiler::compile()`] was successful
115    ///
116    /// [`Compiler::compile()`]: crate::compilers::Compiler::compile
117    fn on_compiler_success(&self, _compiler_name: &str, _version: &Version, _duration: &Duration) {}
118
119    /// Invoked before a new compiler version is installed
120    fn on_solc_installation_start(&self, _version: &Version) {}
121
122    /// Invoked after a new compiler version was successfully installed
123    fn on_solc_installation_success(&self, _version: &Version) {}
124
125    /// Invoked after a compiler installation failed
126    fn on_solc_installation_error(&self, _version: &Version, _error: &str) {}
127
128    /// Invoked if imports couldn't be resolved with the given remappings, where `imports` is the
129    /// list of all import paths and the file they occurred in: `(import stmt, file)`
130    fn on_unresolved_imports(&self, _imports: &[(&Path, &Path)], _remappings: &[Remapping]) {}
131
132    /// If `self` is the same type as the provided `TypeId`, returns an untyped
133    /// [`NonNull`] pointer to that type. Otherwise, returns `None`.
134    ///
135    /// If you wish to downcast a `Reporter`, it is strongly advised to use
136    /// the safe API provided by downcast_ref instead.
137    ///
138    /// This API is required for `downcast_raw` to be a trait method; a method
139    /// signature like downcast_ref (with a generic type parameter) is not
140    /// object-safe, and thus cannot be a trait method for `Reporter`. This
141    /// means that if we only exposed downcast_ref, `Reporter`
142    /// implementations could not override the downcasting behavior
143    ///
144    /// # Safety
145    ///
146    /// The downcast_ref method expects that the pointer returned by
147    /// `downcast_raw` points to a valid instance of the type
148    /// with the provided `TypeId`. Failure to ensure this will result in
149    /// undefined behaviour, so implementing `downcast_raw` is unsafe.
150    unsafe fn downcast_raw(&self, id: TypeId) -> Option<NonNull<()>> {
151        if id == TypeId::of::<Self>() {
152            Some(NonNull::from(self).cast())
153        } else {
154            None
155        }
156    }
157}
158
159impl dyn Reporter {
160    /// Returns `true` if this `Reporter` is the same type as `T`.
161    pub fn is<T: Any>(&self) -> bool {
162        self.downcast_ref::<T>().is_some()
163    }
164
165    /// Returns some reference to this `Reporter` value if it is of type `T`,
166    /// or `None` if it isn't.
167    pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
168        unsafe {
169            let raw = self.downcast_raw(TypeId::of::<T>())?;
170            Some(&*(raw.cast().as_ptr()))
171        }
172    }
173}
174
175pub(crate) fn compiler_spawn(compiler_name: &str, version: &Version, dirty_files: &[PathBuf]) {
176    get_default(|r| r.reporter.on_compiler_spawn(compiler_name, version, dirty_files));
177}
178
179pub(crate) fn compiler_success(compiler_name: &str, version: &Version, duration: &Duration) {
180    get_default(|r| r.reporter.on_compiler_success(compiler_name, version, duration));
181}
182
183#[allow(dead_code)]
184pub(crate) fn solc_installation_start(version: &Version) {
185    get_default(|r| r.reporter.on_solc_installation_start(version));
186}
187
188#[allow(dead_code)]
189pub(crate) fn solc_installation_success(version: &Version) {
190    get_default(|r| r.reporter.on_solc_installation_success(version));
191}
192
193#[allow(dead_code)]
194pub(crate) fn solc_installation_error(version: &Version, error: &str) {
195    get_default(|r| r.reporter.on_solc_installation_error(version, error));
196}
197
198pub(crate) fn unresolved_imports(imports: &[(&Path, &Path)], remappings: &[Remapping]) {
199    get_default(|r| r.reporter.on_unresolved_imports(imports, remappings));
200}
201
202fn get_global() -> Option<&'static Report> {
203    if GLOBAL_REPORTER_STATE.load(Ordering::SeqCst) != SET {
204        return None;
205    }
206    unsafe {
207        // This is safe given the invariant that setting the global reporter
208        // also sets `GLOBAL_REPORTER_STATE` to `SET`.
209        Some(GLOBAL_REPORTER.as_ref().expect(
210            "Reporter invariant violated: GLOBAL_REPORTER must be initialized before GLOBAL_REPORTER_STATE is set",
211        ))
212    }
213}
214
215/// Executes a closure with a reference to this thread's current reporter.
216#[inline(always)]
217pub fn get_default<T, F>(mut f: F) -> T
218where
219    F: FnMut(&Report) -> T,
220{
221    if SCOPED_COUNT.load(Ordering::Acquire) == 0 {
222        // fast path if no scoped reporter has been set; use the global
223        // default.
224        return if let Some(glob) = get_global() { f(glob) } else { f(&Report::none()) };
225    }
226
227    get_default_scoped(f)
228}
229
230#[inline(never)]
231fn get_default_scoped<T, F>(mut f: F) -> T
232where
233    F: FnMut(&Report) -> T,
234{
235    CURRENT_STATE
236        .try_with(|state| {
237            let scoped = state.scoped.borrow_mut();
238            f(&scoped)
239        })
240        .unwrap_or_else(|_| f(&Report::none()))
241}
242
243/// Executes a closure with a reference to the `Reporter`.
244pub fn with_global<T>(f: impl FnOnce(&Report) -> T) -> Option<T> {
245    let report = get_global()?;
246    Some(f(report))
247}
248
249/// Sets this reporter as the scoped reporter for the duration of a closure.
250pub fn with_scoped<T>(report: &Report, f: impl FnOnce() -> T) -> T {
251    // When this guard is dropped, the scoped reporter will be reset to the
252    // prior reporter. Using this (rather than simply resetting after calling
253    // `f`) ensures that we always reset to the prior reporter even if `f`
254    // panics.
255    let _guard = set_scoped(report);
256    f()
257}
258
259/// The report state of a thread.
260struct State {
261    /// This thread's current scoped reporter.
262    scoped: RefCell<Report>,
263}
264
265impl State {
266    /// Replaces the current scoped reporter on this thread with the provided
267    /// reporter.
268    ///
269    /// Dropping the returned `ResetGuard` will reset the scoped reporter to
270    /// the previous value.
271    #[inline]
272    fn set_scoped(new_report: Report) -> ScopeGuard {
273        let prior = CURRENT_STATE.try_with(|state| state.scoped.replace(new_report)).ok();
274        EXISTS.store(true, Ordering::Release);
275        SCOPED_COUNT.fetch_add(1, Ordering::Release);
276        ScopeGuard(prior)
277    }
278}
279
280/// A guard that resets the current scoped reporter to the prior
281/// scoped reporter when dropped.
282#[derive(Debug)]
283pub struct ScopeGuard(Option<Report>);
284
285impl Drop for ScopeGuard {
286    #[inline]
287    fn drop(&mut self) {
288        SCOPED_COUNT.fetch_sub(1, Ordering::Release);
289        if let Some(report) = self.0.take() {
290            // Replace the reporter and then drop the old one outside
291            // of the thread-local context.
292            let prev = CURRENT_STATE.try_with(|state| state.scoped.replace(report));
293            drop(prev)
294        }
295    }
296}
297
298/// Sets the reporter as the scoped reporter for the duration of the lifetime
299/// of the returned DefaultGuard
300#[must_use = "Dropping the guard unregisters the reporter."]
301pub fn set_scoped(reporter: &Report) -> ScopeGuard {
302    // When this guard is dropped, the scoped reporter will be reset to the
303    // prior default. Using this ensures that we always reset to the prior
304    // reporter even if the thread calling this function panics.
305    State::set_scoped(reporter.clone())
306}
307
308/// A no-op [`Reporter`] that does nothing.
309#[derive(Clone, Copy, Debug, Default)]
310pub struct NoReporter(());
311
312impl Reporter for NoReporter {}
313
314/// A [`Reporter`] that emits some general information to `stdout`
315#[derive(Clone, Debug, Default)]
316pub struct BasicStdoutReporter {
317    _priv: (),
318}
319
320impl Reporter for BasicStdoutReporter {
321    /// Callback invoked right before [`Compiler::compile()`] is called
322    ///
323    /// [`Compiler::compile()`]: crate::compilers::Compiler::compile
324    fn on_compiler_spawn(&self, compiler_name: &str, version: &Version, dirty_files: &[PathBuf]) {
325        println!(
326            "Compiling {} files with {} {}.{}.{}",
327            dirty_files.len(),
328            compiler_name,
329            version.major,
330            version.minor,
331            version.patch
332        );
333    }
334
335    fn on_compiler_success(&self, compiler_name: &str, version: &Version, duration: &Duration) {
336        println!(
337            "{} {}.{}.{} finished in {duration:.2?}",
338            compiler_name, version.major, version.minor, version.patch
339        );
340    }
341
342    /// Invoked before a new compiler is installed
343    fn on_solc_installation_start(&self, version: &Version) {
344        println!("installing solc version \"{version}\"");
345    }
346
347    /// Invoked before a new compiler was successfully installed
348    fn on_solc_installation_success(&self, version: &Version) {
349        println!("Successfully installed solc {version}");
350    }
351
352    fn on_solc_installation_error(&self, version: &Version, error: &str) {
353        eprintln!("Failed to install solc {version}: {error}");
354    }
355
356    fn on_unresolved_imports(&self, imports: &[(&Path, &Path)], remappings: &[Remapping]) {
357        if imports.is_empty() {
358            return;
359        }
360        println!("{}", format_unresolved_imports(imports, remappings))
361    }
362}
363
364/// Creates a meaningful message for all unresolved imports
365pub fn format_unresolved_imports(imports: &[(&Path, &Path)], remappings: &[Remapping]) -> String {
366    let info = imports
367        .iter()
368        .map(|(import, file)| format!("\"{}\" in \"{}\"", import.display(), file.display()))
369        .collect::<Vec<_>>()
370        .join("\n      ");
371    format!(
372        "Unable to resolve imports:\n      {}\nwith remappings:\n      {}",
373        info,
374        remappings.iter().map(|r| r.to_string()).collect::<Vec<_>>().join("\n      ")
375    )
376}
377
378/// Returned if setting the global reporter fails.
379#[derive(Debug)]
380pub struct SetGlobalReporterError {
381    // private marker so this type can't be initiated
382    _priv: (),
383}
384
385impl fmt::Display for SetGlobalReporterError {
386    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
387        f.pad("a global reporter has already been set")
388    }
389}
390
391impl Error for SetGlobalReporterError {}
392
393/// `Report` trace data to a [`Reporter`].
394#[derive(Clone)]
395pub struct Report {
396    reporter: Arc<dyn Reporter + Send + Sync>,
397}
398
399impl Report {
400    /// Returns a new `Report` that does nothing
401    pub fn none() -> Self {
402        Self { reporter: Arc::new(NoReporter::default()) }
403    }
404
405    /// Returns a `Report` that forwards to the given [`Reporter`].
406    ///
407    /// [`Reporter`]: ../reporter/trait.Reporter.html
408    pub fn new<S>(reporter: S) -> Self
409    where
410        S: Reporter + Send + Sync + 'static,
411    {
412        Self { reporter: Arc::new(reporter) }
413    }
414
415    /// Returns `true` if this `Report` forwards to a reporter of type
416    /// `T`.
417    #[inline]
418    pub fn is<T: Any>(&self) -> bool {
419        <dyn Reporter>::is::<T>(&*self.reporter)
420    }
421}
422
423impl fmt::Debug for Report {
424    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
425        f.pad("Report(...)")
426    }
427}
428
429/// Sets this report as the global default for the duration of the entire program.
430///
431/// The global reporter can only be set once; additional attempts to set the global reporter will
432/// fail. Returns `Err` if the global reporter has already been set.
433fn set_global_reporter(report: Report) -> Result<(), SetGlobalReporterError> {
434    // `compare_exchange` tries to store `SETTING` if the current value is `UN_SET`
435    // this returns `Ok(_)` if the current value of `GLOBAL_REPORTER_STATE` was `UN_SET` and
436    // `SETTING` was written, this guarantees the value is `SETTING`.
437    if GLOBAL_REPORTER_STATE
438        .compare_exchange(UN_SET, SETTING, Ordering::SeqCst, Ordering::SeqCst)
439        .is_ok()
440    {
441        unsafe {
442            GLOBAL_REPORTER = Some(report);
443        }
444        GLOBAL_REPORTER_STATE.store(SET, Ordering::SeqCst);
445        Ok(())
446    } else {
447        Err(SetGlobalReporterError { _priv: () })
448    }
449}
450
451#[cfg(test)]
452mod tests {
453    use super::*;
454    use std::str::FromStr;
455
456    #[test]
457    fn scoped_reporter_works() {
458        #[derive(Debug)]
459        struct TestReporter;
460        impl Reporter for TestReporter {}
461
462        with_scoped(&Report::new(TestReporter), || {
463            get_default(|reporter| assert!(reporter.is::<TestReporter>()))
464        });
465    }
466
467    #[test]
468    fn global_and_scoped_reporter_works() {
469        get_default(|reporter| {
470            assert!(reporter.is::<NoReporter>());
471        });
472
473        set_global_reporter(Report::new(BasicStdoutReporter::default())).unwrap();
474        #[derive(Debug)]
475        struct TestReporter;
476        impl Reporter for TestReporter {}
477
478        with_scoped(&Report::new(TestReporter), || {
479            get_default(|reporter| assert!(reporter.is::<TestReporter>()))
480        });
481
482        get_default(|reporter| assert!(reporter.is::<BasicStdoutReporter>()))
483    }
484
485    #[test]
486    fn test_unresolved_message() {
487        let unresolved = vec![(Path::new("./src/Import.sol"), Path::new("src/File.col"))];
488
489        let remappings = vec![Remapping::from_str("oz=a/b/c/d").unwrap()];
490
491        assert_eq!(
492            format_unresolved_imports(&unresolved, &remappings).trim(),
493            r#"
494Unable to resolve imports:
495      "./src/Import.sol" in "src/File.col"
496with remappings:
497      oz/=a/b/c/d/"#
498                .trim()
499        )
500    }
501}