Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
fix: cleanup lock file after exec cargo metadata (#431)
Browse files Browse the repository at this point in the history
* fix: cleanup lock file after exec cargo metadata

* fix typos
  • Loading branch information
mattsse authored Sep 5, 2021
1 parent 32ad5a6 commit 32c75ab
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions ethers-contract/ethers-contract-abigen/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,47 @@ pub fn ethers_providers_crate() -> Path {
/// | ethers_contract`, we need to use the fitting crate ident when expand the
/// macros This will attempt to parse the current `Cargo.toml` and check the
/// ethers related dependencies.
///
/// This process is a bit hacky, we run `cargo metadata` internally which
/// resolves the current package but creates a new `Cargo.lock` file in the
/// process. This is not a problem for regular workspaces but becomes an issue
/// during publishing with `cargo publish` if the project does not ignore
/// `Cargo.lock` in `.gitignore`, because then cargo can't proceed with
/// publishing the crate because the created `Cargo.lock` leads to a modified
/// workspace, not the `CARGO_MANIFEST_DIR` but the workspace `cargo publish`
/// created in `./target/package/..`. Therefore we check prior to executing
/// `cargo metadata` if a `Cargo.lock` file exists and delete it afterwards if
/// it was created by `cargo metadata`.
pub fn determine_ethers_crates() -> (&'static str, &'static str, &'static str) {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("No Manifest found");

// check if the lock file exists, if it's missing we need to clean up afterward
let lock_file = format!("{}/Cargo.lock", manifest_dir);
let needs_lock_file_cleanup = !std::path::Path::new(&lock_file).exists();

let res = MetadataCommand::new()
.manifest_path(&format!(
"{}/Cargo.toml",
std::env::var("CARGO_MANIFEST_DIR").expect("No Manifest found")
))
.no_deps()
.manifest_path(&format!("{}/Cargo.toml", manifest_dir))
.exec()
.ok()
.and_then(|metadata| {
metadata.packages[0]
.dependencies
.iter()
.filter(|dep| dep.kind == DependencyKind::Normal)
.find_map(|dep| {
(dep.name == "ethers")
.then(|| ("ethers::core", "ethers::contract", "ethers::providers"))
})
metadata.root_package().and_then(|pkg| {
pkg.dependencies
.iter()
.filter(|dep| dep.kind == DependencyKind::Normal)
.find_map(|dep| {
(dep.name == "ethers")
.then(|| ("ethers::core", "ethers::contract", "ethers::providers"))
})
})
})
.unwrap_or(("ethers_core", "ethers_contract", "ethers_providers"));

if needs_lock_file_cleanup {
// delete the `Cargo.lock` file that was created by `cargo metadata`
// if the package is not part of a workspace
let _ = std::fs::remove_file(lock_file);
}

res
}

Expand Down

0 comments on commit 32c75ab

Please sign in to comment.