Skip to content

Commit 7194d52

Browse files
authored
Use duncet to fix path issues (#4137)
1 parent c019181 commit 7194d52

File tree

10 files changed

+22
-59
lines changed

10 files changed

+22
-59
lines changed

Diff for: Cargo.lock

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ generic-simd = ["rustfmt_lib/generic-simd"]
3939

4040
[dependencies]
4141
anyhow = "1.0"
42+
dunce = "1.0"
4243
env_logger = "0.7"
4344
log = "0.4"
4445
structopt = "0.3"

Diff for: rustfmt-core/rustfmt-bin/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ generic-simd = ["rustfmt_lib/generic-simd"]
3131
ansi_term = "0.12"
3232
anyhow = "1.0"
3333
cargo_metadata = "0.9"
34+
dunce = "1.0"
3435
env_logger = "0.7"
3536
getopts = "0.2"
3637
log = "0.4"

Diff for: rustfmt-core/rustfmt-bin/src/bin/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use structopt::StructOpt;
1414
use thiserror::Error;
1515

1616
use rustfmt_lib::{
17-
absolute_path, load_config, CliOptions, Config, Edition, EmitMode, FileLines, FileName,
17+
load_config, CliOptions, Config, Edition, EmitMode, FileLines, FileName,
1818
FormatReportFormatterBuilder, Input, Session, Verbosity,
1919
};
2020

@@ -252,7 +252,7 @@ enum OptError {
252252
impl Opt {
253253
fn canonicalize(&mut self) {
254254
for f in &mut self.files {
255-
if let Ok(canonical_path) = absolute_path(&f) {
255+
if let Ok(canonical_path) = dunce::canonicalize(&f) {
256256
*f = canonical_path;
257257
}
258258
}

Diff for: rustfmt-core/rustfmt-bin/src/cargo-fmt/main.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::path::{Path, PathBuf};
1313
use std::process::Command;
1414
use std::str;
1515

16-
use rustfmt_lib::absolute_path;
1716
use structopt::StructOpt;
1817

1918
#[derive(StructOpt, Debug)]
@@ -289,7 +288,7 @@ impl Target {
289288
nested_int_test_files: Option<Vec<PathBuf>>,
290289
) -> Self {
291290
let path = PathBuf::from(&target.src_path);
292-
let canonicalized = absolute_path(&path).unwrap_or(path);
291+
let canonicalized = dunce::canonicalize(&path).unwrap_or(path);
293292
let test_files = nested_int_test_files.unwrap_or_else(Vec::new);
294293

295294
Self {
@@ -389,14 +388,14 @@ fn get_targets_root_only(
389388
include_nested_test_files: bool,
390389
) -> Result<(), io::Error> {
391390
let metadata = get_cargo_metadata(manifest_path, false)?;
392-
let workspace_root_path = absolute_path(PathBuf::from(&metadata.workspace_root))?;
391+
let workspace_root_path = dunce::canonicalize(PathBuf::from(&metadata.workspace_root))?;
393392
let (in_workspace_root, current_dir_manifest) = if let Some(target_manifest) = manifest_path {
394393
(
395-
workspace_root_path == target_manifest,
396-
absolute_path(target_manifest)?,
394+
workspace_root_path.as_path() == target_manifest,
395+
dunce::canonicalize(target_manifest)?,
397396
)
398397
} else {
399-
let current_dir = absolute_path(env::current_dir()?)?;
398+
let current_dir = dunce::canonicalize(env::current_dir()?)?;
400399
(
401400
workspace_root_path == current_dir,
402401
current_dir.join("Cargo.toml"),
@@ -414,7 +413,7 @@ fn get_targets_root_only(
414413
.into_iter()
415414
.filter(|p| {
416415
in_workspace_root
417-
|| absolute_path(PathBuf::from(&p.manifest_path)).unwrap_or_default()
416+
|| dunce::canonicalize(PathBuf::from(&p.manifest_path)).unwrap_or_default()
418417
== current_dir_manifest
419418
})
420419
.map(|p| p.targets)
@@ -1051,7 +1050,7 @@ mod cargo_fmt_tests {
10511050
edition: &str,
10521051
) -> Target {
10531052
let path = PathBuf::from(src_path);
1054-
let canonicalized = absolute_path(&path).unwrap_or(path);
1053+
let canonicalized = dunce::canonicalize(&path).unwrap_or(path);
10551054
Target {
10561055
path: canonicalized,
10571056
kind: String::from(kind),

Diff for: rustfmt-core/rustfmt-lib/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ emitter = [
3131
annotate-snippets = { version = "0.6", features = ["ansi_term"] }
3232
anyhow = "1.0"
3333
bytecount = "0.6"
34+
dunce = "1.0"
3435
ignore = "0.4.11"
3536
itertools = "0.8"
3637
lazy_static = "1.0.0"

Diff for: rustfmt-core/rustfmt-lib/src/config.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub use crate::config::lists::*;
1313
pub use crate::config::options::*;
1414

1515
use crate::config::config_type::ConfigType;
16-
use crate::utils::absolute_path;
1716

1817
#[macro_use]
1918
pub mod config_type;
@@ -238,7 +237,7 @@ impl Config {
238237
dir.to_path_buf()
239238
};
240239

241-
current = absolute_path(current)?;
240+
current = dunce::canonicalize(current)?;
242241

243242
loop {
244243
match get_toml_path(&current) {

Diff for: rustfmt-core/rustfmt-lib/src/config/file_lines.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ use thiserror::Error;
1212

1313
use rustc_span::{self, SourceFile};
1414

15-
use crate::utils::absolute_path;
16-
1715
/// A range of lines in a file, inclusive of both ends.
1816
pub struct LineRange {
1917
pub file: Rc<SourceFile>,
@@ -297,7 +295,7 @@ impl<'a> iter::Iterator for Files<'a> {
297295

298296
fn canonicalize_path_string(file: &FileName) -> std::io::Result<FileName> {
299297
match *file {
300-
FileName::Real(ref path) => absolute_path(path).map(FileName::Real),
298+
FileName::Real(ref path) => dunce::canonicalize(path).map(FileName::Real),
301299
_ => Ok(file.clone()),
302300
}
303301
}

Diff for: rustfmt-core/rustfmt-lib/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ pub use crate::config::{
2424
};
2525
pub use crate::emitter::rustfmt_diff::{ModifiedChunk, ModifiedLines};
2626
pub use crate::format_report_formatter::{FormatReportFormatter, FormatReportFormatterBuilder};
27-
pub use crate::utils::absolute_path;
2827

2928
use crate::comment::LineClasses;
3029
use crate::emitter::Emitter;

Diff for: rustfmt-core/rustfmt-lib/src/utils.rs

-43
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use std::borrow::Cow;
2-
use std::io;
3-
use std::path;
42

53
use rustc_ast::ast::{
64
self, Attribute, CrateSugar, MetaItem, MetaItemKind, NestedMetaItem, NodeId, Path, Visibility,
@@ -663,47 +661,6 @@ pub(crate) fn unicode_str_width(s: &str) -> usize {
663661
s.width()
664662
}
665663

666-
#[cfg(windows)]
667-
pub fn absolute_path<P: AsRef<path::Path>>(p: P) -> io::Result<path::PathBuf> {
668-
use std::ffi::OsString;
669-
use std::iter::once;
670-
use std::os::windows::ffi::{OsStrExt, OsStringExt};
671-
use std::ptr::null_mut;
672-
use winapi::um::errhandlingapi::GetLastError;
673-
use winapi::um::fileapi::GetFullPathNameW;
674-
675-
// FIXME: This `MAX_PATH` may be valid only from Windows 10, version 1607.
676-
// https://docs.microsoft.com/ja-jp/windows/desktop/FileIO/naming-a-file#paths
677-
const MAX_PATH: usize = 32767;
678-
let wide: Vec<u16> = p
679-
.as_ref()
680-
.as_os_str()
681-
.encode_wide()
682-
.chain(once(0))
683-
.collect();
684-
let mut buffer: Vec<u16> = vec![0; MAX_PATH];
685-
unsafe {
686-
let result = GetFullPathNameW(
687-
wide.as_ptr(),
688-
MAX_PATH as u32,
689-
buffer.as_mut_ptr(),
690-
null_mut(),
691-
);
692-
if result == 0 {
693-
Err(io::Error::from_raw_os_error(GetLastError() as i32))
694-
} else {
695-
Ok(path::PathBuf::from(OsString::from_wide(
696-
&buffer[..result as usize],
697-
)))
698-
}
699-
}
700-
}
701-
702-
#[cfg(not(windows))]
703-
pub fn absolute_path<P: AsRef<path::Path>>(p: P) -> io::Result<path::PathBuf> {
704-
std::fs::canonicalize(p)
705-
}
706-
707664
#[cfg(test)]
708665
mod test {
709666
use super::*;

0 commit comments

Comments
 (0)