From a675e7098f9c6252a9ae6e6af6ebf123013ce953 Mon Sep 17 00:00:00 2001 From: iho Date: Tue, 10 Oct 2023 02:48:08 +0300 Subject: [PATCH 01/11] fix: make it work on Windows --- cargo-near/src/util/mod.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cargo-near/src/util/mod.rs b/cargo-near/src/util/mod.rs index 7478bcd0..a1eb65b9 100644 --- a/cargo-near/src/util/mod.rs +++ b/cargo-near/src/util/mod.rs @@ -57,6 +57,13 @@ where if let Some(path) = working_dir { let path = path.as_ref(); log::debug!("Setting cargo working dir to '{}'", path); + #[cfg(target_os = "windows")] + { + let mut path = path.as_std_path().to_string_lossy().to_string(); + // remove first 4 elements from path string + path.drain(..4); + let path = std::path::PathBuf::from(path); + } cmd.current_dir(path); } From 4fde946d7d914d4a618d2c5d406e23661ec4b662 Mon Sep 17 00:00:00 2001 From: iho Date: Wed, 11 Oct 2023 23:01:57 +0300 Subject: [PATCH 02/11] Strip UNC prefix --- cargo-near/src/util/mod.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/cargo-near/src/util/mod.rs b/cargo-near/src/util/mod.rs index a1eb65b9..eeaa05a6 100644 --- a/cargo-near/src/util/mod.rs +++ b/cargo-near/src/util/mod.rs @@ -57,13 +57,6 @@ where if let Some(path) = working_dir { let path = path.as_ref(); log::debug!("Setting cargo working dir to '{}'", path); - #[cfg(target_os = "windows")] - { - let mut path = path.as_std_path().to_string_lossy().to_string(); - // remove first 4 elements from path string - path.drain(..4); - let path = std::path::PathBuf::from(path); - } cmd.current_dir(path); } @@ -245,10 +238,18 @@ pub(crate) fn compile_project( } } +const UNC_PREFIX: &str = r"\\?\"; /// Create the directory if it doesn't exist, and return the absolute path to it. pub(crate) fn force_canonicalize_dir(dir: &Utf8Path) -> color_eyre::eyre::Result { fs::create_dir_all(dir).wrap_err_with(|| format!("failed to create directory `{}`", dir))?; dir.canonicalize_utf8() + .map(|path| { + if path.starts_with(UNC_PREFIX) { + path.strip_prefix(UNC_PREFIX).unwrap().to_path_buf() + } else { + path + } + }) .wrap_err_with(|| format!("failed to access output directory `{}`", dir)) } From 3e102e2e0b7765b43e8a2e65ae8bbf83fc8fb689 Mon Sep 17 00:00:00 2001 From: iho Date: Wed, 11 Oct 2023 23:33:45 +0300 Subject: [PATCH 03/11] Use dunce --- Cargo.lock | 7 +++++++ cargo-near/Cargo.toml | 1 + cargo-near/src/util/mod.rs | 16 ++++++---------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 845860f6..70861865 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -656,6 +656,7 @@ dependencies = [ "color-eyre", "colored", "derive_more", + "dunce", "env_logger", "inquire", "interactive-clap", @@ -1312,6 +1313,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" +[[package]] +name = "dunce" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56ce8c6da7551ec6c462cbaf3bfbc75131ebbfa1c944aeaa9dab51ca1c5f0c3b" + [[package]] name = "dyn-clone" version = "1.0.14" diff --git a/cargo-near/Cargo.toml b/cargo-near/Cargo.toml index 74774bfa..b26c3307 100644 --- a/cargo-near/Cargo.toml +++ b/cargo-near/Cargo.toml @@ -42,3 +42,4 @@ interactive-clap-derive = "0.2.5" near-cli-rs = { version = "0.5.2" } derive_more = "0.99.9" shell-words = "1.0.0" +dunce = "1" \ No newline at end of file diff --git a/cargo-near/src/util/mod.rs b/cargo-near/src/util/mod.rs index eeaa05a6..795f2b10 100644 --- a/cargo-near/src/util/mod.rs +++ b/cargo-near/src/util/mod.rs @@ -56,6 +56,7 @@ where if let Some(path) = working_dir { let path = path.as_ref(); + let path = force_canonicalize_dir(path).unwrap(); log::debug!("Setting cargo working dir to '{}'", path); cmd.current_dir(path); } @@ -238,19 +239,14 @@ pub(crate) fn compile_project( } } -const UNC_PREFIX: &str = r"\\?\"; /// Create the directory if it doesn't exist, and return the absolute path to it. pub(crate) fn force_canonicalize_dir(dir: &Utf8Path) -> color_eyre::eyre::Result { fs::create_dir_all(dir).wrap_err_with(|| format!("failed to create directory `{}`", dir))?; - dir.canonicalize_utf8() - .map(|path| { - if path.starts_with(UNC_PREFIX) { - path.strip_prefix(UNC_PREFIX).unwrap().to_path_buf() - } else { - path - } - }) - .wrap_err_with(|| format!("failed to access output directory `{}`", dir)) + let compatible_path = dunce::canonicalize(&dir); + match compatible_path { + Ok(path) => Ok(Utf8PathBuf::from_path_buf(path).unwrap()), + Err(err) => Err(err).wrap_err_with(|| format!("failed to canonicalize path: {} ", dir)), + } } /// Copy a file to a destination. From 06dbb8d0aa86e08ee0830fbbac5b8c2680e54035 Mon Sep 17 00:00:00 2001 From: iho Date: Wed, 11 Oct 2023 23:39:12 +0300 Subject: [PATCH 04/11] Add comments --- cargo-near/src/util/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cargo-near/src/util/mod.rs b/cargo-near/src/util/mod.rs index 795f2b10..3b421873 100644 --- a/cargo-near/src/util/mod.rs +++ b/cargo-near/src/util/mod.rs @@ -56,6 +56,7 @@ where if let Some(path) = working_dir { let path = path.as_ref(); + // remove UNC prefix to be able compile on Windows let path = force_canonicalize_dir(path).unwrap(); log::debug!("Setting cargo working dir to '{}'", path); cmd.current_dir(path); @@ -242,6 +243,7 @@ pub(crate) fn compile_project( /// Create the directory if it doesn't exist, and return the absolute path to it. pub(crate) fn force_canonicalize_dir(dir: &Utf8Path) -> color_eyre::eyre::Result { fs::create_dir_all(dir).wrap_err_with(|| format!("failed to create directory `{}`", dir))?; + // use `dunce` create instead of default one because it's compatible with Windows let compatible_path = dunce::canonicalize(&dir); match compatible_path { Ok(path) => Ok(Utf8PathBuf::from_path_buf(path).unwrap()), From ad0708a1862b908448cbbbdc67b416565a89d3f3 Mon Sep 17 00:00:00 2001 From: iho Date: Wed, 11 Oct 2023 23:40:10 +0300 Subject: [PATCH 05/11] Add comments --- cargo-near/src/util/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cargo-near/src/util/mod.rs b/cargo-near/src/util/mod.rs index 3b421873..bbf0f76e 100644 --- a/cargo-near/src/util/mod.rs +++ b/cargo-near/src/util/mod.rs @@ -243,7 +243,9 @@ pub(crate) fn compile_project( /// Create the directory if it doesn't exist, and return the absolute path to it. pub(crate) fn force_canonicalize_dir(dir: &Utf8Path) -> color_eyre::eyre::Result { fs::create_dir_all(dir).wrap_err_with(|| format!("failed to create directory `{}`", dir))?; - // use `dunce` create instead of default one because it's compatible with Windows + // use `dunce` create instead of default one because it's compatible with Windows UNC paths + // and don't breake cargo compilation on Windows + // https://github.com/rust-lang/rust/issues/42869 let compatible_path = dunce::canonicalize(&dir); match compatible_path { Ok(path) => Ok(Utf8PathBuf::from_path_buf(path).unwrap()), From 52ea1a1ecb297b36dc4909497753daa3d35be06c Mon Sep 17 00:00:00 2001 From: iho Date: Wed, 11 Oct 2023 23:42:01 +0300 Subject: [PATCH 06/11] clippy --- cargo-near/src/util/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cargo-near/src/util/mod.rs b/cargo-near/src/util/mod.rs index bbf0f76e..af385a96 100644 --- a/cargo-near/src/util/mod.rs +++ b/cargo-near/src/util/mod.rs @@ -246,7 +246,7 @@ pub(crate) fn force_canonicalize_dir(dir: &Utf8Path) -> color_eyre::eyre::Result // use `dunce` create instead of default one because it's compatible with Windows UNC paths // and don't breake cargo compilation on Windows // https://github.com/rust-lang/rust/issues/42869 - let compatible_path = dunce::canonicalize(&dir); + let compatible_path = dunce::canonicalize(dir); match compatible_path { Ok(path) => Ok(Utf8PathBuf::from_path_buf(path).unwrap()), Err(err) => Err(err).wrap_err_with(|| format!("failed to canonicalize path: {} ", dir)), From 85a39cc83dcfa2f2dd227419771df7c7e0eff725 Mon Sep 17 00:00:00 2001 From: iho Date: Wed, 11 Oct 2023 23:43:39 +0300 Subject: [PATCH 07/11] Fix doc --- cargo-near/src/util/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cargo-near/src/util/mod.rs b/cargo-near/src/util/mod.rs index af385a96..d7ba444f 100644 --- a/cargo-near/src/util/mod.rs +++ b/cargo-near/src/util/mod.rs @@ -56,7 +56,7 @@ where if let Some(path) = working_dir { let path = path.as_ref(); - // remove UNC prefix to be able compile on Windows + // remove UNC prefix to be able to compile on Windows let path = force_canonicalize_dir(path).unwrap(); log::debug!("Setting cargo working dir to '{}'", path); cmd.current_dir(path); @@ -243,7 +243,7 @@ pub(crate) fn compile_project( /// Create the directory if it doesn't exist, and return the absolute path to it. pub(crate) fn force_canonicalize_dir(dir: &Utf8Path) -> color_eyre::eyre::Result { fs::create_dir_all(dir).wrap_err_with(|| format!("failed to create directory `{}`", dir))?; - // use `dunce` create instead of default one because it's compatible with Windows UNC paths + // use canonicalize from `dunce` create instead of default one from std because it's compatible with Windows UNC paths // and don't breake cargo compilation on Windows // https://github.com/rust-lang/rust/issues/42869 let compatible_path = dunce::canonicalize(dir); From bae7fe52890b6d219b2b3c83a1db74aa2a376fc1 Mon Sep 17 00:00:00 2001 From: iho Date: Wed, 11 Oct 2023 23:55:24 +0300 Subject: [PATCH 08/11] Replace unwrap with ? --- cargo-near/src/util/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cargo-near/src/util/mod.rs b/cargo-near/src/util/mod.rs index d7ba444f..6396a707 100644 --- a/cargo-near/src/util/mod.rs +++ b/cargo-near/src/util/mod.rs @@ -57,7 +57,7 @@ where if let Some(path) = working_dir { let path = path.as_ref(); // remove UNC prefix to be able to compile on Windows - let path = force_canonicalize_dir(path).unwrap(); + let path = force_canonicalize_dir(path)?; log::debug!("Setting cargo working dir to '{}'", path); cmd.current_dir(path); } From 09d217bc6d6ca370a2d0c4b8d4de77d169acc3a4 Mon Sep 17 00:00:00 2001 From: iho Date: Thu, 12 Oct 2023 00:28:38 +0300 Subject: [PATCH 09/11] Update cargo-near/src/util/mod.rs Co-authored-by: Vlad Frolov --- cargo-near/src/util/mod.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cargo-near/src/util/mod.rs b/cargo-near/src/util/mod.rs index 6396a707..fdadf97b 100644 --- a/cargo-near/src/util/mod.rs +++ b/cargo-near/src/util/mod.rs @@ -246,11 +246,12 @@ pub(crate) fn force_canonicalize_dir(dir: &Utf8Path) -> color_eyre::eyre::Result // use canonicalize from `dunce` create instead of default one from std because it's compatible with Windows UNC paths // and don't breake cargo compilation on Windows // https://github.com/rust-lang/rust/issues/42869 - let compatible_path = dunce::canonicalize(dir); - match compatible_path { - Ok(path) => Ok(Utf8PathBuf::from_path_buf(path).unwrap()), - Err(err) => Err(err).wrap_err_with(|| format!("failed to canonicalize path: {} ", dir)), - } + Utf8PathBuf::from_path_buf( + dunce::canonicalize(dir) + .wrap_err_with(|| format!("failed to canonicalize path: {} ", dir))? + ) + .wrap_err_with(|| format!("failed to convert canonicalized path to UTF-8 path: {} ", dir)) + .into() } /// Copy a file to a destination. From 298182f98e000feba48f590df37eb5e863b326ef Mon Sep 17 00:00:00 2001 From: iho Date: Thu, 12 Oct 2023 00:42:13 +0300 Subject: [PATCH 10/11] fix eyre error --- cargo-near/src/util/mod.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cargo-near/src/util/mod.rs b/cargo-near/src/util/mod.rs index fdadf97b..cb2b9b7d 100644 --- a/cargo-near/src/util/mod.rs +++ b/cargo-near/src/util/mod.rs @@ -248,10 +248,9 @@ pub(crate) fn force_canonicalize_dir(dir: &Utf8Path) -> color_eyre::eyre::Result // https://github.com/rust-lang/rust/issues/42869 Utf8PathBuf::from_path_buf( dunce::canonicalize(dir) - .wrap_err_with(|| format!("failed to canonicalize path: {} ", dir))? + .wrap_err_with(|| format!("failed to canonicalize path: {} ", dir))?, ) - .wrap_err_with(|| format!("failed to convert canonicalized path to UTF-8 path: {} ", dir)) - .into() + .map_err(|err| color_eyre::eyre::eyre!(" failed to convert path {}", err.to_string_lossy())) } /// Copy a file to a destination. From 208cef385d83493e3abfcbdcc3a6d56242294c65 Mon Sep 17 00:00:00 2001 From: Vlad Frolov Date: Wed, 11 Oct 2023 23:47:40 +0200 Subject: [PATCH 11/11] Apply suggestions from code review --- cargo-near/src/util/mod.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/cargo-near/src/util/mod.rs b/cargo-near/src/util/mod.rs index cb2b9b7d..5c9ec441 100644 --- a/cargo-near/src/util/mod.rs +++ b/cargo-near/src/util/mod.rs @@ -55,9 +55,7 @@ where cmd.envs(env); if let Some(path) = working_dir { - let path = path.as_ref(); - // remove UNC prefix to be able to compile on Windows - let path = force_canonicalize_dir(path)?; + let path = force_canonicalize_dir(path.as_ref())?; log::debug!("Setting cargo working dir to '{}'", path); cmd.current_dir(path); } @@ -244,13 +242,13 @@ pub(crate) fn compile_project( pub(crate) fn force_canonicalize_dir(dir: &Utf8Path) -> color_eyre::eyre::Result { fs::create_dir_all(dir).wrap_err_with(|| format!("failed to create directory `{}`", dir))?; // use canonicalize from `dunce` create instead of default one from std because it's compatible with Windows UNC paths - // and don't breake cargo compilation on Windows + // and don't break cargo compilation on Windows // https://github.com/rust-lang/rust/issues/42869 Utf8PathBuf::from_path_buf( dunce::canonicalize(dir) .wrap_err_with(|| format!("failed to canonicalize path: {} ", dir))?, ) - .map_err(|err| color_eyre::eyre::eyre!(" failed to convert path {}", err.to_string_lossy())) + .map_err(|err| color_eyre::eyre::eyre!("failed to convert path {}", err.to_string_lossy())) } /// Copy a file to a destination.