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

Fix sdist with local path dependencies on Windows #580

Merged
merged 2 commits into from
Jun 29, 2021
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
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Support Aarch64 on OpenBSD in [#570](https://github.com/PyO3/maturin/pull/570)
* Support Aarch64 on FreeBSD in [#571](https://github.com/PyO3/maturin/pull/571)
* `Cargo.toml`'s `authors` field is now optional per Rust [RFC 3052](https://github.com/rust-lang/rfcs/blob/master/text/3052-optional-authors-field.md) in [#573](https://github.com/PyO3/maturin/pull/573)
* Fix source distribution with local path dependencies on Windows in [#580](https://github.com/PyO3/maturin/pull/580)

## 0.10.6 - 2021-05-21

Expand Down
31 changes: 20 additions & 11 deletions src/source_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::{Metadata21, SDistWriter};
use anyhow::{bail, Context, Result};
use cargo_metadata::Metadata;
use fs_err as fs;
use regex::Regex;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::Command;
Expand All @@ -20,7 +19,7 @@ const LOCAL_DEPENDENCIES_FOLDER: &str = "local_dependencies";
/// This method is rather frail, but unfortunately I don't know a better solution.
fn rewrite_cargo_toml(
manifest_path: impl AsRef<Path>,
known_path_deps: &HashMap<String, String>,
known_path_deps: &HashMap<String, PathBuf>,
root_crate: bool,
) -> Result<String> {
let text = fs::read_to_string(&manifest_path).context(format!(
Expand Down Expand Up @@ -82,7 +81,7 @@ fn add_crate_to_source_distribution(
writer: &mut SDistWriter,
manifest_path: impl AsRef<Path>,
prefix: impl AsRef<Path>,
known_path_deps: &HashMap<String, String>,
known_path_deps: &HashMap<String, PathBuf>,
root_crate: bool,
) -> Result<()> {
let output = Command::new("cargo")
Expand Down Expand Up @@ -170,19 +169,28 @@ pub fn source_distribution(
sdist_include: Option<&Vec<String>>,
) -> Result<PathBuf> {
// Parse ids in the format:
// some_path_dep 0.1.0 (path+file:///home/konsti/maturin/test-crates/some_path_dep)
// on unix: some_path_dep 0.1.0 (path+file:///home/konsti/maturin/test-crates/some_path_dep)
// on windows: some_path_dep 0.1.0 (path+file:///C:/konsti/maturin/test-crates/some_path_dep)
// This is not a good way to identify path dependencies, but I don't know a better one
let matcher = Regex::new(r"^(.*) .* \(path\+file://(.*)\)$").unwrap();
let resolve = cargo_metadata
.resolve
.as_ref()
.context("Expected to get a dependency graph from cargo")?;
let known_path_deps: HashMap<String, String> = resolve
let known_path_deps: HashMap<String, PathBuf> = resolve
.nodes
.iter()
.filter(|node| &node.id != resolve.root.as_ref().unwrap())
.filter_map(|node| matcher.captures(&node.id.repr))
.map(|captures| (captures[1].to_string(), captures[2].to_string()))
.filter(|node| {
&node.id != resolve.root.as_ref().unwrap() && node.id.repr.contains("path+file://")
})
.filter_map(|node| {
cargo_metadata.packages.iter().find_map(|pkg| {
if pkg.id.repr == node.id.repr {
Some((pkg.name.clone(), PathBuf::from(&pkg.manifest_path)))
} else {
None
}
})
})
.collect();

let mut writer = SDistWriter::new(wheel_dir, &metadata21)?;
Expand All @@ -196,14 +204,15 @@ pub fn source_distribution(
for (name, path) in known_path_deps.iter() {
add_crate_to_source_distribution(
&mut writer,
&PathBuf::from(path).join("Cargo.toml"),
&path,
&root_dir.join(LOCAL_DEPENDENCIES_FOLDER).join(name),
&known_path_deps,
false,
)
.context(format!(
"Failed to add local dependency {} at {} to the source distribution",
name, path
name,
path.display()
))?;
}

Expand Down