1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-21 23:04:45 -05:00

fix(cli/fmt): Strip "\\?\" prefix when displaying Windows paths (#8135)

This commit is contained in:
Nayeem Rahman 2020-10-30 02:19:03 +00:00 committed by GitHub
parent 1854c6f73b
commit 6be6c517d0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 31 additions and 18 deletions

View file

@ -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)?);
};
}
}

View file

@ -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()

View file

@ -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()?
};

View file

@ -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

View file

@ -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!(

View file

@ -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!(