Skip to content

Commit

Permalink
forge: configurable build-info path (foundry-rs#2223)
Browse files Browse the repository at this point in the history
* forge: configurable build-info path

Allows the user to configure the output path of the
emitted build-info files. This is useful because tooling
that integrates with hardhat expects the `build-info` directory
to exist at a particular location and `forge` by default outputs
it at a different location.

A new cli flag to `forge build` is added: `--build-info-path`.
It is implemented as an `Option<PathBuf>` so that `ethers-rs`
can handle the default values for it when it is not configured.

* bump ethers

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
2 people authored and Rjected committed Jul 7, 2022
1 parent 9e94f9c commit f7d16fe
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 21 deletions.
34 changes: 17 additions & 17 deletions Cargo.lock

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

11 changes: 11 additions & 0 deletions cli/src/cmd/forge/build/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ pub struct CoreBuildArgs {
#[clap(help_heading = "PROJECT OPTIONS", help = "Generate build info files.", long)]
#[serde(skip)]
pub build_info: bool,

#[clap(
help_heading = "PROJECT OPTIONS",
help = "Output path to directory that build info files will be written to.",
long,
value_hint = ValueHint::DirPath,
value_name = "PATH",
requires = "build-info"
)]
#[serde(skip_serializing_if = "Option::is_none")]
pub build_info_path: Option<PathBuf>,
}

impl CoreBuildArgs {
Expand Down
1 change: 1 addition & 0 deletions cli/src/cmd/forge/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl Cmd for FlattenArgs {
revert_strings: None,
silent: false,
build_info: false,
build_info_path: None,
};

let config = Config::from(&build_args);
Expand Down
3 changes: 2 additions & 1 deletion cli/src/cmd/forge/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ impl VerifyArgs {
revert_strings: None,
silent: false,
build_info: false,
build_info_path: None,
};

let project = build_args.project()?;
Expand Down Expand Up @@ -261,7 +262,7 @@ impl VerifyArgs {
/// Parse the compiler version.
/// The priority desc:
/// 1. Through CLI arg `--compiler-version`
/// 2. `solc` defined in foundry.toml
/// 2. `solc` defined in foundry.toml
fn compiler_version(
&self,
config: &Config,
Expand Down
1 change: 1 addition & 0 deletions cli/tests/it/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ forgetest!(can_extract_config_values, |prj: TestProject, mut cmd: TestCommand| {
allow_paths: vec![],
rpc_endpoints: Default::default(),
build_info: false,
build_info_path: None,
__non_exhaustive: (),
};
prj.write_config(input.clone());
Expand Down
20 changes: 17 additions & 3 deletions config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ pub struct Config {
/// If set to `true`, `ethers-solc` will generate additional build info json files for every
/// new build, containing the `CompilerInput` and `CompilerOutput`
pub build_info: bool,
/// The path to the `build-info` directory that contains the build info json files.
pub build_info_path: Option<PathBuf>,
/// The root path where the config detection started from, `Config::with_root`
#[doc(hidden)]
// We're skipping serialization here, so it won't be included in the [`Config::to_string()`]
Expand Down Expand Up @@ -436,6 +438,10 @@ impl Config {
self.script = p(&root, &self.script);
self.out = p(&root, &self.out);

if let Some(build_info_path) = self.build_info_path {
self.build_info_path = Some(p(&root, &build_info_path));
}

self.libs = self.libs.into_iter().map(|lib| p(&root, &lib)).collect();

self.remappings =
Expand Down Expand Up @@ -609,15 +615,20 @@ impl Config {
/// let paths = config.project_paths();
/// ```
pub fn project_paths(&self) -> ProjectPathsConfig {
ProjectPathsConfig::builder()
let mut builder = ProjectPathsConfig::builder()
.cache(&self.cache_path.join(SOLIDITY_FILES_CACHE_FILENAME))
.sources(&self.src)
.tests(&self.test)
.scripts(&self.script)
.artifacts(&self.out)
.libs(self.libs.clone())
.remappings(self.get_all_remappings())
.build_with_root(&self.__root.0)
.remappings(self.get_all_remappings());

if let Some(build_info_path) = &self.build_info_path {
builder = builder.build_infos(&build_info_path);
}

builder.build_with_root(&self.__root.0)
}

/// Returns all configured [`Remappings`]
Expand Down Expand Up @@ -1417,6 +1428,7 @@ impl Default for Config {
revert_strings: None,
sparse_mode: false,
build_info: false,
build_info_path: None,
__non_exhaustive: (),
}
}
Expand Down Expand Up @@ -2379,6 +2391,7 @@ mod tests {
bytecode_hash = "ipfs"
revert_strings = "strip"
allow_paths = ["allow", "paths"]
build_info_path = "build-info"
"#,
)?;

Expand Down Expand Up @@ -2408,6 +2421,7 @@ mod tests {
("optimism", RpcEndpoint::Url("https://example.com/".to_string())),
("mainnet", RpcEndpoint::Env("RPC_MAINNET".to_string()))
]),
build_info_path: Some("build-info".into()),
..Config::default()
}
);
Expand Down

0 comments on commit f7d16fe

Please sign in to comment.