Skip to content

Commit

Permalink
Use normpath crate instead of fs::canonicalize
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Oct 18, 2022
1 parent 7eadb90 commit 006e5dd
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 18 deletions.
10 changes: 10 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 @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/cross_compile.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -123,7 +124,7 @@ pub fn find_sysconfigdata(lib_dir: &Path, target: &Target) -> Result<PathBuf> {
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,
Expand Down
3 changes: 2 additions & 1 deletion src/module_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 15 additions & 12 deletions src/project_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(&current_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:
//
Expand Down
10 changes: 7 additions & 3 deletions src/source_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -401,7 +402,10 @@ pub fn source_distribution(
) -> Result<PathBuf> {
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
Expand Down Expand Up @@ -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 =
Expand Down
5 changes: 4 additions & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -93,7 +94,9 @@ pub fn handle_result<T>(result: Result<T>) -> T {

/// Create virtualenv
pub fn create_virtualenv(name: &str, python_interp: Option<PathBuf>) -> 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)?;
Expand Down

0 comments on commit 006e5dd

Please sign in to comment.