Skip to content

Commit 3232263

Browse files
authored
fix(forge): skip scripts in get_sources_to_compile (#11540)
1 parent 961241c commit 3232263

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

crates/forge/src/cmd/test/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{
33
MultiContractRunner, MultiContractRunnerBuilder,
44
decode::decode_console_logs,
55
gas_report::GasReport,
6-
multi_runner::{is_test_contract, matches_artifact},
6+
multi_runner::matches_artifact,
77
result::{SuiteResult, TestOutcome, TestStatus},
88
traces::{
99
CallTraceDecoderBuilder, InternalTraceMode, TraceKind,
@@ -24,8 +24,8 @@ use foundry_common::{
2424
EmptyTestFilter, TestFunctionExt, compile::ProjectCompiler, evm::EvmArgs, fs, shell,
2525
};
2626
use foundry_compilers::{
27-
ProjectCompileOutput, artifacts::output_selection::OutputSelection,
28-
compilers::multi::MultiCompiler,
27+
Language, ProjectCompileOutput, artifacts::output_selection::OutputSelection,
28+
compilers::multi::MultiCompiler, multi::MultiCompilerLanguage, utils::source_files_iter,
2929
};
3030
use foundry_config::{
3131
Config, figment,
@@ -207,17 +207,18 @@ impl TestArgs {
207207
/// This means that it will return all sources that are not test contracts or that match the
208208
/// filter. We want to compile all non-test sources always because tests might depend on them
209209
/// dynamically through cheatcodes.
210-
///
211-
/// Returns `None` if all sources should be compiled.
212210
#[instrument(target = "forge::test", skip_all)]
213211
pub fn get_sources_to_compile(
214212
&self,
215213
config: &Config,
216214
test_filter: &ProjectPathsAwareFilter,
217-
) -> Result<Option<BTreeSet<PathBuf>>> {
215+
) -> Result<BTreeSet<PathBuf>> {
218216
// An empty filter doesn't filter out anything.
217+
// We can still optimize slightly by excluding scripts.
219218
if test_filter.is_empty() {
220-
return Ok(None);
219+
return Ok(source_files_iter(&config.src, MultiCompilerLanguage::FILE_EXTENSIONS)
220+
.chain(source_files_iter(&config.test, MultiCompilerLanguage::FILE_EXTENSIONS))
221+
.collect());
221222
}
222223

223224
let mut project = config.create_project(true, true)?;
@@ -230,15 +231,14 @@ impl TestArgs {
230231
eyre::bail!("Compilation failed");
231232
}
232233

233-
let sources = output
234+
Ok(output
234235
.artifact_ids()
235236
.filter_map(|(id, artifact)| artifact.abi.as_ref().map(|abi| (id, abi)))
236237
.filter(|(id, abi)| {
237-
!is_test_contract(abi.functions()) || matches_artifact(test_filter, id, abi)
238+
id.source.starts_with(&config.src) || matches_artifact(test_filter, id, abi)
238239
})
239240
.map(|(id, _)| id.source)
240-
.collect::<BTreeSet<_>>();
241-
Ok(Some(sources))
241+
.collect())
242242
}
243243

244244
/// Executes all the tests in the project.
@@ -275,7 +275,7 @@ impl TestArgs {
275275
let compiler = ProjectCompiler::new()
276276
.dynamic_test_linking(config.dynamic_test_linking)
277277
.quiet(shell::is_json() || self.junit)
278-
.files(self.get_sources_to_compile(&config, &filter)?.unwrap_or_default());
278+
.files(self.get_sources_to_compile(&config, &filter)?);
279279
let output = compiler.compile(&project)?;
280280

281281
// Create test options from general project settings and compiler output.

crates/forge/src/multi_runner.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -579,10 +579,3 @@ pub(crate) fn matches_contract(
579579
(filter.matches_path(path) && filter.matches_contract(contract_name))
580580
&& functions.into_iter().any(|func| filter.matches_test_function(func.borrow()))
581581
}
582-
583-
/// Returns `true` if the given contract is a test contract.
584-
pub(crate) fn is_test_contract(
585-
functions: impl IntoIterator<Item = impl std::borrow::Borrow<Function>>,
586-
) -> bool {
587-
functions.into_iter().any(|func| func.borrow().is_any_test())
588-
}

crates/forge/tests/cli/test_optimizer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ forgetest_init!(toggle_invalidate_cache_on_test, |prj, cmd| {
4141
// All files are built with optimized tests.
4242
cmd.args(["test"]).with_no_redact().assert_success().stdout_eq(str![[r#"
4343
...
44-
Compiling 23 files with [..]
44+
Compiling 21 files with [..]
4545
...
4646
4747
"#]]);
@@ -60,7 +60,7 @@ No files changed, compilation skipped
6060
// All files are rebuilt with preprocessed cache false.
6161
cmd.with_no_redact().assert_success().stdout_eq(str![[r#"
6262
...
63-
Compiling 23 files with [..]
63+
Compiling 21 files with [..]
6464
...
6565
6666
"#]]);

0 commit comments

Comments
 (0)