Skip to content

Commit

Permalink
Use duncet to fix path issues (#4137)
Browse files Browse the repository at this point in the history
  • Loading branch information
topecongiro committed Apr 23, 2020
1 parent c019181 commit 7194d52
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 59 deletions.
8 changes: 8 additions & 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
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions rustfmt-core/rustfmt-bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions rustfmt-core/rustfmt-bin/src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -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;
}
}
Expand Down
15 changes: 7 additions & 8 deletions rustfmt-core/rustfmt-bin/src/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -289,7 +288,7 @@ impl Target {
nested_int_test_files: Option<Vec<PathBuf>>,
) -> 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 {
Expand Down Expand Up @@ -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"),
Expand All @@ -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)
Expand Down Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions rustfmt-core/rustfmt-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 1 addition & 2 deletions rustfmt-core/rustfmt-lib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -238,7 +237,7 @@ impl Config {
dir.to_path_buf()
};

current = absolute_path(current)?;
current = dunce::canonicalize(current)?;

loop {
match get_toml_path(&current) {
Expand Down
4 changes: 1 addition & 3 deletions rustfmt-core/rustfmt-lib/src/config/file_lines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SourceFile>,
Expand Down Expand Up @@ -297,7 +295,7 @@ impl<'a> iter::Iterator for Files<'a> {

fn canonicalize_path_string(file: &FileName) -> std::io::Result<FileName> {
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()),
}
}
Expand Down
1 change: 0 additions & 1 deletion rustfmt-core/rustfmt-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
43 changes: 0 additions & 43 deletions rustfmt-core/rustfmt-lib/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -663,47 +661,6 @@ pub(crate) fn unicode_str_width(s: &str) -> usize {
s.width()
}

#[cfg(windows)]
pub fn absolute_path<P: AsRef<path::Path>>(p: P) -> io::Result<path::PathBuf> {
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<u16> = p
.as_ref()
.as_os_str()
.encode_wide()
.chain(once(0))
.collect();
let mut buffer: Vec<u16> = 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: AsRef<path::Path>>(p: P) -> io::Result<path::PathBuf> {
std::fs::canonicalize(p)
}

#[cfg(test)]
mod test {
use super::*;
Expand Down

0 comments on commit 7194d52

Please sign in to comment.