From 2aca29c0ff5914e704dd2070956575fa6818e483 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Thu, 19 May 2022 08:05:28 +0200 Subject: [PATCH 1/6] Copy `_Cargo.toml` to `Cargo.toml` instead of moving it --- build.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/build.rs b/build.rs index e0d516e1..a80201bb 100644 --- a/build.rs +++ b/build.rs @@ -111,9 +111,9 @@ fn zip_template_and_build_dylint_driver( } let tmp_name = ink_dylint_driver_dir.join("Cargo.toml"); - std::fs::rename(&original_name, &tmp_name).map_err(|err| { + std::fs::copy(&original_name, &tmp_name).map_err(|err| { anyhow::anyhow!( - "Failed renaming '{:?}' to '{:?}': {:?}", + "Failed copying '{:?}' to '{:?}': {:?}", original_name, tmp_name, err @@ -126,14 +126,13 @@ fn zip_template_and_build_dylint_driver( dylint_driver_dst_file, ); - // After the build process of `ink_linting` happened we need to name back to the original - // `_Cargo.toml` name, otherwise the directory would be "dirty" and `cargo publish` would - // fail with `Source directory was modified by build.rs during cargo publish`. - std::fs::rename(&tmp_name, &original_name).map_err(|err| { + // After the build process of `ink_linting` happened we need to remove the `Cargo.toml` file. + // Otherwise the directory would be "dirty" and `cargo publish` would fail with `Source + // directory was modified by build.rs during cargo publish`. + std::fs::remove_file(&tmp_name).map_err(|err| { anyhow::anyhow!( - "Failed renaming '{:?}' to '{:?}': {:?}", + "Failed removing '{:?}': {:?}", tmp_name, - original_name, err ) })?; -- GitLab From 137abeb708ece755c934c348232133d0b3e01628 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Thu, 19 May 2022 08:12:15 +0200 Subject: [PATCH 2/6] Update changelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77622ff6..7137253f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Updated `cargo contract new` template dependencies to ink! `3` - [#569](https://github.com/paritytech/cargo-contract/pull/569) +### Fixed +- Fix dirty directory issue when crate installation had been interrupted - [#571](https://github.com/paritytech/cargo-contract/pull/571) + ## [1.3.0] - 2022-05-09 ### Added -- GitLab From 771f635f67cad6bb163fdb364a2f7e29a3d044dd Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Thu, 19 May 2022 08:16:10 +0200 Subject: [PATCH 3/6] Apply `cargo fmt` --- build.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/build.rs b/build.rs index a80201bb..5b1f7654 100644 --- a/build.rs +++ b/build.rs @@ -129,13 +129,8 @@ fn zip_template_and_build_dylint_driver( // After the build process of `ink_linting` happened we need to remove the `Cargo.toml` file. // Otherwise the directory would be "dirty" and `cargo publish` would fail with `Source // directory was modified by build.rs during cargo publish`. - std::fs::remove_file(&tmp_name).map_err(|err| { - anyhow::anyhow!( - "Failed removing '{:?}': {:?}", - tmp_name, - err - ) - })?; + std::fs::remove_file(&tmp_name) + .map_err(|err| anyhow::anyhow!("Failed removing '{:?}': {:?}", tmp_name, err))?; res } -- GitLab From d9ee22282b86f576eb4cb84d30329e69a4b07f33 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Thu, 19 May 2022 11:12:34 +0200 Subject: [PATCH 4/6] Implement `Drop` guard structure to ensure cleanup --- build.rs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/build.rs b/build.rs index 5b1f7654..ea3cf7c2 100644 --- a/build.rs +++ b/build.rs @@ -64,6 +64,17 @@ fn main() { } } +/// Holds the path to a file meant to be temporary. +struct TmpFileGuard(PathBuf); + +impl Drop for TmpFileGuard { + // Once the struct instance is dropped we remove the file. + fn drop(&mut self) { + std::fs::remove_file(&self.0) + .unwrap_or_else(|err| panic!("Failed removing '{:?}': {:?}", self.0, err)) + } +} + /// This method: /// * Creates a zip archive of the `new` project template. /// * Builds the `dylint` driver found in `ink_linting`, the compiled @@ -120,19 +131,12 @@ fn zip_template_and_build_dylint_driver( ) })?; - let res = build_and_zip_dylint_driver( - ink_dylint_driver_dir, - out_dir, - dylint_driver_dst_file, - ); - // After the build process of `ink_linting` happened we need to remove the `Cargo.toml` file. // Otherwise the directory would be "dirty" and `cargo publish` would fail with `Source // directory was modified by build.rs during cargo publish`. - std::fs::remove_file(&tmp_name) - .map_err(|err| anyhow::anyhow!("Failed removing '{:?}': {:?}", tmp_name, err))?; + let _guard = TmpFileGuard(tmp_name); - res + build_and_zip_dylint_driver(ink_dylint_driver_dir, out_dir, dylint_driver_dst_file) } /// Creates a zip archive `template.zip` of the `new` project template in `out_dir`. -- GitLab From 7b619ce1dff0a895754294d62350133c90ffe78c Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Thu, 19 May 2022 11:32:20 +0200 Subject: [PATCH 5/6] Implement `FileGuard` cleaner --- build.rs | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/build.rs b/build.rs index ea3cf7c2..f3dea428 100644 --- a/build.rs +++ b/build.rs @@ -64,17 +64,6 @@ fn main() { } } -/// Holds the path to a file meant to be temporary. -struct TmpFileGuard(PathBuf); - -impl Drop for TmpFileGuard { - // Once the struct instance is dropped we remove the file. - fn drop(&mut self) { - std::fs::remove_file(&self.0) - .unwrap_or_else(|err| panic!("Failed removing '{:?}': {:?}", self.0, err)) - } -} - /// This method: /// * Creates a zip archive of the `new` project template. /// * Builds the `dylint` driver found in `ink_linting`, the compiled @@ -134,7 +123,7 @@ fn zip_template_and_build_dylint_driver( // After the build process of `ink_linting` happened we need to remove the `Cargo.toml` file. // Otherwise the directory would be "dirty" and `cargo publish` would fail with `Source // directory was modified by build.rs during cargo publish`. - let _guard = TmpFileGuard(tmp_name); + let _guard = tmp_file_guard::FileGuard::new(tmp_name); build_and_zip_dylint_driver(ink_dylint_driver_dir, out_dir, dylint_driver_dst_file) } @@ -417,3 +406,30 @@ fn check_dylint_link_installed() -> Result<()> { } Ok(()) } + +mod tmp_file_guard { + use std::path::PathBuf; + + /// Holds the path to a file meant to be temporary. + pub struct FileGuard { + path: PathBuf, + } + + impl FileGuard { + /// Create a new new file guard. + /// + /// Once the object instance is dropped the file will be removed automatically. + pub fn new(path: PathBuf) -> Self { + Self { path } + } + } + + impl Drop for FileGuard { + // Once the struct instance is dropped we remove the file. + fn drop(&mut self) { + std::fs::remove_file(&self.path).unwrap_or_else(|err| { + panic!("Failed removing '{:?}': {:?}", self.path, err) + }) + } + } +} -- GitLab From a7dfc19470a32afeb32f1d2023cf0e4a4a748893 Mon Sep 17 00:00:00 2001 From: Michael Mueller Date: Thu, 19 May 2022 16:46:23 +0200 Subject: [PATCH 6/6] Improvements to `FileGuard` --- build.rs | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/build.rs b/build.rs index f3dea428..bbb7baad 100644 --- a/build.rs +++ b/build.rs @@ -106,24 +106,12 @@ fn zip_template_and_build_dylint_driver( // // (from https://doc.rust-lang.org/cargo/reference/manifest.html#the-exclude-and-include-fields) let original_name = ink_dylint_driver_dir.join("_Cargo.toml"); - if !original_name.exists() { - anyhow::bail!("'{:?}' does not exist", original_name); - } - - let tmp_name = ink_dylint_driver_dir.join("Cargo.toml"); - std::fs::copy(&original_name, &tmp_name).map_err(|err| { - anyhow::anyhow!( - "Failed copying '{:?}' to '{:?}': {:?}", - original_name, - tmp_name, - err - ) - })?; // After the build process of `ink_linting` happened we need to remove the `Cargo.toml` file. // Otherwise the directory would be "dirty" and `cargo publish` would fail with `Source // directory was modified by build.rs during cargo publish`. - let _guard = tmp_file_guard::FileGuard::new(tmp_name); + let tmp_name = ink_dylint_driver_dir.join("Cargo.toml"); + let _guard = tmp_file_guard::FileGuard::new(original_name, tmp_name); build_and_zip_dylint_driver(ink_dylint_driver_dir, out_dir, dylint_driver_dst_file) } @@ -419,8 +407,14 @@ mod tmp_file_guard { /// Create a new new file guard. /// /// Once the object instance is dropped the file will be removed automatically. - pub fn new(path: PathBuf) -> Self { - Self { path } + pub fn new(original_name: PathBuf, tmp_path: PathBuf) -> Self { + std::fs::copy(&original_name, &tmp_path).unwrap_or_else(|err| { + panic!( + "Failed copying '{:?}' to '{:?}': {:?}", + original_name, tmp_path, err + ) + }); + Self { path: tmp_path } } } -- GitLab