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

Warn about incorrect version in build-system.requires #1793

Merged
merged 1 commit into from
Oct 2, 2023
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
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

* Warning about incorrect maturin version pyproject.toml `[build-system] requires`

## [1.2.3] - 2023-08-17

* Fix sdist build failure with workspace path dependencies by HerringtonDarkholme in [#1739](https://github.com/PyO3/maturin/pull/1739)
Expand Down
2 changes: 1 addition & 1 deletion src/project_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl ProjectResolver {
let manifest_dir = manifest_file.parent().unwrap();
let pyproject_toml: Option<PyProjectToml> = if pyproject_file.is_file() {
let pyproject = PyProjectToml::new(&pyproject_file)?;
pyproject.warn_missing_maturin_version();
pyproject.warn_bad_maturin_version();
pyproject.warn_missing_build_backend();
Some(pyproject)
} else {
Expand Down
83 changes: 64 additions & 19 deletions src/pyproject_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
use crate::PlatformTag;
use anyhow::{Context, Result};
use fs_err as fs;
use pep440_rs::Version;
use pep508_rs::VersionOrUrl;
use pyproject_toml::{BuildSystem, Project};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::str::FromStr;

/// The `[tool]` section of a pyproject.toml
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
Expand Down Expand Up @@ -298,30 +301,52 @@ impl PyProjectToml {
self.maturin()?.manifest_path.as_deref()
}

/// Warn about `build-system.requires` mismatching expectations.
///
/// Having a pyproject.toml without a version constraint is a bad idea
/// because at some point we'll have to do breaking changes and then source
/// distributions would break
/// distributions would break.
///
/// The second problem we check for is the current maturin version not matching the constraint.
///
/// Returns true if the pyproject.toml has the constraint
pub fn warn_missing_maturin_version(&self) -> bool {
/// Returns false if a warning was emitted.
pub fn warn_bad_maturin_version(&self) -> bool {
let maturin = env!("CARGO_PKG_NAME");
if let Some(requires_maturin) = self
let current_major = env!("CARGO_PKG_VERSION_MAJOR").parse::<usize>().unwrap();
let self_version = Version::from_str(env!("CARGO_PKG_VERSION")).unwrap();
let requires_maturin = self
.build_system
.requires
.iter()
.find(|x| x.name == maturin)
{
let current_major: usize = env!("CARGO_PKG_VERSION_MAJOR").parse().unwrap();
if requires_maturin.version_or_url.is_none() {
eprintln!(
"⚠️ Warning: Please use {maturin} in pyproject.toml with a version constraint, \
e.g. `requires = [\"{maturin}>={current}.0,<{next}.0\"]`. \
This will become an error.",
maturin = maturin,
current = current_major,
next = current_major + 1,
);
return false;
.find(|x| x.name == maturin);
if let Some(requires_maturin) = requires_maturin {
match requires_maturin.version_or_url.as_ref() {
Some(VersionOrUrl::VersionSpecifier(version_specifier)) => {
if !version_specifier.contains(&self_version) {
eprintln!(
"⚠️ Warning: You specified {requires_maturin} in pyproject.toml under \
`build-system.requires`, but the current {maturin} version is {version}",
requires_maturin = requires_maturin,
maturin = maturin,
version = self_version,
);
return false;
}
}
Some(VersionOrUrl::Url(_)) => {
// We can't check this
}
None => {
eprintln!(
"⚠️ Warning: Please use {maturin} in pyproject.toml with a version constraint, \
e.g. `requires = [\"{maturin}>={current}.0,<{next}.0\"]`. \
This will become an error.",
maturin = maturin,
current = current_major,
next = current_major + 1,
);
return false;
}
}
}
true
Expand Down Expand Up @@ -421,7 +446,7 @@ mod tests {
#[test]
fn test_warn_missing_maturin_version() {
let with_constraint = PyProjectToml::new("test-crates/pyo3-pure/pyproject.toml").unwrap();
assert!(with_constraint.warn_missing_maturin_version());
assert!(with_constraint.warn_bad_maturin_version());
let without_constraint_dir = TempDir::new().unwrap();
let pyproject_file = without_constraint_dir.path().join("pyproject.toml");

Expand All @@ -437,7 +462,27 @@ mod tests {
)
.unwrap();
let without_constraint = PyProjectToml::new(pyproject_file).unwrap();
assert!(!without_constraint.warn_missing_maturin_version());
assert!(!without_constraint.warn_bad_maturin_version());
}

#[test]
fn test_warn_incorrect_maturin_version() {
let without_constraint_dir = TempDir::new().unwrap();
let pyproject_file = without_constraint_dir.path().join("pyproject.toml");

fs::write(
&pyproject_file,
r#"[build-system]
requires = ["maturin==0.0.1"]
build-backend = "maturin"

[tool.maturin]
manylinux = "2010"
"#,
)
.unwrap();
let without_constraint = PyProjectToml::new(pyproject_file).unwrap();
assert!(!without_constraint.warn_bad_maturin_version());
}

#[test]
Expand Down
Loading