Skip to content

Commit 6547691

Browse files
authored
refactor(forge): use new ethers-solc functions in forge run (#776)
* test: add forge run test * refactor(forge): use new ethers-solc functions in forge run
1 parent 26b2256 commit 6547691

File tree

4 files changed

+60
-69
lines changed

4 files changed

+60
-69
lines changed

Cargo.lock

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/src/cmd/mod.rs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,8 @@ pub mod verify;
5454
use crate::opts::forge::ContractInfo;
5555
use ethers::{
5656
abi::Abi,
57-
prelude::{
58-
artifacts::{CompactBytecode, CompactDeployedBytecode},
59-
Graph,
60-
},
61-
solc::{artifacts::Source, cache::SolFilesCache},
57+
prelude::artifacts::{CompactBytecode, CompactDeployedBytecode},
58+
solc::cache::SolFilesCache,
6259
};
6360
use std::path::PathBuf;
6461

@@ -97,35 +94,14 @@ If you are in a subdirectory in a Git repository, try adding `--root .`"#,
9794
Ok(output)
9895
}
9996

100-
/// Manually compile a project with added sources
101-
pub fn manual_compile(
102-
project: &Project,
103-
added_sources: Vec<PathBuf>,
104-
) -> eyre::Result<ProjectCompileOutput> {
105-
let mut sources = project.paths.read_input_files()?;
106-
sources.extend(Source::read_all_files(added_sources)?);
97+
/// Compile a set of files not necessarily included in the `project`'s source dir
98+
pub fn compile_files(project: &Project, files: Vec<PathBuf>) -> eyre::Result<ProjectCompileOutput> {
10799
println!("compiling...");
108-
if project.auto_detect {
109-
tracing::trace!("using solc auto detection to compile sources");
110-
let output = project.svm_compile(sources)?;
111-
if output.has_compiler_errors() {
112-
// return the diagnostics error back to the user.
113-
eyre::bail!(output.to_string())
114-
}
115-
return Ok(output)
116-
}
117-
118-
let mut solc = project.solc.clone();
119-
if !project.allowed_lib_paths.is_empty() {
120-
solc = solc.arg("--allow-paths").arg(project.allowed_lib_paths.to_string());
121-
}
122-
123-
let (sources, _) = Graph::resolve_sources(&project.paths, sources)?.into_sources();
124-
let output = project.compile_with_version(&solc, sources)?;
100+
let output = project.compile_files(files)?;
125101
if output.has_compiler_errors() {
126-
// return the diagnostics error back to the user.
127102
eyre::bail!(output.to_string())
128103
}
104+
println!("success.");
129105
Ok(output)
130106
}
131107

cli/src/cmd/run.rs

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::cmd::{build::BuildArgs, compile, manual_compile, Cmd};
1+
use crate::cmd::{build::BuildArgs, compile_files, Cmd};
22
use clap::{Parser, ValueHint};
33
use evm_adapters::sputnik::cheatcodes::{CONSOLE_ABI, HEVMCONSOLE_ABI, HEVM_ABI};
44

@@ -247,7 +247,7 @@ impl Cmd for RunArgs {
247247
}
248248

249249
println!("Gas Used: {}", result.gas_used);
250-
println!("== Logs == ");
250+
println!("== Logs ==");
251251
result.logs.iter().for_each(|log| println!("{}", log));
252252
}
253253

@@ -275,32 +275,8 @@ impl RunArgs {
275275
/// Compiles the file with auto-detection and compiler params.
276276
pub fn build(&self, config: Config, evm_opts: &EvmOpts) -> eyre::Result<BuildOutput> {
277277
let target_contract = dunce::canonicalize(&self.path)?;
278-
let (project, output) = if let Ok(mut project) = config.project() {
279-
// TODO: caching causes no output until https://github.com/gakonst/ethers-rs/issues/727
280-
// is fixed
281-
project.cached = false;
282-
project.no_artifacts = true;
283-
284-
// target contract may not be in the compilation path, add it and manually compile
285-
match manual_compile(&project, vec![target_contract]) {
286-
Ok(output) => (project, output),
287-
Err(e) => {
288-
println!("No extra contracts compiled {:?}", e);
289-
let mut target_project = config.ephemeral_no_artifacts_project()?;
290-
target_project.cached = false;
291-
target_project.no_artifacts = true;
292-
let res = compile(&target_project)?;
293-
(target_project, res)
294-
}
295-
}
296-
} else {
297-
let mut target_project = config.ephemeral_no_artifacts_project()?;
298-
target_project.cached = false;
299-
target_project.no_artifacts = true;
300-
let res = compile(&target_project)?;
301-
(target_project, res)
302-
};
303-
println!("success.");
278+
let project = config.ephemeral_no_artifacts_project()?;
279+
let output = compile_files(&project, vec![target_contract])?;
304280

305281
let (sources, all_contracts) = output.output().split();
306282

cli/tests/cmd.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! Contains various tests for checking forge's commands
2+
use ansi_term::Colour;
23
use ethers::solc::{artifacts::Metadata, ConfigurableContractArtifact};
34
use evm_adapters::evm_opts::{EvmOpts, EvmType};
45
use foundry_cli_test_utils::{
@@ -316,6 +317,44 @@ success.
316317
);
317318
});
318319

320+
// tests that the `run` command works correctly
321+
forgetest!(can_execute_run_command, |prj: TestProject, mut cmd: TestCommand| {
322+
let script = prj
323+
.inner()
324+
.add_source(
325+
"Foo",
326+
r#"
327+
// SPDX-License-Identifier: UNLICENSED
328+
pragma solidity 0.8.10;
329+
contract Demo {
330+
event log_string(string);
331+
function run() external {
332+
emit log_string("script ran");
333+
}
334+
}
335+
"#,
336+
)
337+
.unwrap();
338+
339+
cmd.arg("run").arg(script);
340+
let output = cmd.stdout_lossy();
341+
assert_eq!(
342+
format!(
343+
"compiling...
344+
Compiling 1 files with 0.8.10
345+
Compilation finished successfully
346+
success.
347+
{}
348+
Gas Used: 1751
349+
== Logs ==
350+
script ran
351+
",
352+
Colour::Green.paint("Script ran successfully.")
353+
),
354+
output
355+
);
356+
});
357+
319358
// test against a local checkout, useful to debug with local ethers-rs patch
320359
forgetest_ignore!(can_compile_local_spells, |_: TestProject, mut cmd: TestCommand| {
321360
let current_dir = std::env::current_dir().unwrap();

0 commit comments

Comments
 (0)