fix: cleanup lock file after exec cargo metadata (#431)

* fix: cleanup lock file after exec cargo metadata

* fix typos
This commit is contained in:
Matthias Seitz 2021-09-05 12:19:08 +02:00 committed by GitHub
parent 32ad5a6abd
commit 32c75ab1f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 13 deletions

View File

@ -38,26 +38,47 @@ pub fn ethers_providers_crate() -> Path {
/// | ethers_contract`, we need to use the fitting crate ident when expand the /// | 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 /// macros This will attempt to parse the current `Cargo.toml` and check the
/// ethers related dependencies. /// 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) { 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() let res = MetadataCommand::new()
.manifest_path(&format!( .manifest_path(&format!("{}/Cargo.toml", manifest_dir))
"{}/Cargo.toml",
std::env::var("CARGO_MANIFEST_DIR").expect("No Manifest found")
))
.no_deps()
.exec() .exec()
.ok() .ok()
.and_then(|metadata| { .and_then(|metadata| {
metadata.packages[0] metadata.root_package().and_then(|pkg| {
.dependencies pkg.dependencies
.iter() .iter()
.filter(|dep| dep.kind == DependencyKind::Normal) .filter(|dep| dep.kind == DependencyKind::Normal)
.find_map(|dep| { .find_map(|dep| {
(dep.name == "ethers") (dep.name == "ethers")
.then(|| ("ethers::core", "ethers::contract", "ethers::providers")) .then(|| ("ethers::core", "ethers::contract", "ethers::providers"))
}) })
})
}) })
.unwrap_or(("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 res
} }