Skip to content

fix(forge): warn if file for artifact no longer exists #10025

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

Merged
merged 5 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 18 additions & 1 deletion crates/evm/traces/src/debug/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use foundry_linking::Linker;
use rayon::prelude::*;
use solar_parse::{interface::Session, Parser};
use std::{
collections::{BTreeMap, HashMap},
collections::{BTreeMap, HashMap, HashSet},
fmt::Write,
ops::Range,
path::{Path, PathBuf},
sync::Arc,
Expand Down Expand Up @@ -181,8 +182,14 @@ impl ContractSources {
// Not all source files produce artifacts, so we are populating sources by using build
// infos.
let mut files: BTreeMap<PathBuf, Arc<SourceData>> = BTreeMap::new();
let mut removed_files = HashSet::new();
for (build_id, build) in output.builds() {
for (source_id, path) in &build.source_id_to_path {
if !path.exists() {
removed_files.insert(path);
continue;
}

let source_data = match files.entry(path.clone()) {
std::collections::btree_map::Entry::Vacant(entry) => {
let source = Source::read(path).wrap_err_with(|| {
Expand All @@ -206,6 +213,16 @@ impl ContractSources {
}
}

if !removed_files.is_empty() {
let mut warning = "Detected artifacts built from source files that no longer exist. \
Run `forge clean` to make sure builds are in sync with project files."
.to_string();
for file in removed_files {
write!(warning, "\n - {}", file.display())?;
}
let _ = sh_warn!("{}", warning);
}

Ok(())
}

Expand Down
31 changes: 30 additions & 1 deletion crates/forge/tests/cli/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use foundry_test_utils::{
};
use regex::Regex;
use serde_json::Value;
use std::{env, path::PathBuf, str::FromStr};
use std::{env, fs, path::PathBuf, str::FromStr};

// Tests that fork cheat codes can be used in script
forgetest_init!(
Expand Down Expand Up @@ -2614,5 +2614,34 @@ SIMULATION COMPLETE. To broadcast these transactions, add --broadcast and wallet
[SAVED_SENSITIVE_VALUES]


"#]]);
});

// Tests warn when artifact source file no longer exists.
// <https://github.com/foundry-rs/foundry/issues/9068>
forgetest_init!(should_warn_if_artifact_source_no_longer_exists, |prj, cmd| {
cmd.args(["script", "script/Counter.s.sol"]).assert_success().stdout_eq(str![[r#"
...
Script ran successfully.
...

"#]]);
fs::rename(
prj.paths().scripts.join("Counter.s.sol"),
prj.paths().scripts.join("Counter1.s.sol"),
)
.unwrap();
cmd.forge_fuse().args(["script", "script/Counter1.s.sol"]).assert_success().stderr_eq(str![[r#"
...
Warning: Detected artifacts built from source files that no longer exist. Run `forge clean` to make sure builds are in sync with project files.
- [..]script/Counter.s.sol
...

"#]])
.stdout_eq(str![[r#"
...
Script ran successfully.
...

"#]]);
});