Skip to content

Commit 006e5dd

Browse files
committed
Use normpath crate instead of fs::canonicalize
* https://docs.rs/normpath * rust-lang/rust#91673
1 parent 7eadb90 commit 006e5dd

File tree

7 files changed

+41
-18
lines changed

7 files changed

+41
-18
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ clap_complete_fig = "4.0.0"
6464
tracing = "0.1.36"
6565
tracing-subscriber = { version = "0.3.15", features = ["env-filter"], optional = true }
6666
dunce = "1.0.2"
67+
normpath = "0.3.2"
6768
pep440 = "0.2.0"
6869

6970
# upload

src/cross_compile.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::{PythonInterpreter, Target};
22
use anyhow::{bail, Result};
33
use fs_err::{self as fs, DirEntry};
4+
use normpath::PathExt as _;
45
use std::collections::HashMap;
56
use std::env;
67
use std::path::{Path, PathBuf};
@@ -123,7 +124,7 @@ pub fn find_sysconfigdata(lib_dir: &Path, target: &Target) -> Result<PathBuf> {
123124
let mut sysconfig_paths = sysconfig_paths
124125
.iter()
125126
.filter_map(|p| {
126-
let canonical = fs::canonicalize(p).ok();
127+
let canonical = p.normalize().ok().map(|p| p.into_path_buf());
127128
match &sysconfig_name {
128129
Some(_) => canonical.filter(|p| p.file_stem() == sysconfig_name.as_deref()),
129130
None => canonical,

src/module_writer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use flate2::Compression;
77
use fs_err as fs;
88
use fs_err::File;
99
use ignore::WalkBuilder;
10+
use normpath::PathExt as _;
1011
use sha2::{Digest, Sha256};
1112
use std::collections::{HashMap, HashSet};
1213
use std::ffi::OsStr;
@@ -275,7 +276,7 @@ impl WheelWriter {
275276
metadata21: &Metadata21,
276277
) -> Result<()> {
277278
if let Some(python_module) = &project_layout.python_module {
278-
let absolute_path = fs::canonicalize(python_module)?;
279+
let absolute_path = python_module.normalize()?.into_path_buf();
279280
if let Some(python_path) = absolute_path.parent().and_then(|p| p.to_str()) {
280281
let name = metadata21.get_distribution_escaped();
281282
let target = format!("{}.pth", name);

src/project_layout.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::build_options::{extract_cargo_metadata_args, CargoOptions};
22
use crate::{CargoToml, Metadata21, PyProjectToml};
33
use anyhow::{bail, format_err, Context, Result};
44
use cargo_metadata::{Metadata, MetadataCommand};
5-
use fs_err as fs;
5+
use normpath::PathExt as _;
66
use std::env;
77
use std::io;
88
use std::path::{Path, PathBuf};
@@ -191,39 +191,42 @@ impl ProjectResolver {
191191
) -> Result<(PathBuf, PathBuf)> {
192192
// use command line argument if specified
193193
if let Some(path) = cargo_manifest_path {
194+
let path = path.normalize()?.into_path_buf();
194195
let workspace_root = Self::resolve_cargo_metadata(&path, cargo_options)?.workspace_root;
195196
let workspace_parent = workspace_root.parent().unwrap_or(&workspace_root);
196-
for parent in fs::canonicalize(&path)?.ancestors().skip(1) {
197+
for parent in path.ancestors().skip(1) {
197198
// Allow looking outside to the parent directory of Cargo workspace root
198199
if !dunce::simplified(parent).starts_with(&workspace_parent) {
199200
break;
200201
}
201202
let pyproject_file = parent.join(PYPROJECT_TOML);
202203
if pyproject_file.is_file() {
203-
// Don't return canonicalized manifest path
204-
// cargo doesn't handle them well.
205-
// See https://github.com/rust-lang/cargo/issues/9770
206204
return Ok((path, pyproject_file));
207205
}
208206
}
209-
return Ok((path.clone(), path.parent().unwrap().join(PYPROJECT_TOML)));
207+
let pyproject_file = path.parent().unwrap().join(PYPROJECT_TOML);
208+
return Ok((path, pyproject_file));
210209
}
211210
// check `manifest-path` option in pyproject.toml
212-
let current_dir = fs::canonicalize(
213-
env::current_dir().context("Failed to detect current directory ಠ_ಠ")?,
214-
)?;
211+
let current_dir = env::current_dir()
212+
.context("Failed to detect current directory ಠ_ಠ")?
213+
.normalize()?
214+
.into_path_buf();
215215
let pyproject_file = current_dir.join(PYPROJECT_TOML);
216216
if pyproject_file.is_file() {
217217
let pyproject =
218218
PyProjectToml::new(&pyproject_file).context("pyproject.toml is invalid")?;
219219
if let Some(path) = pyproject.manifest_path() {
220220
// pyproject.toml must be placed at top directory
221-
let manifest_dir =
222-
fs::canonicalize(path.parent().context("missing parent directory")?)?;
221+
let manifest_dir = path
222+
.parent()
223+
.context("missing parent directory")?
224+
.normalize()?
225+
.into_path_buf();
223226
if !manifest_dir.starts_with(&current_dir) {
224227
bail!("Cargo.toml can not be placed outside of the directory containing pyproject.toml");
225228
}
226-
return Ok((path.to_path_buf(), pyproject_file));
229+
return Ok((path.normalize()?.into_path_buf(), pyproject_file));
227230
} else {
228231
// Detect src layout:
229232
//

src/source_distribution.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{BuildContext, PyProjectToml, SDistWriter};
33
use anyhow::{bail, Context, Result};
44
use cargo_metadata::{Metadata, MetadataCommand};
55
use fs_err as fs;
6+
use normpath::PathExt as _;
67
use std::collections::HashMap;
78
use std::path::{Path, PathBuf};
89
use std::process::Command;
@@ -266,7 +267,7 @@ fn add_crate_to_source_distribution(
266267
.map(Path::new)
267268
.collect();
268269

269-
let abs_manifest_path = fs::canonicalize(manifest_path)?;
270+
let abs_manifest_path = manifest_path.normalize()?.into_path_buf();
270271
let abs_manifest_dir = abs_manifest_path.parent().unwrap();
271272
let pyproject_dir = pyproject_toml_path.parent().unwrap();
272273
let cargo_toml_in_subdir = root_crate
@@ -401,7 +402,10 @@ pub fn source_distribution(
401402
) -> Result<PathBuf> {
402403
let metadata21 = &build_context.metadata21;
403404
let manifest_path = &build_context.manifest_path;
404-
let pyproject_toml_path = fs::canonicalize(&build_context.pyproject_toml_path)?;
405+
let pyproject_toml_path = build_context
406+
.pyproject_toml_path
407+
.normalize()?
408+
.into_path_buf();
405409
let workspace_manifest_path = build_context
406410
.cargo_metadata
407411
.workspace_root
@@ -475,7 +479,7 @@ pub fn source_distribution(
475479
true,
476480
)?;
477481

478-
let abs_manifest_path = fs::canonicalize(manifest_path)?;
482+
let abs_manifest_path = manifest_path.normalize()?.into_path_buf();
479483
let abs_manifest_dir = abs_manifest_path.parent().unwrap();
480484
let cargo_lock_path = abs_manifest_dir.join("Cargo.lock");
481485
let cargo_lock_required =

tests/common/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use anyhow::{bail, Result};
22
use fs_err as fs;
33
use maturin::Target;
4+
use normpath::PathExt as _;
45
use std::path::Path;
56
use std::path::PathBuf;
67
use std::process::{Command, Stdio};
@@ -93,7 +94,9 @@ pub fn handle_result<T>(result: Result<T>) -> T {
9394

9495
/// Create virtualenv
9596
pub fn create_virtualenv(name: &str, python_interp: Option<PathBuf>) -> Result<(PathBuf, PathBuf)> {
96-
let venv_dir = fs::canonicalize(PathBuf::from("test-crates"))?
97+
let venv_dir = PathBuf::from("test-crates")
98+
.normalize()?
99+
.into_path_buf()
97100
.join("venvs")
98101
.join(name);
99102
let target = Target::from_target_triple(None)?;

0 commit comments

Comments
 (0)