Skip to content

Commit

Permalink
fix canonicalize on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
xmclark committed Oct 5, 2018
1 parent 210bf07 commit ef68e5b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 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.toml
Expand Up @@ -12,6 +12,7 @@ documentation = "https://rustwasm.github.io/wasm-pack/"
[dependencies]
atty = "0.2.11"
cargo_metadata = "0.6.0"
cfg-if = "0.1.5"
console = "0.6.1"
curl = "0.4.13"
failure = "0.1.2"
Expand Down
3 changes: 2 additions & 1 deletion src/command/build.rs
Expand Up @@ -2,6 +2,7 @@

use bindgen;
use build;
use command::utils::canonicalize_path;
use command::utils::{create_pkg_dir, set_crate_path};
use emoji;
use error::Error;
Expand Down Expand Up @@ -141,7 +142,7 @@ impl Build {
PBAR.message(&format!(
"{} Your wasm pkg is ready to publish at {:#?}.",
emoji::PACKAGE,
self.out_dir.canonicalize().unwrap_or(self.out_dir.clone())
canonicalize_path(self.out_dir.clone()).unwrap_or(self.out_dir.clone())
));
Ok(())
}
Expand Down
32 changes: 28 additions & 4 deletions src/command/utils.rs
Expand Up @@ -4,19 +4,17 @@ use emoji;
use error::Error;
use progressbar::Step;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use PBAR;

/// If an explicit path is given, then use it, otherwise assume the current
/// directory is the crate path.
pub fn set_crate_path(path: Option<PathBuf>) -> io::Result<PathBuf> {
pub fn set_crate_path(path: Option<PathBuf>) -> Result<PathBuf, Error> {
let crate_path = match path {
Some(p) => p,
None => PathBuf::from("."),
};

crate_path.canonicalize()
canonicalize_path(crate_path)
}

/// Construct our `pkg` directory in the crate.
Expand Down Expand Up @@ -44,3 +42,29 @@ pub fn find_pkg_directory(path: &Path) -> Option<PathBuf> {
fn is_pkg_directory(path: &Path) -> bool {
path.exists() && path.is_dir() && path.ends_with("pkg")
}

cfg_if! {
if #[cfg(windows)] {
/// Strips UNC from canonical path on Windows.
/// See https://github.com/rust-lang/rust/issues/42869 for why this is needed.
pub fn canonicalize_path(path: PathBuf) -> Result<PathBuf, Error> {
use std::ffi::OsString;
use std::os::windows::prelude::*;
let canonical = path.canonicalize()?;
let vec_chars = canonical.as_os_str().encode_wide().collect::<Vec<u16>>();
if vec_chars[0..4] == [92, 92, 63, 92] {
Ok(Path::new(&OsString::from_wide(&vec_chars[4..])).to_owned())
}
else {
Ok(canonical)
}
}
}
else {
/// Strips UNC from canonical path on Windows.
pub fn canonicalize_path(path: PathBuf) -> Result<PathBuf, Error> {
let canonical = path.canonicalize()?;
Ok(canonical)
}
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Expand Up @@ -3,6 +3,8 @@
#![deny(missing_docs)]

extern crate cargo_metadata;
#[macro_use]
extern crate cfg_if;
extern crate console;
extern crate curl;
#[macro_use]
Expand Down

0 comments on commit ef68e5b

Please sign in to comment.