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

feat(forge): new flatten implementation #6936

Merged
merged 11 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ foundry-test-utils = { path = "crates/test-utils" }

# solc & compilation utilities
foundry-block-explorers = { version = "0.2.0", default-features = false }
foundry-compilers = { version = "0.2.4", default-features = false }
foundry-compilers = { version = "0.2.5", default-features = false }

## revm
# no default features to avoid c-kzg
Expand Down Expand Up @@ -222,4 +222,4 @@ revm-primitives = { git = "https://github.com/bluealloy/revm", branch = "reth_fr
revm-interpreter = { git = "https://github.com/bluealloy/revm", branch = "reth_freeze" }
revm-precompile = { git = "https://github.com/bluealloy/revm", branch = "reth_freeze" }

revm-inspectors = { git = "https://github.com/paradigmxyz/evm-inspectors" }
revm-inspectors = { git = "https://github.com/paradigmxyz/evm-inspectors" }
27 changes: 23 additions & 4 deletions crates/forge/bin/cmd/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use foundry_cli::{
opts::{CoreBuildArgs, ProjectPathsArgs},
utils::LoadConfig,
};
use foundry_common::fs;
use foundry_common::{compile::ProjectCompiler, fs};
use foundry_compilers::{artifacts::Source, error::SolcError, flatten::Flattener, Graph};
use std::path::PathBuf;

/// CLI arguments for `forge flatten`.
Expand Down Expand Up @@ -38,10 +39,28 @@ impl FlattenArgs {

let config = build_args.try_load_config_emit_warnings()?;

let paths = config.project_paths();
let target_path = dunce::canonicalize(target_path)?;
let flattened =
paths.flatten(&target_path).map_err(|err| eyre::eyre!("Failed to flatten: {err}"))?;

// We need to provide Flattener with compiled output of target and all of it's imports.
klkvr marked this conversation as resolved.
Show resolved Hide resolved
let project = config.ephemeral_no_artifacts_project()?;
let sources = Source::read_all(vec![target_path.clone()])?;
let (sources, _) = Graph::resolve_sources(&project.paths, sources)?.into_sources();

let compiler_output = ProjectCompiler::new().files(sources.into_keys()).compile(&project);
klkvr marked this conversation as resolved.
Show resolved Hide resolved

let flattened = match compiler_output {
Ok(compiler_output) => match Flattener::new(&project, &compiler_output, &target_path) {
Ok(flattener) => Ok(flattener.flatten()),
Err(err) => Err(err),
},
klkvr marked this conversation as resolved.
Show resolved Hide resolved
Err(_) => {
// Fallback to the old flattening compilation if we couldn't compile the target
// successfully. This would be the case if the target has invalid
// syntax. (e.g. Solang)
project.paths.flatten(&target_path)
}
}
.map_err(|err: SolcError| eyre::eyre!("Failed to flatten: {err}"))?;
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved

match output {
Some(output) => {
Expand Down
Loading