From 37ba503f3b52885e6ce10db7c1f8bd6772f9241c Mon Sep 17 00:00:00 2001 From: messense Date: Tue, 18 Oct 2022 12:30:19 +0800 Subject: [PATCH 1/3] Revert 'Fix sdist build for maturin itself' Reverts 3ba5bea7c43da89e58b6b09627523fa0aa34e40d --- src/source_distribution.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/source_distribution.rs b/src/source_distribution.rs index 1ab4d8b47..ae7341276 100644 --- a/src/source_distribution.rs +++ b/src/source_distribution.rs @@ -512,7 +512,6 @@ pub fn source_distribution( debug!("Ignoring {}", source.display()); continue; } - let source = fs::canonicalize(source)?; let target = root_dir.join(source.strip_prefix(&pyproject_dir).unwrap()); if source.is_dir() { writer.add_directory(target)?; From 7eadb90aba54f857a130a0c07e250afb0ac4d74e Mon Sep 17 00:00:00 2001 From: messense Date: Tue, 18 Oct 2022 12:37:15 +0800 Subject: [PATCH 2/3] Ensure manifest file and pyproject file paths are absolute --- src/project_layout.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/project_layout.rs b/src/project_layout.rs index c7de00d64..aa59778d1 100644 --- a/src/project_layout.rs +++ b/src/project_layout.rs @@ -63,6 +63,17 @@ impl ProjectResolver { manifest_file.display() ); } + // Sanity checks in debug build + debug_assert!( + manifest_file.is_absolute(), + "manifest_file {} is not absolute", + manifest_file.display() + ); + debug_assert!( + pyproject_file.is_absolute(), + "pyproject_file {} is not absolute", + pyproject_file.display() + ); // Set Cargo manifest path cargo_options.manifest_path = Some(manifest_file.clone()); @@ -243,9 +254,9 @@ impl ProjectResolver { } } // check Cargo.toml in current directory - let path = PathBuf::from("Cargo.toml"); + let path = current_dir.join("Cargo.toml"); if path.exists() { - Ok((path, PathBuf::from(PYPROJECT_TOML))) + Ok((path, current_dir.join(PYPROJECT_TOML))) } else { Err(format_err!( "Can't find {} (in {})", From 451a25f3ab117adfa86d5d91dd8e106268729f40 Mon Sep 17 00:00:00 2001 From: messense Date: Tue, 18 Oct 2022 13:15:52 +0800 Subject: [PATCH 3/3] Use `normpath` crate instead of `fs::canonicalize` * https://docs.rs/normpath * https://github.com/rust-lang/rust/pull/91673 --- Cargo.lock | 30 ++++++++++++++++++++---------- Cargo.toml | 1 + src/cross_compile.rs | 3 ++- src/module_writer.rs | 3 ++- src/project_layout.rs | 27 +++++++++++++++------------ src/source_distribution.rs | 10 +++++++--- tests/common/mod.rs | 5 ++++- 7 files changed, 51 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b185f8350..5c6d88a16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -249,7 +249,7 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6755badfe5e0224c6e2ae710d82cd7dce5866e8ebcb22e5f3eced8f25fbd1ace" dependencies = [ - "clap 4.0.15", + "clap 4.0.16", ] [[package]] @@ -269,7 +269,7 @@ checksum = "c0051cd416c6a5dd0db1583f2063580357eabc46e9e8f23abda2e7b78f309b6e" dependencies = [ "anyhow", "cargo-options", - "clap 4.0.15", + "clap 4.0.16", "dirs", "fs-err", "indicatif", @@ -287,7 +287,7 @@ dependencies = [ "anyhow", "cargo-options", "cargo_metadata", - "clap 4.0.15", + "clap 4.0.16", "dirs", "fs-err", "path-slash", @@ -407,9 +407,9 @@ dependencies = [ [[package]] name = "clap" -version = "4.0.15" +version = "4.0.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bf8832993da70a4c6d13c581f4463c2bdda27b9bf1c5498dc4365543abe6d6f" +checksum = "2ef582e2c00a63a0c0aa1fb4a4870781c4f5729f51196d3537fa7c1c1992eaa3" dependencies = [ "atty", "bitflags", @@ -427,7 +427,7 @@ version = "4.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11cba7abac9b56dfe2f035098cdb3a43946f276e6db83b72c4e692343f9aab9a" dependencies = [ - "clap 4.0.15", + "clap 4.0.16", ] [[package]] @@ -436,7 +436,7 @@ version = "4.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf2fc15aa585faf3c59b96c40923b2f4d119c710a84e8a81251eed54ba75335c" dependencies = [ - "clap 4.0.15", + "clap 4.0.16", "clap_complete", ] @@ -1403,7 +1403,7 @@ dependencies = [ "cargo_metadata", "cbindgen", "cc", - "clap 4.0.15", + "clap 4.0.16", "clap_complete", "clap_complete_fig", "configparser", @@ -1424,6 +1424,7 @@ dependencies = [ "minijinja", "multipart", "native-tls", + "normpath", "once_cell", "pep440", "platform-info", @@ -1572,6 +1573,15 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" +[[package]] +name = "normpath" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "04aaf5e9cb0fbf883cc0423159eacdf96a9878022084b35c462c428cab73bcaf" +dependencies = [ + "winapi", +] + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -1968,9 +1978,9 @@ checksum = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" [[package]] name = "proc-macro2" -version = "1.0.46" +version = "1.0.47" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "94e2ef8dbfc347b10c094890f778ee2e36ca9bb4262e86dc99cd217e35f3470b" +checksum = "5ea3d908b0e36316caf9e9e2c4625cdde190a7e6f440d794667ed17a1855e725" dependencies = [ "unicode-ident", ] diff --git a/Cargo.toml b/Cargo.toml index cd90d42f9..807f33ddb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,6 +64,7 @@ clap_complete_fig = "4.0.0" tracing = "0.1.36" tracing-subscriber = { version = "0.3.15", features = ["env-filter"], optional = true } dunce = "1.0.2" +normpath = "0.3.2" pep440 = "0.2.0" # upload diff --git a/src/cross_compile.rs b/src/cross_compile.rs index 643cdca4d..c5bfc9841 100644 --- a/src/cross_compile.rs +++ b/src/cross_compile.rs @@ -1,6 +1,7 @@ use crate::{PythonInterpreter, Target}; use anyhow::{bail, Result}; use fs_err::{self as fs, DirEntry}; +use normpath::PathExt as _; use std::collections::HashMap; use std::env; use std::path::{Path, PathBuf}; @@ -123,7 +124,7 @@ pub fn find_sysconfigdata(lib_dir: &Path, target: &Target) -> Result { let mut sysconfig_paths = sysconfig_paths .iter() .filter_map(|p| { - let canonical = fs::canonicalize(p).ok(); + let canonical = p.normalize().ok().map(|p| p.into_path_buf()); match &sysconfig_name { Some(_) => canonical.filter(|p| p.file_stem() == sysconfig_name.as_deref()), None => canonical, diff --git a/src/module_writer.rs b/src/module_writer.rs index 95c306698..c7cc92ec1 100644 --- a/src/module_writer.rs +++ b/src/module_writer.rs @@ -7,6 +7,7 @@ use flate2::Compression; use fs_err as fs; use fs_err::File; use ignore::WalkBuilder; +use normpath::PathExt as _; use sha2::{Digest, Sha256}; use std::collections::{HashMap, HashSet}; use std::ffi::OsStr; @@ -275,7 +276,7 @@ impl WheelWriter { metadata21: &Metadata21, ) -> Result<()> { if let Some(python_module) = &project_layout.python_module { - let absolute_path = fs::canonicalize(python_module)?; + let absolute_path = python_module.normalize()?.into_path_buf(); if let Some(python_path) = absolute_path.parent().and_then(|p| p.to_str()) { let name = metadata21.get_distribution_escaped(); let target = format!("{}.pth", name); diff --git a/src/project_layout.rs b/src/project_layout.rs index aa59778d1..8e5b5b486 100644 --- a/src/project_layout.rs +++ b/src/project_layout.rs @@ -2,7 +2,7 @@ use crate::build_options::{extract_cargo_metadata_args, CargoOptions}; use crate::{CargoToml, Metadata21, PyProjectToml}; use anyhow::{bail, format_err, Context, Result}; use cargo_metadata::{Metadata, MetadataCommand}; -use fs_err as fs; +use normpath::PathExt as _; use std::env; use std::io; use std::path::{Path, PathBuf}; @@ -191,39 +191,42 @@ impl ProjectResolver { ) -> Result<(PathBuf, PathBuf)> { // use command line argument if specified if let Some(path) = cargo_manifest_path { + let path = path.normalize()?.into_path_buf(); let workspace_root = Self::resolve_cargo_metadata(&path, cargo_options)?.workspace_root; let workspace_parent = workspace_root.parent().unwrap_or(&workspace_root); - for parent in fs::canonicalize(&path)?.ancestors().skip(1) { + for parent in path.ancestors().skip(1) { // Allow looking outside to the parent directory of Cargo workspace root if !dunce::simplified(parent).starts_with(&workspace_parent) { break; } let pyproject_file = parent.join(PYPROJECT_TOML); if pyproject_file.is_file() { - // Don't return canonicalized manifest path - // cargo doesn't handle them well. - // See https://github.com/rust-lang/cargo/issues/9770 return Ok((path, pyproject_file)); } } - return Ok((path.clone(), path.parent().unwrap().join(PYPROJECT_TOML))); + let pyproject_file = path.parent().unwrap().join(PYPROJECT_TOML); + return Ok((path, pyproject_file)); } // check `manifest-path` option in pyproject.toml - let current_dir = fs::canonicalize( - env::current_dir().context("Failed to detect current directory ಠ_ಠ")?, - )?; + let current_dir = env::current_dir() + .context("Failed to detect current directory ಠ_ಠ")? + .normalize()? + .into_path_buf(); let pyproject_file = current_dir.join(PYPROJECT_TOML); if pyproject_file.is_file() { let pyproject = PyProjectToml::new(&pyproject_file).context("pyproject.toml is invalid")?; if let Some(path) = pyproject.manifest_path() { // pyproject.toml must be placed at top directory - let manifest_dir = - fs::canonicalize(path.parent().context("missing parent directory")?)?; + let manifest_dir = path + .parent() + .context("missing parent directory")? + .normalize()? + .into_path_buf(); if !manifest_dir.starts_with(¤t_dir) { bail!("Cargo.toml can not be placed outside of the directory containing pyproject.toml"); } - return Ok((path.to_path_buf(), pyproject_file)); + return Ok((path.normalize()?.into_path_buf(), pyproject_file)); } else { // Detect src layout: // diff --git a/src/source_distribution.rs b/src/source_distribution.rs index ae7341276..a52eac04e 100644 --- a/src/source_distribution.rs +++ b/src/source_distribution.rs @@ -3,6 +3,7 @@ use crate::{BuildContext, PyProjectToml, SDistWriter}; use anyhow::{bail, Context, Result}; use cargo_metadata::{Metadata, MetadataCommand}; use fs_err as fs; +use normpath::PathExt as _; use std::collections::HashMap; use std::path::{Path, PathBuf}; use std::process::Command; @@ -266,7 +267,7 @@ fn add_crate_to_source_distribution( .map(Path::new) .collect(); - let abs_manifest_path = fs::canonicalize(manifest_path)?; + let abs_manifest_path = manifest_path.normalize()?.into_path_buf(); let abs_manifest_dir = abs_manifest_path.parent().unwrap(); let pyproject_dir = pyproject_toml_path.parent().unwrap(); let cargo_toml_in_subdir = root_crate @@ -401,7 +402,10 @@ pub fn source_distribution( ) -> Result { let metadata21 = &build_context.metadata21; let manifest_path = &build_context.manifest_path; - let pyproject_toml_path = fs::canonicalize(&build_context.pyproject_toml_path)?; + let pyproject_toml_path = build_context + .pyproject_toml_path + .normalize()? + .into_path_buf(); let workspace_manifest_path = build_context .cargo_metadata .workspace_root @@ -475,7 +479,7 @@ pub fn source_distribution( true, )?; - let abs_manifest_path = fs::canonicalize(manifest_path)?; + let abs_manifest_path = manifest_path.normalize()?.into_path_buf(); let abs_manifest_dir = abs_manifest_path.parent().unwrap(); let cargo_lock_path = abs_manifest_dir.join("Cargo.lock"); let cargo_lock_required = diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 95e64b8d6..fbf45a1ae 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -1,6 +1,7 @@ use anyhow::{bail, Result}; use fs_err as fs; use maturin::Target; +use normpath::PathExt as _; use std::path::Path; use std::path::PathBuf; use std::process::{Command, Stdio}; @@ -93,7 +94,9 @@ pub fn handle_result(result: Result) -> T { /// Create virtualenv pub fn create_virtualenv(name: &str, python_interp: Option) -> Result<(PathBuf, PathBuf)> { - let venv_dir = fs::canonicalize(PathBuf::from("test-crates"))? + let venv_dir = PathBuf::from("test-crates") + .normalize()? + .into_path_buf() .join("venvs") .join(name); let target = Target::from_target_triple(None)?;