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(coverage): caching for coverage #9366

Draft
wants to merge 25 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
39c5922
fix(`coverage`): write coverage artifacts to separate dir
yash-atreya Nov 20, 2024
215ac18
Merge branch 'master' into yash/fix-8840
yash-atreya Nov 21, 2024
20b09e8
adjust cache path for coverage
yash-atreya Nov 21, 2024
581cf4c
partial test
yash-atreya Nov 21, 2024
a0d7e19
enable cache
yash-atreya Nov 21, 2024
c59364a
don't assume recompilation
yash-atreya Nov 21, 2024
66cfb3d
fix(`coverage`): dont recompile when cache exists
yash-atreya Nov 21, 2024
730b26d
Merge branch 'master' into yash/fix-8840
yash-atreya Nov 21, 2024
0eadcfa
account for cached compiled artifacts at once
yash-atreya Nov 22, 2024
9902adf
test with multi solc versions
yash-atreya Nov 22, 2024
0433863
fix(`forge clean`): remove cache/coverage dir
yash-atreya Nov 22, 2024
e90a354
add test
klkvr Nov 22, 2024
7d639e6
fix(`coverage`): account for `build_id` in source identification (#9…
yash-atreya Nov 27, 2024
2b1af19
fix
yash-atreya Nov 29, 2024
498e427
rm version as key in report
yash-atreya Nov 29, 2024
06f01dc
rm version key from ContractId and report.items
yash-atreya Nov 29, 2024
38db689
fix: scale anchor.items_id
yash-atreya Nov 29, 2024
a6edce9
fix: rm dependence of coverage on version
yash-atreya Nov 29, 2024
8297739
fix: respect artifact.build_id while processing hitmaps
yash-atreya Nov 29, 2024
13d3fd7
fix: respect element idx
yash-atreya Nov 29, 2024
973cfe0
key source_ids by version and path
yash-atreya Dec 2, 2024
2eaab46
Merge branch 'master' into yash/fix-8840
yash-atreya Dec 2, 2024
1624587
nit
yash-atreya Dec 2, 2024
860ff71
Merge branch 'master' into yash/fix-8840
yash-atreya Dec 13, 2024
54be7b9
Merge branch 'master' into yash/fix-8840
yash-atreya Dec 20, 2024
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
Prev Previous commit
Next Next commit
don't assume recompilation
yash-atreya committed Nov 21, 2024
commit c59364a061bb47f7fafe71b3f997ad0e718111c0
48 changes: 27 additions & 21 deletions crates/forge/bin/cmd/coverage.rs
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ use foundry_cli::utils::{LoadConfig, STATIC_FUZZ_SEED};
use foundry_common::{compile::ProjectCompiler, fs};
use foundry_compilers::{
artifacts::{sourcemap::SourceMap, CompactBytecode, CompactDeployedBytecode, SolcLanguage},
Artifact, ArtifactId, Project, ProjectCompileOutput,
output, Artifact, ArtifactId, Project, ProjectCompileOutput,
};
use foundry_config::{Config, SolcReq};
use rayon::prelude::*;
@@ -162,29 +162,35 @@ impl CoverageArgs {
// Collect source files.
let project_paths = &project.paths;
let mut versioned_sources = HashMap::<Version, SourceFiles<'_>>::default();
for (path, source_file, version) in output.output().sources.sources_with_version() {
report.add_source(version.clone(), source_file.id as usize, path.clone());

// Filter out dependencies
if !self.include_libs && project_paths.has_library_ancestor(path) {
continue;
}
if !output.output().sources.is_empty() {
// Freshly compiled sources
for (path, source_file, version) in output.output().sources.sources_with_version() {
report.add_source(version.clone(), source_file.id as usize, path.clone());

// Filter out dependencies
if !self.include_libs && project_paths.has_library_ancestor(path) {
continue;
}

if let Some(ast) = &source_file.ast {
let file = project_paths.root.join(path);
trace!(root=?project_paths.root, ?file, "reading source file");

let source = SourceFile {
ast,
source: fs::read_to_string(&file)
.wrap_err("Could not read source code for analysis")?,
};
versioned_sources
.entry(version.clone())
.or_default()
.sources
.insert(source_file.id as usize, source);
if let Some(ast) = &source_file.ast {
let file = project_paths.root.join(path);
trace!(root=?project_paths.root, ?file, "reading source file");

let source = SourceFile {
ast,
source: fs::read_to_string(&file)
.wrap_err("Could not read source code for analysis")?,
};
versioned_sources
.entry(version.clone())
.or_default()
.sources
.insert(source_file.id as usize, source);
}
}
} else {
// Cached sources
}

// Get source maps and bytecodes
10 changes: 10 additions & 0 deletions crates/forge/tests/cli/coverage.rs
Original file line number Diff line number Diff line change
@@ -1513,4 +1513,14 @@ contract AContractTest is DSTest {
);

// TODO: forge coverage - Should not compile again.
cmd.forge_fuse().arg("coverage").args(["--summary".to_string()]).assert_success().stdout_eq(
str![[r#"
| File | % Lines | % Statements | % Branches | % Funcs |
|-------------------|---------------|---------------|---------------|---------------|
| src/AContract.sol | 100.00% (2/2) | 100.00% (2/2) | 100.00% (0/0) | 100.00% (2/2) |
| Total | 100.00% (2/2) | 100.00% (2/2) | 100.00% (0/0) | 100.00% (2/2) |
"#]],
);
});