Skip to content

Commit

Permalink
Use dunce
Browse files Browse the repository at this point in the history
  • Loading branch information
iho committed Oct 11, 2023
1 parent 4fde946 commit 3e102e2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cargo-near/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
16 changes: 6 additions & 10 deletions cargo-near/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<Utf8PathBuf> {
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.
Expand Down

0 comments on commit 3e102e2

Please sign in to comment.