Skip to content

Commit

Permalink
fix(config)!: fix remappings logic and logging (#125)
Browse files Browse the repository at this point in the history
* fix(config): fix remappings logic and logging

During the `update` command, the remappings were not correctly generated
if no remappings already existed (such as when the user manually deletes
the remappings.txt file)

* refactor(config): pass config path as argument to remappings functions

This eases testing

* style: fix clippy warning

* fix: remove debug print statements
  • Loading branch information
beeb authored Aug 5, 2024
1 parent eb20e25 commit b9a5929
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 50 deletions.
90 changes: 60 additions & 30 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,24 +329,25 @@ pub enum RemappingsAction {

pub async fn remappings_txt(
dependency: &RemappingsAction,
config_path: impl AsRef<Path>,
soldeer_config: &SoldeerConfig,
) -> Result<()> {
let remappings_path = get_current_working_dir().join("remappings.txt");
if soldeer_config.remappings_regenerate {
remove_file(&remappings_path).map_err(ConfigError::RemappingsError)?;
}
let contents = match remappings_path.exists() {
true => read_file_to_string(&remappings_path),
false => "".to_string(),
};
let existing_remappings = contents.lines().filter_map(|r| r.split_once('=')).collect();

if !remappings_path.exists() {
File::create(remappings_path.clone()).unwrap();
}

let new_remappings = match dependency {
RemappingsAction::None => generate_remappings(dependency, soldeer_config, vec![])?,
_ => {
let contents = read_file_to_string(&remappings_path);
let existing_remappings = contents.lines().filter_map(|r| r.split_once('=')).collect();
generate_remappings(dependency, soldeer_config, existing_remappings)?
}
};
let new_remappings =
generate_remappings(dependency, config_path, soldeer_config, existing_remappings)?;

let mut file = File::create(remappings_path)?;
for remapping in new_remappings {
Expand All @@ -373,7 +374,8 @@ pub async fn remappings_foundry(
let Some(Some(remappings)) = profile.get_mut("remappings").map(|v| v.as_array_mut()) else {
// except the default profile, where we always add the remappings
if name == "default" {
let new_remappings = generate_remappings(dependency, soldeer_config, vec![])?;
let new_remappings =
generate_remappings(dependency, &config_path, soldeer_config, vec![])?;
let array = Array::from_iter(new_remappings.into_iter());
profile["remappings"] = value(array);
}
Expand All @@ -384,7 +386,8 @@ pub async fn remappings_foundry(
.filter_map(|r| r.as_str())
.filter_map(|r| r.split_once('='))
.collect();
let new_remappings = generate_remappings(dependency, soldeer_config, existing_remappings)?;
let new_remappings =
generate_remappings(dependency, &config_path, soldeer_config, existing_remappings)?;
remappings.clear();
for remapping in new_remappings {
remappings.push(remapping);
Expand Down Expand Up @@ -499,25 +502,35 @@ fn parse_dependency(name: impl Into<String>, value: &Item) -> Result<Dependency>
}
}

fn remappings_from_deps(
config_path: impl AsRef<Path>,
soldeer_config: &SoldeerConfig,
) -> Result<Vec<String>> {
let config_path = config_path.as_ref().to_path_buf();
let dependencies = read_config_deps(Some(config_path))?;
Ok(dependencies
.iter()
.map(|dependency| {
let dependency_name_formatted = format_remap_name(soldeer_config, dependency);
format!(
"{dependency_name_formatted}=dependencies/{}-{}/",
dependency.name(),
dependency.version()
)
})
.collect())
}

fn generate_remappings(
dependency: &RemappingsAction,
config_path: impl AsRef<Path>,
soldeer_config: &SoldeerConfig,
existing_remappings: Vec<(&str, &str)>,
) -> Result<Vec<String>> {
let mut new_remappings = Vec::new();
if soldeer_config.remappings_regenerate {
let dependencies = read_config_deps(None)?;

dependencies.iter().for_each(|dependency| {
let dependency_name_formatted = format_remap_name(soldeer_config, dependency);

println!("{}", format!("Adding {dependency} to remappings").green());
new_remappings.push(format!(
"{dependency_name_formatted}=dependencies/{}-{}/",
dependency.name(),
dependency.version()
));
});
new_remappings = remappings_from_deps(config_path, soldeer_config)?;
println!("{}", "Added all dependencies to remapppings".green());
} else {
match &dependency {
RemappingsAction::Remove(remove_dep) => {
Expand Down Expand Up @@ -551,8 +564,27 @@ fn generate_remappings(
}
}
RemappingsAction::None => {
for (remapped, orig) in existing_remappings {
new_remappings.push(format!("{}={}", remapped, orig));
// This is where we end up in the `update` command if we don't want to re-generate
// all remappings. We need to merge existing remappings with the full list of deps.
// We generate all remappings from the dependencies, then replace existing items.
new_remappings = remappings_from_deps(config_path, soldeer_config)?;
if !existing_remappings.is_empty() {
for item in new_remappings.iter_mut() {
let (item_remap, item_orig) =
item.split_once('=').expect("remappings should have two parts");
// try to find an existing item with the same path
if let Some((remapped, orig)) =
existing_remappings.iter().find(|(_, o)| item_orig == *o)
{
*item = format!("{}={}", remapped, orig);
} else {
println!(
"{}",
format!("Added {} to remappings", item_remap.trim_end_matches('/'))
.green()
);
}
}
}
}
}
Expand Down Expand Up @@ -1875,7 +1907,8 @@ remappings_version = true
checksum: None,
});
let soldeer_config = read_soldeer_config(Some(target_config.clone())).unwrap();
let _ = remappings_txt(&RemappingsAction::Add(dependency), &soldeer_config).await;
let _ = remappings_txt(&RemappingsAction::Add(dependency), &target_config, &soldeer_config)
.await;

content = "@dep1-1.0.0/=dependencies/dep1-1.0.0/\n";

Expand Down Expand Up @@ -1910,7 +1943,8 @@ remappings_version = false
checksum: None,
});
let soldeer_config = read_soldeer_config(Some(target_config.clone())).unwrap();
let _ = remappings_txt(&RemappingsAction::Add(dependency), &soldeer_config).await;
let _ = remappings_txt(&RemappingsAction::Add(dependency), &target_config, &soldeer_config)
.await;

content = "dep1/=dependencies/dep1-1.0.0/\n";

Expand Down Expand Up @@ -2081,10 +2115,6 @@ remappings_regenerate = true
"#;

let target_config = define_config(true);
unsafe {
// became unsafe in Rust 1.80
env::set_var("config_file", target_config.to_string_lossy().to_string());
}

write_to_config(&target_config, content);

Expand Down
49 changes: 29 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ pub async fn run(command: Subcommands) -> Result<(), SoldeerError> {

Subcommands::Uninstall(uninstall) => {
// define the config file
let path = get_config_path()?;
let config_path = get_config_path()?;

// delete from the config file and return the dependency
let dependency = delete_config(&uninstall.dependency, &path)?;
let dependency = delete_config(&uninstall.dependency, &config_path)?;

// deleting the files
delete_dependency_files(&dependency).map_err(|e| SoldeerError::DownloadError {
Expand All @@ -160,25 +160,31 @@ pub async fn run(command: Subcommands) -> Result<(), SoldeerError> {
// removing the dependency from the lock file
remove_lock(&dependency)?;

let config = read_soldeer_config(Some(path.clone()))?;
let config = read_soldeer_config(Some(config_path.clone()))?;

if config.remappings_generate {
if path.to_string_lossy().contains("foundry.toml") {
if config_path.to_string_lossy().contains("foundry.toml") {
match config.remappings_location {
RemappingsLocation::Txt => {
remappings_txt(&RemappingsAction::Remove(dependency), &config).await?
remappings_txt(
&RemappingsAction::Remove(dependency),
&config_path,
&config,
)
.await?
}
RemappingsLocation::Config => {
remappings_foundry(
&RemappingsAction::Remove(dependency),
&path,
&config_path,
&config,
)
.await?
}
}
} else {
remappings_txt(&RemappingsAction::Remove(dependency), &config).await?;
remappings_txt(&RemappingsAction::Remove(dependency), &config_path, &config)
.await?;
}
}
}
Expand All @@ -197,14 +203,14 @@ async fn install_dependency(
) -> Result<(), SoldeerError> {
lock_check(&dependency, true)?;

let config_file = match get_config_path() {
let config_path = match get_config_path() {
Ok(file) => file,
Err(e) => {
cleanup_dependency(&dependency, true)?;
return Err(e.into());
}
};
add_to_config(&dependency, &config_file)?;
add_to_config(&dependency, &config_path)?;

let result = download_dependency(&dependency, false)
.await
Expand All @@ -226,7 +232,7 @@ async fn install_dependency(
}
}

let mut config = read_soldeer_config(Some(config_file.clone()))?;
let mut config = read_soldeer_config(Some(config_path.clone()))?;
if regenerate_remappings {
config.remappings_regenerate = regenerate_remappings;
}
Expand All @@ -236,18 +242,19 @@ async fn install_dependency(
janitor::cleanup_dependency(&dependency, false)?;

if config.remappings_generate {
if config_file.to_string_lossy().contains("foundry.toml") {
if config_path.to_string_lossy().contains("foundry.toml") {
match config.remappings_location {
RemappingsLocation::Txt => {
remappings_txt(&RemappingsAction::Add(dependency), &config).await?
remappings_txt(&RemappingsAction::Add(dependency), &config_path, &config)
.await?
}
RemappingsLocation::Config => {
remappings_foundry(&RemappingsAction::Add(dependency), &config_file, &config)
remappings_foundry(&RemappingsAction::Add(dependency), &config_path, &config)
.await?
}
}
} else {
remappings_txt(&RemappingsAction::Add(dependency), &config).await?;
remappings_txt(&RemappingsAction::Add(dependency), &config_path, &config).await?;
}
}

Expand All @@ -257,8 +264,8 @@ async fn install_dependency(
async fn update(regenerate_remappings: bool) -> Result<(), SoldeerError> {
println!("{}", "🦌 Running Soldeer update 🦌".green());

let config_file = get_config_path()?;
let mut config = read_soldeer_config(Some(config_file.clone()))?;
let config_path = get_config_path()?;
let mut config = read_soldeer_config(Some(config_path.clone()))?;
if regenerate_remappings {
config.remappings_regenerate = regenerate_remappings;
}
Expand Down Expand Up @@ -289,15 +296,17 @@ async fn update(regenerate_remappings: bool) -> Result<(), SoldeerError> {
cleanup_after(&dependencies)?;

if config.remappings_generate {
if config_file.to_string_lossy().contains("foundry.toml") {
if config_path.to_string_lossy().contains("foundry.toml") {
match config.remappings_location {
RemappingsLocation::Txt => remappings_txt(&RemappingsAction::None, &config).await?,
RemappingsLocation::Txt => {
remappings_txt(&RemappingsAction::None, &config_path, &config).await?
}
RemappingsLocation::Config => {
remappings_foundry(&RemappingsAction::None, &config_file, &config).await?
remappings_foundry(&RemappingsAction::None, &config_path, &config).await?
}
}
} else {
remappings_txt(&RemappingsAction::None, &config).await?;
remappings_txt(&RemappingsAction::None, &config_path, &config).await?;
}
}

Expand Down

0 comments on commit b9a5929

Please sign in to comment.