Skip to content

Commit

Permalink
fix: absolute paths in build info (#121)
Browse files Browse the repository at this point in the history
Closes foundry-rs/foundry#7878

Removes paths stripping logic from solc implementation, instead we now
use `CompilerInput::strip_prefix` and `CompilerOutput::join_all`
directly in `compile_*` functions, and only join output paths after
writing build info.
  • Loading branch information
klkvr authored May 7, 2024
1 parent b16faac commit f56a318
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 41 deletions.
12 changes: 0 additions & 12 deletions src/artifacts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1648,18 +1648,6 @@ impl CompilerOutput {
self.contracts.extend(other.contracts);
self.sources.extend(other.sources);
}

pub fn join_all(&mut self, root: impl AsRef<Path>) {
let root = root.as_ref();
self.contracts = std::mem::take(&mut self.contracts)
.into_iter()
.map(|(path, contracts)| (root.join(path), contracts))
.collect();
self.sources = std::mem::take(&mut self.sources)
.into_iter()
.map(|(path, source)| (root.join(path), source))
.collect();
}
}

/// A wrapper helper type for the `Contracts` type alias
Expand Down
15 changes: 10 additions & 5 deletions src/compile/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,15 +558,16 @@ fn compile_sequential<C: Compiler>(
input.sources().keys()
);

let input = input.with_remappings(paths.remappings.clone());
let mut input = input.with_remappings(paths.remappings.clone());
input.strip_prefix(paths.root.as_path());

let start = Instant::now();
report::compiler_spawn(
&input.compiler_name(),
compiler.version(),
actually_dirty.as_slice(),
);
let (input, output) = compiler.compile(input)?;
let mut output = compiler.compile(&input)?;
report::compiler_success(&input.compiler_name(), compiler.version(), &start.elapsed());
// trace!("compiled input, output has error: {}", output.has_error());
trace!("received compiler output: {:?}", output.contracts.keys());
Expand All @@ -577,6 +578,8 @@ fn compile_sequential<C: Compiler>(
aggregated.build_infos.insert(version.clone(), build_info);
}

output.join_all(paths.root.as_path());

aggregated.extend(version.clone(), output);
}
}
Expand Down Expand Up @@ -645,7 +648,8 @@ fn compile_parallel<C: Compiler>(
input.sources().keys()
);

let input = input.with_remappings(paths.remappings.clone());
let mut input = input.with_remappings(paths.remappings.clone());
input.strip_prefix(paths.root.as_path());

jobs.push((compiler.clone(), version.clone(), input, actually_dirty));
}
Expand Down Expand Up @@ -677,7 +681,7 @@ fn compile_parallel<C: Compiler>(
compiler.version(),
actually_dirty.as_slice(),
);
compiler.compile(input).map(move |(input, output)| {
compiler.compile(&input).map(move |output| {
report::compiler_success(
&input.compiler_name(),
compiler.version(),
Expand All @@ -690,12 +694,13 @@ fn compile_parallel<C: Compiler>(
})?;

let mut aggregated = AggregatedCompilerOutput::default();
for (version, input, output) in outputs {
for (version, input, mut output) in outputs {
// if configured also create the build info
if create_build_info {
let build_info = RawBuildInfo::new(&input, &output, &version)?;
aggregated.build_infos.insert(version.clone(), build_info);
}
output.join_all(paths.root.as_path());
aggregated.extend(version, output);
}

Expand Down
20 changes: 16 additions & 4 deletions src/compilers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ pub trait CompilerInput: Serialize + Send + Sync + Sized {

/// Returns compiler name used by reporters to display output during compilation.
fn compiler_name(&self) -> String;

/// Strips given prefix from all paths.
fn strip_prefix(&mut self, base: &Path);
}

/// Parser of the source files which is used to identify imports and version requirements of the
Expand Down Expand Up @@ -105,6 +108,18 @@ impl<E> CompilerOutput<E> {
self.contracts.extend(other.contracts);
self.sources.extend(other.sources);
}

pub fn join_all(&mut self, root: impl AsRef<Path>) {
let root = root.as_ref();
self.contracts = std::mem::take(&mut self.contracts)
.into_iter()
.map(|(path, contracts)| (root.join(path), contracts))
.collect();
self.sources = std::mem::take(&mut self.sources)
.into_iter()
.map(|(path, source)| (root.join(path), source))
.collect();
}
}

impl<E> Default for CompilerOutput<E> {
Expand Down Expand Up @@ -132,10 +147,7 @@ pub trait Compiler: Send + Sync + Clone {
/// Main entrypoint for the compiler. Compiles given input into [CompilerOutput]. Takes
/// ownership over the input and returns back version with potential modifications made to it.
/// Returned input is always the one which was seen by the binary.
fn compile(
&self,
input: Self::Input,
) -> Result<(Self::Input, CompilerOutput<Self::CompilationError>)>;
fn compile(&self, input: &Self::Input) -> Result<CompilerOutput<Self::CompilationError>>;

/// Returns the version of the compiler.
fn version(&self) -> &Version;
Expand Down
24 changes: 8 additions & 16 deletions src/compilers/solc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
use semver::Version;
use std::{
collections::{BTreeMap, BTreeSet},
path::PathBuf,
path::{Path, PathBuf},
};

impl Compiler for Solc {
Expand All @@ -31,28 +31,16 @@ impl Compiler for Solc {
type ParsedSource = SolData;
type Settings = SolcSettings;

fn compile(
&self,
mut input: Self::Input,
) -> Result<(Self::Input, CompilerOutput<Self::CompilationError>)> {
if let Some(base_path) = self.base_path.clone() {
// Strip prefix from all sources to ensure deterministic metadata.
input.strip_prefix(base_path);
}

let mut solc_output = self.compile(&input)?;

if let Some(ref base_path) = self.base_path {
solc_output.join_all(base_path);
}
fn compile(&self, input: &Self::Input) -> Result<CompilerOutput<Self::CompilationError>> {
let solc_output = self.compile(&input)?;

let output = CompilerOutput {
errors: solc_output.errors,
contracts: solc_output.contracts,
sources: solc_output.sources,
};

Ok((input, output))
Ok(output)
}

fn version(&self) -> &Version {
Expand Down Expand Up @@ -123,6 +111,10 @@ impl CompilerInput for SolcInput {
fn compiler_name(&self) -> String {
"Solc".to_string()
}

fn strip_prefix(&mut self, base: &Path) {
self.strip_prefix(base)
}
}

impl CompilerSettings for SolcSettings {
Expand Down
9 changes: 9 additions & 0 deletions src/compilers/vyper/input.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::Path;

use super::settings::VyperSettings;
use crate::{artifacts::Sources, compilers::CompilerInput};
use semver::Version;
Expand All @@ -24,4 +26,11 @@ impl CompilerInput for VyperInput {
fn compiler_name(&self) -> String {
"Vyper".to_string()
}

fn strip_prefix(&mut self, base: &Path) {
self.sources = std::mem::take(&mut self.sources)
.into_iter()
.map(|(path, s)| (path.strip_prefix(base).map(Into::into).unwrap_or(path), s))
.collect();
}
}
6 changes: 2 additions & 4 deletions src/compilers/vyper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,8 @@ impl Compiler for Vyper {
type ParsedSource = VyperParsedSource;
type Input = VyperInput;

fn compile(&self, input: Self::Input) -> Result<(Self::Input, VyperCompilerOutput)> {
let output = self.compile(&input)?;

Ok((input, output))
fn compile(&self, input: &Self::Input) -> Result<VyperCompilerOutput> {
self.compile(input)
}

fn version(&self) -> &Version {
Expand Down

0 comments on commit f56a318

Please sign in to comment.