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 6 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
12 changes: 4 additions & 8 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ foundry-macros = { path = "crates/macros" }
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-block-explorers = { version = "0.2.3", default-features = false }
foundry-compilers = { version = "0.3.0", 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" }
26 changes: 22 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,27 @@ 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 its imports.
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) => {
Flattener::new(&project, &compiler_output, &target_path).map(|f| f.flatten())
}
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