Skip to content

Commit

Permalink
Add support for Cargo workspace inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Sep 23, 2022
1 parent 71ccc6b commit 9cdc479
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 10 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Fix `Cargo.toml` in new project template in [#1109](https://github.com/PyO3/maturin/pull/1109)
* Fix `maturin develop` on Windows when using Python installed from msys2 in [#1112](https://github.com/PyO3/maturin/pull/1112)
* Fix duplicated `Cargo.toml` of local dependencies in sdist in [#1114](https://github.com/PyO3/maturin/pull/1114)
* Add support for Cargo workspace inheritance in [#1123](https://github.com/PyO3/maturin/pull/1123)

## [0.13.3] - 2022-09-15

Expand Down
35 changes: 25 additions & 10 deletions src/source_distribution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,28 @@ fn rewrite_cargo_toml(
if let Some(table) = data.get_mut(*dep_category).and_then(|x| x.as_table_mut()) {
let dep_names: Vec<_> = table.iter().map(|(key, _)| key.to_string()).collect();
for dep_name in dep_names {
// There should either be no value for path, or it should be a string
if table.get(&dep_name).and_then(|x| x.get("path")).is_none() {
continue;
let mut workspace_inherit = false;
if let Some(true) = table
.get(&dep_name)
.and_then(|x| x.get("workspace"))
.and_then(|x| x.as_bool())
{
workspace_inherit = true;
}
if !table[&dep_name]["path"].is_str() {
bail!(
"In {}, {} {} has a path value that is not a string",
manifest_path.as_ref().display(),
dep_category,
dep_name
)

if !workspace_inherit {
// There should either be no value for path, or it should be a string
if table.get(&dep_name).and_then(|x| x.get("path")).is_none() {
continue;
}
if !table[&dep_name]["path"].is_str() {
bail!(
"In {}, {} {} has a path value that is not a string",
manifest_path.as_ref().display(),
dep_category,
dep_name
)
}
}
if !known_path_deps.contains_key(&dep_name) {
bail!(
Expand All @@ -69,6 +80,10 @@ fn rewrite_cargo_toml(
// Cargo.toml contains relative paths, and we're already in LOCAL_DEPENDENCIES_FOLDER
toml_edit::value(format!("../{}", dep_name))
};
if workspace_inherit {
// Remove workspace inheritance now that we converted it into a path dependency
table[&dep_name].as_table_mut().unwrap().remove("workspace");
}
rewritten = true;
}
}
Expand Down
254 changes: 254 additions & 0 deletions test-crates/workspace-inheritance/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions test-crates/workspace-inheritance/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[workspace]
members = [
"generic_lib",
"python"
]

[workspace.dependencies]
generic_lib = { path = "generic_lib" }
8 changes: 8 additions & 0 deletions test-crates/workspace-inheritance/generic_lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "generic_lib"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
3 changes: 3 additions & 0 deletions test-crates/workspace-inheritance/generic_lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn foo() -> &'static str {
"foo"
}
13 changes: 13 additions & 0 deletions test-crates/workspace-inheritance/python/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "workspace-inheritance"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "workspace_inheritance"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.16.5", features = ["extension-module"] }
generic_lib.workspace = true
3 changes: 3 additions & 0 deletions test-crates/workspace-inheritance/python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["maturin>=0.13,<0.14"]
build-backend = "maturin"
14 changes: 14 additions & 0 deletions test-crates/workspace-inheritance/python/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use pyo3::prelude::*;

/// Formats the sum of two numbers as string.
#[pyfunction]
fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
Ok((a + b).to_string())
}

/// A Python module implemented in Rust.
#[pymodule]
fn workspace_with_path_dep(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(sum_as_string, m)?)?;
Ok(())
}
16 changes: 16 additions & 0 deletions tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,22 @@ fn workspace_with_path_dep_sdist() {
))
}

#[test]
fn workspace_inheritance_sdist() {
handle_result(other::test_source_distribution(
"test-crates/workspace-inheritance/python",
vec![
"workspace_inheritance-0.1.0/local_dependencies/generic_lib/Cargo.toml",
"workspace_inheritance-0.1.0/local_dependencies/generic_lib/src/lib.rs",
"workspace_inheritance-0.1.0/Cargo.toml",
"workspace_inheritance-0.1.0/pyproject.toml",
"workspace_inheritance-0.1.0/src/lib.rs",
"workspace_inheritance-0.1.0/PKG-INFO",
],
"workspace_inheritance_sdist",
))
}

#[test]
fn abi3_python_interpreter_args() {
handle_result(other::abi3_python_interpreter_args());
Expand Down

0 comments on commit 9cdc479

Please sign in to comment.