From 6be6c517d0691bf1d14eef67668e575c84cdfdf4 Mon Sep 17 00:00:00 2001 From: Nayeem Rahman Date: Fri, 30 Oct 2020 02:19:03 +0000 Subject: [PATCH] fix(cli/fmt): Strip "\\?\" prefix when displaying Windows paths (#8135) --- cli/fmt.rs | 7 ++++--- cli/fs.rs | 16 +++++++++++++++- cli/installer.rs | 5 +++-- cli/ops/fs.rs | 15 +++++---------- cli/tsc.rs | 3 ++- cli/tsc_config.rs | 3 ++- 6 files changed, 31 insertions(+), 18 deletions(-) diff --git a/cli/fmt.rs b/cli/fmt.rs index 3f97282ebe..a932fbc95a 100644 --- a/cli/fmt.rs +++ b/cli/fmt.rs @@ -9,6 +9,7 @@ use crate::colors; use crate::diff::diff; +use crate::fs::canonicalize_path; use crate::fs::files_in_subtree; use crate::text_encoding; use deno_core::error::generic_error; @@ -238,16 +239,16 @@ pub fn collect_files( if files.is_empty() { target_files.extend(files_in_subtree( - std::env::current_dir()?.canonicalize()?, + canonicalize_path(&std::env::current_dir()?)?, is_supported, )); } else { for file in files { if file.is_dir() { target_files - .extend(files_in_subtree(file.canonicalize()?, is_supported)); + .extend(files_in_subtree(canonicalize_path(&file)?, is_supported)); } else { - target_files.push(file.canonicalize()?); + target_files.push(canonicalize_path(&file)?); }; } } diff --git a/cli/fs.rs b/cli/fs.rs index 8298db6d86..9c0be1bf2d 100644 --- a/cli/fs.rs +++ b/cli/fs.rs @@ -4,7 +4,7 @@ use deno_core::error::AnyError; pub use deno_core::normalize_path; use std::env::current_dir; use std::fs::OpenOptions; -use std::io::Write; +use std::io::{Error, Write}; use std::path::{Path, PathBuf}; use walkdir::WalkDir; @@ -47,6 +47,20 @@ pub fn write_file_2>( file.write_all(data.as_ref()) } +/// Similar to `std::fs::canonicalize()` but strips UNC prefixes on Windows. +pub fn canonicalize_path(path: &Path) -> Result { + let mut canonicalized_path = path.canonicalize()?; + if cfg!(windows) { + canonicalized_path = PathBuf::from( + canonicalized_path + .display() + .to_string() + .trim_start_matches("\\\\?\\"), + ); + } + Ok(canonicalized_path) +} + pub fn resolve_from_cwd(path: &Path) -> Result { let resolved_path = if path.is_absolute() { path.to_owned() diff --git a/cli/installer.rs b/cli/installer.rs index 969572e1b0..4a177b3eab 100644 --- a/cli/installer.rs +++ b/cli/installer.rs @@ -1,6 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. use crate::flags::Flags; +use crate::fs::canonicalize_path; use deno_core::error::generic_error; use deno_core::error::AnyError; use deno_core::url::Url; @@ -84,7 +85,7 @@ deno {} "$@" fn get_installer_root() -> Result { if let Ok(env_dir) = env::var("DENO_INSTALL_ROOT") { if !env_dir.is_empty() { - return PathBuf::from(env_dir).canonicalize(); + return canonicalize_path(&PathBuf::from(env_dir)); } } // Note: on Windows, the $HOME environment variable may be set by users or by @@ -127,7 +128,7 @@ pub fn install( force: bool, ) -> Result<(), AnyError> { let root = if let Some(root) = root { - root.canonicalize()? + canonicalize_path(&root)? } else { get_installer_root()? }; diff --git a/cli/ops/fs.rs b/cli/ops/fs.rs index 204d447be4..720a4db52f 100644 --- a/cli/ops/fs.rs +++ b/cli/ops/fs.rs @@ -2,6 +2,7 @@ // Some deserializer fields are only used on Unix and Windows build fails without it use super::io::std_file_resource; use super::io::{FileMetadata, StreamResource, StreamResourceHolder}; +use crate::fs::canonicalize_path; use crate::permissions::Permissions; use deno_core::error::custom_error; use deno_core::error::type_error; @@ -934,11 +935,8 @@ fn op_realpath_sync( debug!("op_realpath_sync {}", path.display()); // corresponds to the realpath on Unix and // CreateFile and GetFinalPathNameByHandle on Windows - let realpath = std::fs::canonicalize(&path)?; - let mut realpath_str = into_string(realpath.into_os_string())?; - if cfg!(windows) { - realpath_str = realpath_str.trim_start_matches("\\\\?\\").to_string(); - } + let realpath = canonicalize_path(&path)?; + let realpath_str = into_string(realpath.into_os_string())?; Ok(json!(realpath_str)) } @@ -963,11 +961,8 @@ async fn op_realpath_async( debug!("op_realpath_async {}", path.display()); // corresponds to the realpath on Unix and // CreateFile and GetFinalPathNameByHandle on Windows - let realpath = std::fs::canonicalize(&path)?; - let mut realpath_str = into_string(realpath.into_os_string())?; - if cfg!(windows) { - realpath_str = realpath_str.trim_start_matches("\\\\?\\").to_string(); - } + let realpath = canonicalize_path(&path)?; + let realpath_str = into_string(realpath.into_os_string())?; Ok(json!(realpath_str)) }) .await diff --git a/cli/tsc.rs b/cli/tsc.rs index 801ac8e8f3..303868d442 100644 --- a/cli/tsc.rs +++ b/cli/tsc.rs @@ -7,6 +7,7 @@ use crate::disk_cache::DiskCache; use crate::file_fetcher::SourceFile; use crate::file_fetcher::SourceFileFetcher; use crate::flags::Flags; +use crate::fs::canonicalize_path; use crate::js; use crate::media_type::MediaType; use crate::module_graph::ModuleGraph; @@ -174,7 +175,7 @@ impl CompilerConfig { // Convert the PathBuf to a canonicalized string. This is needed by the // compiler to properly deal with the configuration. - let config_path = config_file.canonicalize().map_err(|_| { + let config_path = canonicalize_path(&config_file).map_err(|_| { io::Error::new( io::ErrorKind::InvalidInput, format!( diff --git a/cli/tsc_config.rs b/cli/tsc_config.rs index 93e9dddb2b..52daf2e462 100644 --- a/cli/tsc_config.rs +++ b/cli/tsc_config.rs @@ -1,5 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +use crate::fs::canonicalize_path; use deno_core::error::AnyError; use deno_core::serde_json; use deno_core::serde_json::json; @@ -266,7 +267,7 @@ impl TsConfig { if let Some(path) = maybe_path { let cwd = std::env::current_dir()?; let config_file = cwd.join(path); - let config_path = config_file.canonicalize().map_err(|_| { + let config_path = canonicalize_path(&config_file).map_err(|_| { std::io::Error::new( std::io::ErrorKind::InvalidInput, format!(