From 02f5764eefaf98dc8d1400994c6ab06781983b09 Mon Sep 17 00:00:00 2001 From: konstin Date: Sat, 13 May 2023 01:36:17 +0200 Subject: [PATCH] When determining the python module name, use pyproject.toml `project.name` over Cargo.toml `package.name`. I propose to change the precedence for determining the module name to consider before pyproject.toml `project.name` over Cargo.toml `package.name`. This came up in ruff (https://github.com/charliermarsh/ruff/pull/4397, https://github.com/charliermarsh/ruff/pull/4399), where the crate name is `ruff_cli` and the project name is `ruff`. I'm not sure if there are any cases a user would like the crate name over the package name. --- Changelog.md | 2 ++ src/project_layout.rs | 21 ++++++++++++--------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/Changelog.md b/Changelog.md index 2f42592f7..8619146ee 100644 --- a/Changelog.md +++ b/Changelog.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] + * When determining the python module name, use pyproject.toml `project.name` over Cargo.toml `package.name`. + ## [0.15.1] - 2023-05-07 * Fix finding interpreters from bundled sysconfigs in [#1598](https://github.com/PyO3/maturin/pull/1598) diff --git a/src/project_layout.rs b/src/project_layout.rs index 0852d5459..63ee06603 100644 --- a/src/project_layout.rs +++ b/src/project_layout.rs @@ -119,17 +119,20 @@ impl ProjectResolver { // If the package name contains minuses, you must declare a module with // underscores as lib name - let module_name = cargo_toml - .lib - .as_ref() - .and_then(|lib| lib.name.as_ref()) + // Precedence: + // * Explicitly declared pyproject.toml `tool.maturin.module-name` + // * Cargo.toml `lib.name` + // * pyproject.toml `project.name` + // * Cargo.toml `package.name` + let module_name = pyproject + .and_then(|x| x.module_name()) + .or(cargo_toml.lib.as_ref().and_then(|lib| lib.name.as_deref())) + .or(pyproject + .and_then(|pyproject| pyproject.project.as_ref()) + .map(|project| project.name.as_str())) .unwrap_or(crate_name) .to_owned(); - let extension_name = pyproject - .and_then(|x| x.module_name()) - .unwrap_or(&module_name); - let project_root = if pyproject_file.is_file() { pyproject_file.parent().unwrap_or(manifest_dir) } else { @@ -176,7 +179,7 @@ impl ProjectResolver { } }); let project_layout = - ProjectLayout::determine(project_root, extension_name, py_root, python_packages, data)?; + ProjectLayout::determine(project_root, &module_name, py_root, python_packages, data)?; Ok(Self { project_layout, cargo_toml_path: manifest_file,