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

fix(solc): make scoped reporter work in parallel #1214

Merged
merged 2 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
11 changes: 10 additions & 1 deletion ethers-solc/src/compile/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,20 @@ fn compile_parallel(
}
}

// need to get the currently installed reporter before installing the pool, otherwise each new
// thread in the pool will get initialized with the default value of the `thread_local!`
// localkey. This way we keep access to the reporter in the rayon pool
let scoped_report = report::get_default(|reporter| reporter.clone());

// start a rayon threadpool that will execute all `Solc::compile()` processes
let pool = rayon::ThreadPoolBuilder::new().num_threads(num_jobs).build().unwrap();

let outputs = pool.install(move || {
jobs.into_par_iter()
.map(|(solc, version, input, actually_dirty)| {
.map(move |(solc, version, input, actually_dirty)| {
// set the reporter on this thread
let _guard = report::set_scoped(&scoped_report);

tracing::trace!(
"calling solc `{}` {:?} with {} sources: {:?}",
version,
Expand Down
4 changes: 3 additions & 1 deletion ethers-solc/src/report/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ where
/// print custom messages to `stdout`.
///
/// A `Reporter` is entirely passive and only listens to incoming "events".
pub trait Reporter: 'static {
pub trait Reporter: 'static + std::fmt::Debug {
/// Callback invoked right before [`Solc::compile()`] is called
///
/// This contains the [Solc] its [Version] the complete [CompilerInput] and all files that
Expand Down Expand Up @@ -453,6 +453,7 @@ mod tests {

#[test]
fn scoped_reporter_works() {
#[derive(Debug)]
struct TestReporter;
impl Reporter for TestReporter {}

Expand All @@ -468,6 +469,7 @@ mod tests {
});

set_global_reporter(Report::new(BasicStdoutReporter::default())).unwrap();
#[derive(Debug)]
struct TestReporter;
impl Reporter for TestReporter {}

Expand Down