Skip to content

Commit

Permalink
chore(config): remove RootPath (#9448)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Dec 2, 2024
1 parent d4e91c8 commit 8b8d1cd
Show file tree
Hide file tree
Showing 14 changed files with 41 additions and 75 deletions.
8 changes: 4 additions & 4 deletions crates/cheatcodes/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl CheatsConfig {
running_contract: Option<String>,
running_version: Option<Version>,
) -> Self {
let mut allowed_paths = vec![config.root.0.clone()];
let mut allowed_paths = vec![config.root.clone()];
allowed_paths.extend(config.libs.clone());
allowed_paths.extend(config.allow_paths.clone());

Expand All @@ -88,8 +88,8 @@ impl CheatsConfig {
rpc_endpoints,
paths: config.project_paths(),
fs_permissions: config.fs_permissions.clone().joined(config.root.as_ref()),
root: config.root.0.clone(),
broadcast: config.root.0.clone().join(&config.broadcast),
root: config.root.clone(),
broadcast: config.root.clone().join(&config.broadcast),
allowed_paths,
evm_opts,
labels: config.labels.clone(),
Expand Down Expand Up @@ -239,7 +239,7 @@ mod tests {

fn config(root: &str, fs_permissions: FsPermissions) -> CheatsConfig {
CheatsConfig::new(
&Config { root: PathBuf::from(root).into(), fs_permissions, ..Default::default() },
&Config { root: root.into(), fs_permissions, ..Default::default() },
Default::default(),
None,
None,
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/opts/build/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl<'a> From<&'a CoreBuildArgs> for Config {
// if `--config-path` is set we need to adjust the config's root path to the actual root
// path for the project, otherwise it will the parent dir of the `--config-path`
if args.project_paths.config_path.is_some() {
config.root = args.project_paths.project_root().into();
config.root = args.project_paths.project_root();
}
config
}
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl<'a> Git<'a> {

#[inline]
pub fn from_config(config: &'a Config) -> Self {
Self::new(config.root.0.as_path())
Self::new(config.root.as_path())
}

pub fn root_of(relative_to: &Path) -> Result<PathBuf> {
Expand Down
80 changes: 24 additions & 56 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ pub struct Config {
/// See `profile`.
#[serde(skip)]
pub profiles: Vec<Profile>,

/// The root path where the config detection started from, [`Config::with_root`].
// We're skipping serialization here, so it won't be included in the [`Config::to_string()`]
// representation, but will be deserialized from the `Figment` so that forge commands can
// override it.
#[serde(default = "root_default", skip_serializing)]
pub root: PathBuf,

/// path of the source contracts dir, like `src` or `contracts`
pub src: PathBuf,
/// path of the test dir
Expand Down Expand Up @@ -466,13 +474,6 @@ pub struct Config {
/// Soldeer custom configs
pub soldeer: Option<SoldeerConfig>,

/// The root path where the config detection started from, [`Config::with_root`].
// We're skipping serialization here, so it won't be included in the [`Config::to_string()`]
// representation, but will be deserialized from the `Figment` so that forge commands can
// override it.
#[serde(default, skip_serializing)]
pub root: RootPath,

/// Whether failed assertions should revert.
///
/// Note that this only applies to native (cheatcode) assertions, invoked on Vm contract.
Expand Down Expand Up @@ -679,7 +680,7 @@ impl Config {
return Figment::from(self);
}

let root = self.root.0.as_path();
let root = self.root.as_path();
let profile = Self::selected_profile();
let mut figment = Figment::default().merge(DappHardhatDirProvider(root));

Expand Down Expand Up @@ -760,7 +761,7 @@ impl Config {
/// This joins all relative paths with the current root and attempts to make them canonic
#[must_use]
pub fn canonic(self) -> Self {
let root = self.root.0.clone();
let root = self.root.clone();
self.canonic_at(root)
}

Expand Down Expand Up @@ -1006,7 +1007,7 @@ impl Config {
.set_no_artifacts(no_artifacts);

if !self.skip.is_empty() {
let filter = SkipBuildFilters::new(self.skip.clone(), self.root.0.clone());
let filter = SkipBuildFilters::new(self.skip.clone(), self.root.clone());
builder = builder.sparse_output(filter);
}

Expand Down Expand Up @@ -1057,7 +1058,7 @@ impl Config {
fn ensure_solc(&self) -> Result<Option<Solc>, SolcError> {
if self.eof {
let (tx, rx) = mpsc::channel();
let root = self.root.0.clone();
let root = self.root.clone();
std::thread::spawn(move || {
tx.send(
Solc::new_with_args(
Expand Down Expand Up @@ -1167,7 +1168,7 @@ impl Config {
.artifacts(&self.out)
.libs(self.libs.iter())
.remappings(self.get_all_remappings())
.allowed_path(&self.root.0)
.allowed_path(&self.root)
.allowed_paths(&self.libs)
.allowed_paths(&self.allow_paths)
.include_paths(&self.include_paths);
Expand All @@ -1176,7 +1177,7 @@ impl Config {
builder = builder.build_infos(build_info_path);
}

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

/// Returns configuration for a compiler to use when setting up a [Project].
Expand Down Expand Up @@ -1428,7 +1429,7 @@ impl Config {

/// Returns the remapping for the project's _test_ directory, but only if it exists
pub fn get_test_dir_remapping(&self) -> Option<Remapping> {
if self.root.0.join(&self.test).exists() {
if self.root.join(&self.test).exists() {
get_dir_remapping(&self.test)
} else {
None
Expand All @@ -1437,7 +1438,7 @@ impl Config {

/// Returns the remapping for the project's _script_ directory, but only if it exists
pub fn get_script_dir_remapping(&self) -> Option<Remapping> {
if self.root.0.join(&self.script).exists() {
if self.root.join(&self.script).exists() {
get_dir_remapping(&self.script)
} else {
None
Expand Down Expand Up @@ -1615,7 +1616,7 @@ impl Config {
let paths = ProjectPathsConfig::builder().build_with_root::<()>(root);
let artifacts: PathBuf = paths.artifacts.file_name().unwrap().into();
Self {
root: paths.root.into(),
root: paths.root,
src: paths.sources.file_name().unwrap().into(),
out: artifacts.clone(),
libs: paths.libraries.into_iter().map(|lib| lib.file_name().unwrap().into()).collect(),
Expand Down Expand Up @@ -1707,7 +1708,7 @@ impl Config {
pub fn update_libs(&self) -> eyre::Result<()> {
self.update(|doc| {
let profile = self.profile.as_str().as_str();
let root = &self.root.0;
let root = &self.root;
let libs: toml_edit::Value = self
.libs
.iter()
Expand Down Expand Up @@ -1764,7 +1765,7 @@ impl Config {

/// Returns the path to the `foundry.toml` of this `Config`.
pub fn get_config_path(&self) -> PathBuf {
self.root.0.join(Self::FILE_NAME)
self.root.join(Self::FILE_NAME)
}

/// Returns the selected profile.
Expand Down Expand Up @@ -2211,43 +2212,6 @@ pub(crate) mod from_opt_glob {
}
}

/// A helper wrapper around the root path used during Config detection
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct RootPath(pub PathBuf);

impl Default for RootPath {
fn default() -> Self {
".".into()
}
}

impl<P: Into<PathBuf>> From<P> for RootPath {
fn from(p: P) -> Self {
Self(p.into())
}
}

impl AsRef<Path> for RootPath {
fn as_ref(&self) -> &Path {
&self.0
}
}

impl std::ops::Deref for RootPath {
type Target = PathBuf;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl std::ops::DerefMut for RootPath {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

/// Parses a config profile
///
/// All `Profile` date is ignored by serde, however the `Config::to_string_pretty` includes it and
Expand Down Expand Up @@ -2299,7 +2263,7 @@ impl Default for Config {
profiles: vec![Self::DEFAULT_PROFILE],
fs_permissions: FsPermissions::new([PathPermission::read("out")]),
isolate: cfg!(feature = "isolate-by-default"),
root: Default::default(),
root: root_default(),
src: "src".into(),
test: "test".into(),
script: "script".into(),
Expand Down Expand Up @@ -3068,6 +3032,10 @@ fn canonic(path: impl Into<PathBuf>) -> PathBuf {
foundry_compilers::utils::canonicalize(&path).unwrap_or(path)
}

fn root_default() -> PathBuf {
".".into()
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/bind_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl BindJsonArgs {
let config = self.try_load_config_emit_warnings()?;
let project = config.create_project(false, true)?;

let target_path = config.root.0.join(self.out.as_ref().unwrap_or(&config.bind_json.out));
let target_path = config.root.join(self.out.as_ref().unwrap_or(&config.bind_json.out));

let sources = project.paths.read_input_files()?;
let graph = Graph::<MultiCompilerParsedSource>::resolve_sources(&project.paths, sources)?;
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl BuildArgs {
// directories as well as the `foundry.toml` configuration file.
self.watch.watchexec_config(|| {
let config = Config::from(self);
let foundry_toml: PathBuf = config.root.0.join(Config::FILE_NAME);
let foundry_toml: PathBuf = config.root.join(Config::FILE_NAME);
[config.src, config.test, config.script, foundry_toml]
})
}
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub struct DocArgs {
impl DocArgs {
pub async fn run(self) -> Result<()> {
let config = self.config()?;
let root = &config.root.0;
let root = &config.root;
let project = config.project()?;
let compiler = ProjectCompiler::new().quiet(true);
let _output = compiler.compile(&project)?;
Expand Down
6 changes: 2 additions & 4 deletions crates/forge/bin/cmd/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl FmtArgs {
let config = self.try_load_config_emit_warnings()?;

// Expand ignore globs and canonicalize from the get go
let ignored = expand_globs(&config.root.0, config.fmt.ignore.iter())?
let ignored = expand_globs(&config.root, config.fmt.ignore.iter())?
.iter()
.flat_map(foundry_common::fs::canonicalize_path)
.collect::<Vec<_>>();
Expand Down Expand Up @@ -96,9 +96,7 @@ impl FmtArgs {

let format = |source: String, path: Option<&Path>| -> Result<_> {
let name = match path {
Some(path) => {
path.strip_prefix(&config.root.0).unwrap_or(path).display().to_string()
}
Some(path) => path.strip_prefix(&config.root).unwrap_or(path).display().to_string(),
None => "stdin".to_string(),
};

Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ impl TestArgs {

if self.decode_internal.is_some() {
let sources =
ContractSources::from_project_output(output, &config.root.0, Some(&libraries))?;
ContractSources::from_project_output(output, &config.root, Some(&libraries))?;
builder = builder.with_debug_identifier(DebugTraceIdentifier::new(sources));
}
let mut decoder = builder.build();
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl UpdateArgs {
/// Returns `(root, paths)` where `root` is the root of the Git repository and `paths` are the
/// relative paths of the dependencies.
pub fn dependencies_paths(deps: &[Dependency], config: &Config) -> Result<(PathBuf, Vec<PathBuf>)> {
let git_root = Git::root_of(&config.root.0)?;
let git_root = Git::root_of(&config.root)?;
let libs = config.install_lib_dir();

let mut paths = Vec::with_capacity(deps.len());
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ pub async fn watch_test(args: TestArgs) -> Result<()> {
args.watch.run_all;

let last_test_files = Mutex::new(HashSet::<String>::default());
let project_root = config.root.0.to_string_lossy().into_owned();
let project_root = config.root.to_string_lossy().into_owned();
let config = args.watch.watchexec_config_with_override(
|| [&config.test, &config.src],
move |events, command| {
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/tests/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ forgetest!(can_extract_config_values, |prj, cmd| {
profile: Config::DEFAULT_PROFILE,
// `profiles` is not serialized.
profiles: vec![],
root: Default::default(),
root: ".".into(),
src: "test-src".into(),
test: "test-test".into(),
script: "test-script".into(),
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/tests/it/repros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ test_repro!(6538);

// https://github.com/foundry-rs/foundry/issues/6554
test_repro!(6554; |config| {
let path = config.runner.config.root.0.join("out/default/Issue6554.t.sol");
let path = config.runner.config.root.join("out/default/Issue6554.t.sol");

let mut prj_config = Config::clone(&config.runner.config);
prj_config.fs_permissions.add(PathPermission::read_write(path));
Expand Down
2 changes: 1 addition & 1 deletion crates/script/src/simulate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ impl FilledTransactionsState {
)?)
};

let commit = get_commit_hash(&self.script_config.config.root.0);
let commit = get_commit_hash(&self.script_config.config.root);

let libraries = self
.build_data
Expand Down

0 comments on commit 8b8d1cd

Please sign in to comment.