diff --git a/Cargo.lock b/Cargo.lock index 9a41e6534ea..2bf1e0d05ad 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -287,6 +287,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "dunce" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0ad6bf6a88548d1126045c413548df1453d9be094a8ab9fd59bf1fdd338da4f" + [[package]] name = "either" version = "1.5.3" @@ -1046,6 +1052,7 @@ version = "2.0.0-rc.1" dependencies = [ "anyhow", "cargo_metadata", + "dunce", "env_logger", "lazy_static", "log", @@ -1068,6 +1075,7 @@ dependencies = [ "bytecount", "diff", "dirs", + "dunce", "ignore", "itertools", "lazy_static", diff --git a/Cargo.toml b/Cargo.toml index a3c33c836cf..a741d6adf9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,6 +39,7 @@ generic-simd = ["rustfmt_lib/generic-simd"] [dependencies] anyhow = "1.0" +dunce = "1.0" env_logger = "0.7" log = "0.4" structopt = "0.3" diff --git a/rustfmt-core/rustfmt-bin/Cargo.toml b/rustfmt-core/rustfmt-bin/Cargo.toml index 401467957ac..3d8e033957c 100644 --- a/rustfmt-core/rustfmt-bin/Cargo.toml +++ b/rustfmt-core/rustfmt-bin/Cargo.toml @@ -31,6 +31,7 @@ generic-simd = ["rustfmt_lib/generic-simd"] ansi_term = "0.12" anyhow = "1.0" cargo_metadata = "0.9" +dunce = "1.0" env_logger = "0.7" getopts = "0.2" log = "0.4" diff --git a/rustfmt-core/rustfmt-bin/src/bin/main.rs b/rustfmt-core/rustfmt-bin/src/bin/main.rs index 4f0f7f3010d..512fd311107 100644 --- a/rustfmt-core/rustfmt-bin/src/bin/main.rs +++ b/rustfmt-core/rustfmt-bin/src/bin/main.rs @@ -14,7 +14,7 @@ use structopt::StructOpt; use thiserror::Error; use rustfmt_lib::{ - absolute_path, load_config, CliOptions, Config, Edition, EmitMode, FileLines, FileName, + load_config, CliOptions, Config, Edition, EmitMode, FileLines, FileName, FormatReportFormatterBuilder, Input, Session, Verbosity, }; @@ -252,7 +252,7 @@ enum OptError { impl Opt { fn canonicalize(&mut self) { for f in &mut self.files { - if let Ok(canonical_path) = absolute_path(&f) { + if let Ok(canonical_path) = dunce::canonicalize(&f) { *f = canonical_path; } } diff --git a/rustfmt-core/rustfmt-bin/src/cargo-fmt/main.rs b/rustfmt-core/rustfmt-bin/src/cargo-fmt/main.rs index bb92084ffcd..57c2d5933c8 100644 --- a/rustfmt-core/rustfmt-bin/src/cargo-fmt/main.rs +++ b/rustfmt-core/rustfmt-bin/src/cargo-fmt/main.rs @@ -13,7 +13,6 @@ use std::path::{Path, PathBuf}; use std::process::Command; use std::str; -use rustfmt_lib::absolute_path; use structopt::StructOpt; #[derive(StructOpt, Debug)] @@ -289,7 +288,7 @@ impl Target { nested_int_test_files: Option>, ) -> Self { let path = PathBuf::from(&target.src_path); - let canonicalized = absolute_path(&path).unwrap_or(path); + let canonicalized = dunce::canonicalize(&path).unwrap_or(path); let test_files = nested_int_test_files.unwrap_or_else(Vec::new); Self { @@ -389,14 +388,14 @@ fn get_targets_root_only( include_nested_test_files: bool, ) -> Result<(), io::Error> { let metadata = get_cargo_metadata(manifest_path, false)?; - let workspace_root_path = absolute_path(PathBuf::from(&metadata.workspace_root))?; + let workspace_root_path = dunce::canonicalize(PathBuf::from(&metadata.workspace_root))?; let (in_workspace_root, current_dir_manifest) = if let Some(target_manifest) = manifest_path { ( - workspace_root_path == target_manifest, - absolute_path(target_manifest)?, + workspace_root_path.as_path() == target_manifest, + dunce::canonicalize(target_manifest)?, ) } else { - let current_dir = absolute_path(env::current_dir()?)?; + let current_dir = dunce::canonicalize(env::current_dir()?)?; ( workspace_root_path == current_dir, current_dir.join("Cargo.toml"), @@ -414,7 +413,7 @@ fn get_targets_root_only( .into_iter() .filter(|p| { in_workspace_root - || absolute_path(PathBuf::from(&p.manifest_path)).unwrap_or_default() + || dunce::canonicalize(PathBuf::from(&p.manifest_path)).unwrap_or_default() == current_dir_manifest }) .map(|p| p.targets) @@ -1051,7 +1050,7 @@ mod cargo_fmt_tests { edition: &str, ) -> Target { let path = PathBuf::from(src_path); - let canonicalized = absolute_path(&path).unwrap_or(path); + let canonicalized = dunce::canonicalize(&path).unwrap_or(path); Target { path: canonicalized, kind: String::from(kind), diff --git a/rustfmt-core/rustfmt-lib/Cargo.toml b/rustfmt-core/rustfmt-lib/Cargo.toml index ae9a309604d..bac246be139 100644 --- a/rustfmt-core/rustfmt-lib/Cargo.toml +++ b/rustfmt-core/rustfmt-lib/Cargo.toml @@ -31,6 +31,7 @@ emitter = [ annotate-snippets = { version = "0.6", features = ["ansi_term"] } anyhow = "1.0" bytecount = "0.6" +dunce = "1.0" ignore = "0.4.11" itertools = "0.8" lazy_static = "1.0.0" diff --git a/rustfmt-core/rustfmt-lib/src/config.rs b/rustfmt-core/rustfmt-lib/src/config.rs index decc9be25a1..6b1c73ee4c1 100644 --- a/rustfmt-core/rustfmt-lib/src/config.rs +++ b/rustfmt-core/rustfmt-lib/src/config.rs @@ -13,7 +13,6 @@ pub use crate::config::lists::*; pub use crate::config::options::*; use crate::config::config_type::ConfigType; -use crate::utils::absolute_path; #[macro_use] pub mod config_type; @@ -238,7 +237,7 @@ impl Config { dir.to_path_buf() }; - current = absolute_path(current)?; + current = dunce::canonicalize(current)?; loop { match get_toml_path(¤t) { diff --git a/rustfmt-core/rustfmt-lib/src/config/file_lines.rs b/rustfmt-core/rustfmt-lib/src/config/file_lines.rs index 946f5bd2b67..1af95a57e94 100644 --- a/rustfmt-core/rustfmt-lib/src/config/file_lines.rs +++ b/rustfmt-core/rustfmt-lib/src/config/file_lines.rs @@ -12,8 +12,6 @@ use thiserror::Error; use rustc_span::{self, SourceFile}; -use crate::utils::absolute_path; - /// A range of lines in a file, inclusive of both ends. pub struct LineRange { pub file: Rc, @@ -297,7 +295,7 @@ impl<'a> iter::Iterator for Files<'a> { fn canonicalize_path_string(file: &FileName) -> std::io::Result { match *file { - FileName::Real(ref path) => absolute_path(path).map(FileName::Real), + FileName::Real(ref path) => dunce::canonicalize(path).map(FileName::Real), _ => Ok(file.clone()), } } diff --git a/rustfmt-core/rustfmt-lib/src/lib.rs b/rustfmt-core/rustfmt-lib/src/lib.rs index 3cd626d62ca..4dc0981b6c6 100644 --- a/rustfmt-core/rustfmt-lib/src/lib.rs +++ b/rustfmt-core/rustfmt-lib/src/lib.rs @@ -24,7 +24,6 @@ pub use crate::config::{ }; pub use crate::emitter::rustfmt_diff::{ModifiedChunk, ModifiedLines}; pub use crate::format_report_formatter::{FormatReportFormatter, FormatReportFormatterBuilder}; -pub use crate::utils::absolute_path; use crate::comment::LineClasses; use crate::emitter::Emitter; diff --git a/rustfmt-core/rustfmt-lib/src/utils.rs b/rustfmt-core/rustfmt-lib/src/utils.rs index 61b85d88db0..63b09537961 100644 --- a/rustfmt-core/rustfmt-lib/src/utils.rs +++ b/rustfmt-core/rustfmt-lib/src/utils.rs @@ -1,6 +1,4 @@ use std::borrow::Cow; -use std::io; -use std::path; use rustc_ast::ast::{ self, Attribute, CrateSugar, MetaItem, MetaItemKind, NestedMetaItem, NodeId, Path, Visibility, @@ -663,47 +661,6 @@ pub(crate) fn unicode_str_width(s: &str) -> usize { s.width() } -#[cfg(windows)] -pub fn absolute_path>(p: P) -> io::Result { - use std::ffi::OsString; - use std::iter::once; - use std::os::windows::ffi::{OsStrExt, OsStringExt}; - use std::ptr::null_mut; - use winapi::um::errhandlingapi::GetLastError; - use winapi::um::fileapi::GetFullPathNameW; - - // FIXME: This `MAX_PATH` may be valid only from Windows 10, version 1607. - // https://docs.microsoft.com/ja-jp/windows/desktop/FileIO/naming-a-file#paths - const MAX_PATH: usize = 32767; - let wide: Vec = p - .as_ref() - .as_os_str() - .encode_wide() - .chain(once(0)) - .collect(); - let mut buffer: Vec = vec![0; MAX_PATH]; - unsafe { - let result = GetFullPathNameW( - wide.as_ptr(), - MAX_PATH as u32, - buffer.as_mut_ptr(), - null_mut(), - ); - if result == 0 { - Err(io::Error::from_raw_os_error(GetLastError() as i32)) - } else { - Ok(path::PathBuf::from(OsString::from_wide( - &buffer[..result as usize], - ))) - } - } -} - -#[cfg(not(windows))] -pub fn absolute_path>(p: P) -> io::Result { - std::fs::canonicalize(p) -} - #[cfg(test)] mod test { use super::*;