Skip to content

Commit

Permalink
Fix sdist when pyproject.toml isn't in the same dir of Cargo.toml
Browse files Browse the repository at this point in the history
And switch to use `pretty-assertions`
  • Loading branch information
messense committed Sep 12, 2022
1 parent 01e8160 commit 6c597cb
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 14 deletions.
44 changes: 44 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 @@ -77,6 +77,7 @@ native-tls-crate = { package = "native-tls", version = "0.2.8", optional = true

[dev-dependencies]
indoc = "1.0.3"
pretty_assertions = "1.3.0"

[features]
default = ["log", "upload", "rustls", "human-panic"]
Expand Down
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* auditwheel: find dylibs in Cargo target directory in [#1092](https://github.com/PyO3/maturin/pull/1092)
* Add library search paths in Cargo target directory to rpath in editable mode on Linux in [#1094](https://github.com/PyO3/maturin/pull/1094)
* Remove default manifest path for `maturin sdist` command in [#1097](https://github.com/PyO3/maturin/pull/1097)
* Fix sdist when `pyproject.toml` isn't in the same dir of `Cargo.toml` in [#1099](https://github.com/PyO3/maturin/pull/1099)

## [0.13.2] - 2022-08-14

Expand Down
1 change: 1 addition & 0 deletions src/auditwheel/audit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,7 @@ pub fn relpath(to: &Path, from: &Path) -> PathBuf {
#[cfg(test)]
mod test {
use crate::auditwheel::audit::relpath;
use pretty_assertions::assert_eq;
use std::path::Path;

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/auditwheel/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ impl Policy {
#[cfg(test)]
mod test {
use super::{Arch, Policy, MANYLINUX_POLICIES, MUSLLINUX_POLICIES};
use pretty_assertions::assert_eq;

#[test]
fn test_load_policy() {
Expand Down
2 changes: 2 additions & 0 deletions src/build_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ pub struct BuildContext {
pub bridge: BridgeModel,
/// Whether this project is pure rust or rust mixed with python
pub project_layout: ProjectLayout,
/// The path to pyproject.toml. Required for the source distribution
pub pyproject_toml_path: PathBuf,
/// Parsed pyproject.toml if any
pub pyproject_toml: Option<PyProjectToml>,
/// Python Package Metadata 2.1
Expand Down
3 changes: 3 additions & 0 deletions src/build_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ impl BuildOptions {
project_layout,
cargo_toml_path,
cargo_toml,
pyproject_toml_path,
pyproject_toml,
module_name,
metadata21,
Expand Down Expand Up @@ -690,6 +691,7 @@ impl BuildOptions {
target,
bridge,
project_layout,
pyproject_toml_path,
pyproject_toml,
metadata21,
crate_name,
Expand Down Expand Up @@ -1175,6 +1177,7 @@ impl CargoOptions {
#[cfg(test)]
mod test {
use cargo_metadata::MetadataCommand;
use pretty_assertions::assert_eq;
use std::path::Path;

use super::*;
Expand Down
1 change: 1 addition & 0 deletions src/cargo_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ pub struct RemainingCoreMetadata {
mod test {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;

#[test]
fn test_metadata_from_cargo_toml() {
Expand Down
1 change: 1 addition & 0 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ fn fold_header(text: &str) -> String {
mod test {
use super::*;
use indoc::indoc;
use pretty_assertions::assert_eq;
use std::io::Write;

fn assert_metadata_from_cargo_toml(readme: &str, cargo_toml: &str, expected: &str) {
Expand Down
3 changes: 3 additions & 0 deletions src/project_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct ProjectResolver {
pub cargo_toml_path: PathBuf,
/// Parsed Cargo.toml
pub cargo_toml: CargoToml,
/// pyproject.toml path
pub pyproject_toml_path: PathBuf,
/// Parsed pyproject.toml
pub pyproject_toml: Option<PyProjectToml>,
/// Rust module name
Expand Down Expand Up @@ -143,6 +145,7 @@ impl ProjectResolver {
project_layout,
cargo_toml_path: manifest_file,
cargo_toml,
pyproject_toml_path: pyproject_file,
pyproject_toml,
module_name,
metadata21,
Expand Down
1 change: 1 addition & 0 deletions src/pyproject_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ impl PyProjectToml {
mod tests {
use crate::PyProjectToml;
use fs_err as fs;
use pretty_assertions::assert_eq;
use std::path::Path;
use tempfile::TempDir;

Expand Down
1 change: 1 addition & 0 deletions src/python_interpreter/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ suppress_build_script_link_lines=false"#,
#[cfg(test)]
mod test {
use super::*;
use pretty_assertions::assert_eq;

#[test]
fn test_load_sysconfig() {
Expand Down
49 changes: 38 additions & 11 deletions src/source_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,14 @@ fn rewrite_cargo_toml(
/// Runs `cargo package --list --allow-dirty` to obtain a list of files to package.
fn add_crate_to_source_distribution(
writer: &mut SDistWriter,
pyproject_toml_path: impl AsRef<Path>,
manifest_path: impl AsRef<Path>,
prefix: impl AsRef<Path>,
known_path_deps: &HashMap<String, PathBuf>,
root_crate: bool,
) -> Result<()> {
let manifest_path = manifest_path.as_ref();
let pyproject_toml_path = pyproject_toml_path.as_ref();
let output = Command::new("cargo")
.args(&["package", "--list", "--allow-dirty", "--manifest-path"])
.arg(manifest_path)
Expand Down Expand Up @@ -154,18 +156,29 @@ fn add_crate_to_source_distribution(
.collect();

let manifest_dir = manifest_path.parent().unwrap();
let abs_manifest_dir = manifest_dir.canonicalize()?;
let pyproject_dir = pyproject_toml_path.parent().unwrap().canonicalize()?;
let cargo_toml_in_subdir = root_crate
&& abs_manifest_dir != pyproject_dir
&& abs_manifest_dir.starts_with(pyproject_dir.canonicalize()?);

let target_source: Vec<(PathBuf, PathBuf)> = file_list
let mut target_source: Vec<(PathBuf, PathBuf)> = file_list
.iter()
.map(|relative_to_manifests| {
let relative_to_cwd = manifest_dir.join(relative_to_manifests);
(relative_to_manifests.to_path_buf(), relative_to_cwd)
if root_crate && cargo_toml_in_subdir {
(relative_to_cwd.clone(), relative_to_cwd)
} else {
(relative_to_manifests.to_path_buf(), relative_to_cwd)
}
})
// We rewrite Cargo.toml and add it separately
.filter(|(target, source)| {
// Skip generated files. See https://github.com/rust-lang/cargo/issues/7938#issuecomment-593280660
// and https://github.com/PyO3/maturin/issues/449
if target == Path::new("Cargo.toml.orig") || target == Path::new("Cargo.toml") {
if target == Path::new("Cargo.toml.orig")
|| (root_crate && target.file_name() == Some("Cargo.toml".as_ref()))
{
false
} else {
source.exists()
Expand All @@ -178,20 +191,32 @@ fn add_crate_to_source_distribution(
.iter()
.any(|(target, _)| target == Path::new("pyproject.toml"))
{
bail!(
"pyproject.toml was not included by `cargo package`. \
Please make sure pyproject.toml is not excluded or build without `--sdist`"
)
// Add pyproject.toml to the source distribution
// if Cargo.toml is in subdirectory of pyproject.toml directory
if cargo_toml_in_subdir {
target_source.push((
PathBuf::from("pyproject.toml"),
pyproject_toml_path.to_path_buf(),
));
} else {
bail!(
"pyproject.toml was not included by `cargo package`. \
Please make sure pyproject.toml is not excluded or build without `--sdist`"
)
}
}

let rewritten_cargo_toml = rewrite_cargo_toml(&manifest_path, known_path_deps, root_crate)?;

let prefix = prefix.as_ref();
writer.add_directory(prefix)?;
writer.add_bytes(
prefix.join(manifest_path.file_name().unwrap()),
rewritten_cargo_toml.as_bytes(),
)?;

let cargo_toml = if cargo_toml_in_subdir {
prefix.join(manifest_path)
} else {
prefix.join(manifest_path.file_name().unwrap())
};
writer.add_bytes(cargo_toml, rewritten_cargo_toml.as_bytes())?;
for (target, source) in target_source {
writer.add_file(prefix.join(target), source)?;
}
Expand Down Expand Up @@ -257,6 +282,7 @@ pub fn source_distribution(
for (name, path_dep) in known_path_deps.iter() {
add_crate_to_source_distribution(
&mut writer,
&build_context.pyproject_toml_path,
&path_dep,
&root_dir.join(LOCAL_DEPENDENCIES_FOLDER).join(name),
&known_path_deps,
Expand All @@ -272,6 +298,7 @@ pub fn source_distribution(
// Add the main crate
add_crate_to_source_distribution(
&mut writer,
&build_context.pyproject_toml_path,
&manifest_path,
&root_dir,
&known_path_deps,
Expand Down
1 change: 1 addition & 0 deletions src/target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ fn emcc_version() -> Result<String> {
#[cfg(test)]
mod test {
use super::macosx_deployment_target;
use pretty_assertions::assert_eq;

#[test]
fn test_macosx_deployment_target() {
Expand Down
1 change: 1 addition & 0 deletions tests/common/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::format_err;
use anyhow::{bail, Result};
use clap::Parser;
use maturin::BuildOptions;
use pretty_assertions::assert_eq;

pub fn abi3_without_version() -> Result<()> {
// The first argument is ignored by clap
Expand Down
7 changes: 4 additions & 3 deletions tests/common/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use anyhow::{Context, Result};
use clap::Parser;
use flate2::read::GzDecoder;
use maturin::{BuildOptions, CargoOptions};
use std::collections::HashSet;
use pretty_assertions::assert_eq;
use std::collections::BTreeSet;
use std::iter::FromIterator;
use std::path::{Path, PathBuf};
use tar::Archive;
Expand Down Expand Up @@ -139,14 +140,14 @@ pub fn test_source_distribution(
let tar_gz = fs_err::File::open(path)?;
let tar = GzDecoder::new(tar_gz);
let mut archive = Archive::new(tar);
let mut files = HashSet::new();
let mut files = BTreeSet::new();
for entry in archive.entries()? {
let entry = entry?;
files.insert(format!("{}", entry.path()?.display()));
}
assert_eq!(
files,
HashSet::from_iter(expected_files.into_iter().map(ToString::to_string))
BTreeSet::from_iter(expected_files.into_iter().map(ToString::to_string))
);
Ok(())
}
Expand Down

0 comments on commit 6c597cb

Please sign in to comment.