Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure manifest file and pyproject file paths are absolute #1204

Merged
merged 3 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 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
42 changes: 28 additions & 14 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 @@ -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());
Expand Down Expand Up @@ -180,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 Expand Up @@ -243,9 +257,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 {})",
Expand Down
11 changes: 7 additions & 4 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 Expand Up @@ -512,7 +516,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)?;
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