Skip to content

Commit

Permalink
Merge pull request #1187 from messense/fix-src-layout-sdist
Browse files Browse the repository at this point in the history
Fix sdist build failure when using python first `src` layout
  • Loading branch information
messense authored Oct 11, 2022
2 parents 0370ab7 + d6a3e15 commit 1f02d20
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ venv*/
.pytest_cache/
.tox/
*.so
*.py[cdo]
__pycache__/
*.egg-info/
*.egg
Expand Down
62 changes: 45 additions & 17 deletions src/source_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str;
use tracing::debug;

const LOCAL_DEPENDENCIES_FOLDER: &str = "local_dependencies";
/// Inheritable workspace fields, see
Expand Down Expand Up @@ -279,7 +280,11 @@ fn add_crate_to_source_distribution(
.map(|relative_to_manifests| {
let relative_to_cwd = manifest_dir.join(relative_to_manifests);
if root_crate && cargo_toml_in_subdir {
(relative_to_cwd.clone(), relative_to_cwd)
let relative_to_project_root = abs_manifest_dir
.strip_prefix(&pyproject_dir)
.unwrap()
.join(relative_to_manifests);
(relative_to_project_root, relative_to_cwd)
} else {
(relative_to_manifests.to_path_buf(), relative_to_cwd)
}
Expand Down Expand Up @@ -337,7 +342,8 @@ fn add_crate_to_source_distribution(
writer.add_directory(prefix)?;

let cargo_toml = if cargo_toml_in_subdir {
prefix.join(manifest_path)
let relative_manifest_path = abs_manifest_path.strip_prefix(pyproject_dir)?;
prefix.join(relative_manifest_path)
} else {
prefix.join(manifest_path.file_name().unwrap())
};
Expand Down Expand Up @@ -468,21 +474,53 @@ pub fn source_distribution(
true,
)?;

let manifest_dir = manifest_path.parent().unwrap();
let cargo_lock_path = manifest_dir.join("Cargo.lock");
let abs_manifest_path = fs::canonicalize(manifest_path)?;
let abs_manifest_dir = abs_manifest_path.parent().unwrap();
let cargo_lock_path = abs_manifest_dir.join("Cargo.lock");
let cargo_lock_required =
build_context.cargo_options.locked || build_context.cargo_options.frozen;
if cargo_lock_required || cargo_lock_path.exists() {
writer.add_file(root_dir.join("Cargo.lock"), &cargo_lock_path)?;
let project_root = pyproject_toml_path.parent().unwrap();
let relative_cargo_lock = if cargo_lock_path.starts_with(project_root) {
cargo_lock_path.strip_prefix(project_root)?
} else {
cargo_lock_path.strip_prefix(&abs_manifest_dir)?
};
writer.add_file(root_dir.join(relative_cargo_lock), &cargo_lock_path)?;
} else {
println!(
"⚠️ Warning: Cargo.lock is not found, it is recommended \
to include it in the source distribution"
);
}

// Add readme, license and python source files
let pyproject_dir = pyproject_toml_path.parent().unwrap();
// Add python source files
if let Some(python_source) = build_context.project_layout.python_module.as_ref() {
for entry in ignore::Walk::new(python_source) {
let source = entry?.into_path();
// Technically, `ignore` crate should handle this,
// but somehow it doesn't on Alpine Linux running in GitHub Actions,
// so we do it manually here.
// See https://github.com/PyO3/maturin/pull/1187#issuecomment-1273987013
if source
.extension()
.map(|ext| ext == "pyc" || ext == "pyd" || ext == "so")
.unwrap_or_default()
{
debug!("Ignoring {}", source.display());
continue;
}
let target = root_dir.join(source.strip_prefix(&pyproject_dir)?);
if source.is_dir() {
writer.add_directory(target)?;
} else {
writer.add_file(target, &source)?;
}
}
}

// Add readme, license
if let Some(project) = pyproject.project.as_ref() {
if let Some(pyproject_toml::ReadMe::RelativePath(readme)) = project.readme.as_ref() {
writer.add_file(root_dir.join(readme), pyproject_dir.join(readme))?;
Expand All @@ -494,18 +532,8 @@ pub fn source_distribution(
{
writer.add_file(root_dir.join(license), pyproject_dir.join(license))?;
}
if let Some(python_source) = pyproject.python_source() {
for entry in ignore::Walk::new(pyproject_dir.join(python_source)) {
let source = entry?.into_path();
let target = root_dir.join(source.strip_prefix(&pyproject_dir)?);
if source.is_dir() {
writer.add_directory(target)?;
} else {
writer.add_file(target, &source)?;
}
}
}
}

if let Some(include_targets) = pyproject.sdist_include() {
for pattern in include_targets {
println!("📦 Including files matching \"{}\"", pattern);
Expand Down
21 changes: 20 additions & 1 deletion tests/run.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! To speed up the tests, they are tests all collected in a single module
use common::{develop, editable, errors, handle_result, integration, other};
use std::path::Path;

mod common;

Expand Down Expand Up @@ -292,6 +291,8 @@ fn integration_with_data() {
),
))]
fn integration_wasm_hello_world() {
use std::path::Path;

handle_result(integration::test_integration(
"test-crates/hello-world",
None,
Expand Down Expand Up @@ -368,6 +369,24 @@ fn lib_with_path_dep_sdist() {
))
}

#[test]
fn pyo3_mixed_src_layout_sdist() {
handle_result(other::test_source_distribution(
"test-crates/pyo3-mixed-src/rust",
vec![
"pyo3_mixed_src-2.1.3/pyproject.toml",
"pyo3_mixed_src-2.1.3/src/pyo3_mixed_src/__init__.py",
"pyo3_mixed_src-2.1.3/src/pyo3_mixed_src/python_module/__init__.py",
"pyo3_mixed_src-2.1.3/src/pyo3_mixed_src/python_module/double.py",
"pyo3_mixed_src-2.1.3/rust/Cargo.toml",
"pyo3_mixed_src-2.1.3/rust/Cargo.lock",
"pyo3_mixed_src-2.1.3/rust/src/lib.rs",
"pyo3_mixed_src-2.1.3/PKG-INFO",
],
"sdist-pyo3-mixed-src-layout",
))
}

#[test]
fn workspace_with_path_dep_sdist() {
handle_result(other::test_source_distribution(
Expand Down

0 comments on commit 1f02d20

Please sign in to comment.