summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2020-10-30 02:19:03 +0000
committerGitHub <noreply@github.com>2020-10-30 03:19:03 +0100
commit6be6c517d0691bf1d14eef67668e575c84cdfdf4 (patch)
treefd6b37f2807f78325a08fff5e34166d6eec84ca7
parent1854c6f73be9d4439807911ce9cba1125af93dd4 (diff)
fix(cli/fmt): Strip "\\?\" prefix when displaying Windows paths (#8135)
-rw-r--r--cli/fmt.rs7
-rw-r--r--cli/fs.rs16
-rw-r--r--cli/installer.rs5
-rw-r--r--cli/ops/fs.rs15
-rw-r--r--cli/tsc.rs3
-rw-r--r--cli/tsc_config.rs3
6 files changed, 31 insertions, 18 deletions
diff --git a/cli/fmt.rs b/cli/fmt.rs
index 3f97282eb..a932fbc95 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 8298db6d8..9c0be1bf2 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<T: AsRef<[u8]>>(
file.write_all(data.as_ref())
}
+/// Similar to `std::fs::canonicalize()` but strips UNC prefixes on Windows.
+pub fn canonicalize_path(path: &Path) -> Result<PathBuf, Error> {
+ 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<PathBuf, AnyError> {
let resolved_path = if path.is_absolute() {
path.to_owned()
diff --git a/cli/installer.rs b/cli/installer.rs
index 969572e1b..4a177b3ea 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<PathBuf, io::Error> {
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 204d447be..720a4db52 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 801ac8e8f..303868d44 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 93e9dddb2..52daf2e46 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!(