From 3465e57ecc13f2b0757c0e1e693ee3414ee23376 Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Mon, 20 Oct 2025 20:22:48 +0100 Subject: [PATCH] respect CLI profile over pyproject.toml --- src/build_context.rs | 2 -- src/build_options.rs | 9 -------- src/compile.rs | 5 ----- src/develop.rs | 8 +++++-- src/main.rs | 45 +++++++++++++++++++++++++++---------- tests/common/errors.rs | 5 ----- tests/common/integration.rs | 2 -- tests/common/other.rs | 9 -------- 8 files changed, 39 insertions(+), 46 deletions(-) diff --git a/src/build_context.rs b/src/build_context.rs index 1bfc8c7e7..a85c13d3b 100644 --- a/src/build_context.rs +++ b/src/build_context.rs @@ -113,8 +113,6 @@ pub struct BuildContext { /// The directory to store the built wheels in. Defaults to a new "wheels" /// directory in the project's target directory pub out: PathBuf, - /// Build artifacts in release mode, with optimizations - pub release: bool, /// Strip the library for minimum file size pub strip: bool, /// Checking the linked libraries for manylinux/musllinux compliance diff --git a/src/build_options.rs b/src/build_options.rs index 01cd6cf8b..ed31bc245 100644 --- a/src/build_options.rs +++ b/src/build_options.rs @@ -549,7 +549,6 @@ impl BuildOptions { #[derive(Debug)] pub struct BuildContextBuilder { build_options: BuildOptions, - release: bool, strip: bool, editable: bool, sdist_only: bool, @@ -559,18 +558,12 @@ impl BuildContextBuilder { fn new(build_options: BuildOptions) -> Self { Self { build_options, - release: false, strip: false, editable: false, sdist_only: false, } } - pub fn release(mut self, release: bool) -> Self { - self.release = release; - self - } - pub fn strip(mut self, strip: bool) -> Self { self.strip = strip; self @@ -589,7 +582,6 @@ impl BuildContextBuilder { pub fn build(self) -> Result { let Self { build_options, - release, strip, editable, sdist_only, @@ -819,7 +811,6 @@ impl BuildContextBuilder { manifest_path: cargo_toml_path, target_dir, out: wheel_dir, - release, strip, auditwheel, #[cfg(feature = "zig")] diff --git a/src/compile.rs b/src/compile.rs index 52812deb9..c574eb8e4 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -170,11 +170,6 @@ fn cargo_build_command( .into_rustc_options(user_specified_target); cargo_rustc.message_format = vec!["json-render-diagnostics".to_string()]; - // --release and --profile are conflicting options - if context.release && cargo_rustc.profile.is_none() { - cargo_rustc.release = true; - } - // Add `--crate-type cdylib` if available if compile_target .target diff --git a/src/develop.rs b/src/develop.rs index f1cec46e5..a19311814 100644 --- a/src/develop.rs +++ b/src/develop.rs @@ -390,12 +390,17 @@ pub fn develop(develop_options: DevelopOptions, venv_dir: &Path) -> Result<()> { extras, skip_install, pip_path, - cargo_options, + mut cargo_options, uv, compression, } = develop_options; compression.validate(); + // set profile to release if specified; `--release` and `--profile` are mutually exclusive + if release { + cargo_options.profile = Some("release".to_string()); + } + let mut target_triple = cargo_options.target.clone(); let target = Target::from_target_triple(cargo_options.target.as_ref())?; let python = target.get_venv_python(venv_dir); @@ -429,7 +434,6 @@ pub fn develop(develop_options: DevelopOptions, venv_dir: &Path) -> Result<()> { let build_context = build_options .into_build_context() - .release(release) .strip(strip) .editable(true) .build()?; diff --git a/src/main.rs b/src/main.rs index da594cdf9..317325d75 100644 --- a/src/main.rs +++ b/src/main.rs @@ -273,13 +273,17 @@ fn pep517(subcommand: Pep517Command) -> Result<()> { strip, } => { assert_eq!(build_options.interpreter.len(), 1); - let context = build_options + let mut context = build_options .into_build_context() - .release(true) .strip(strip) .editable(false) .build()?; + // TBD: does `--profile release` do anything here? + if context.cargo_options.profile.is_none() { + context.cargo_options.profile = Some("release".to_string()); + } + let mut writer = PathWriter::from_path(metadata_directory); write_dist_info( &mut writer, @@ -294,12 +298,14 @@ fn pep517(subcommand: Pep517Command) -> Result<()> { strip, editable, } => { - let build_context = build_options + let mut build_context = build_options .into_build_context() - .release(true) .strip(strip) .editable(editable) .build()?; + if build_context.cargo_options.profile.is_none() { + build_context.cargo_options.profile = Some("release".to_string()); + } let wheels = build_context.build_wheels()?; assert_eq!(wheels.len(), 1); println!("{}", wheels[0].0.to_str().unwrap()); @@ -321,7 +327,6 @@ fn pep517(subcommand: Pep517Command) -> Result<()> { }; let build_context = build_options .into_build_context() - .release(false) .strip(false) .editable(false) .sdist_only(true) @@ -363,14 +368,17 @@ fn run() -> Result<()> { match opt.command { Command::Build { - build, + mut build, release, strip, sdist, } => { + // set profile to release if specified; `--release` and `--profile` are mutually exclusive + if release { + build.profile = Some("release".to_string()); + } let build_context = build .into_build_context() - .release(release) .strip(strip) .editable(false) .build()?; @@ -384,20 +392,34 @@ fn run() -> Result<()> { } #[cfg(feature = "upload")] Command::Publish { - build, + mut build, mut publish, debug, no_strip, no_sdist, } => { - let build_context = build + // set profile to dev if specified; `--debug` and `--profile` are mutually exclusive + // + // do it here to take precedence over pyproject.toml profile setting + if debug { + build.profile = Some("dev".to_string()); + } + + let mut build_context = build .into_build_context() - .release(!debug) .strip(!no_strip) .editable(false) .build()?; - if !build_context.release { + // ensure profile always set when publishing + // (respect pyproject.toml if set) + // don't need to check `debug` here, set above to take precedence if set + let profile = build_context + .cargo_options + .profile + .get_or_insert_with(|| "release".to_string()); + + if profile == "dev" { eprintln!("⚠️ Warning: You're publishing debug wheels"); } @@ -462,7 +484,6 @@ fn run() -> Result<()> { }; let build_context = build_options .into_build_context() - .release(false) .strip(false) .editable(false) .sdist_only(true) diff --git a/tests/common/errors.rs b/tests/common/errors.rs index a8a790efa..7c6abd285 100644 --- a/tests/common/errors.rs +++ b/tests/common/errors.rs @@ -24,7 +24,6 @@ pub fn pyo3_no_extension_module() -> Result<()> { let options = BuildOptions::try_parse_from(cli)?; let result = options .into_build_context() - .release(false) .strip(cfg!(feature = "faster-tests")) .editable(false) .build()? @@ -62,7 +61,6 @@ pub fn locked_doesnt_build_without_cargo_lock() -> Result<()> { let options = BuildOptions::try_parse_from(cli)?; let result = options .into_build_context() - .release(false) .strip(cfg!(feature = "faster-tests")) .editable(false) .build(); @@ -100,7 +98,6 @@ pub fn invalid_manylinux_does_not_panic() -> Result<()> { let options: BuildOptions = BuildOptions::try_parse_from(cli)?; let result = options .into_build_context() - .release(false) .strip(cfg!(feature = "faster-tests")) .editable(false) .build()? @@ -169,7 +166,6 @@ pub fn pypi_compatibility_unsupported_target() -> Result<()> { let options: BuildOptions = BuildOptions::try_parse_from(cli)?; let result = options .into_build_context() - .release(false) .strip(cfg!(feature = "faster-tests")) .editable(false) .build(); @@ -210,7 +206,6 @@ pub fn pypi_compatibility_mixed_tags() -> Result<()> { let options: BuildOptions = BuildOptions::try_parse_from(cli)?; let result = options .into_build_context() - .release(false) .strip(cfg!(feature = "faster-tests")) .editable(false) .build(); diff --git a/tests/common/integration.rs b/tests/common/integration.rs index 89196c0c2..2366928c5 100644 --- a/tests/common/integration.rs +++ b/tests/common/integration.rs @@ -132,7 +132,6 @@ pub fn test_integration( let options: BuildOptions = BuildOptions::try_parse_from(cli)?; let build_context = options .into_build_context() - .release(false) .strip(cfg!(feature = "faster-tests")) .editable(false) .build()?; @@ -260,7 +259,6 @@ pub fn test_integration_conda(package: impl AsRef, bindings: Option Result { let build_context = options .into_build_context() - .release(false) .strip(cfg!(feature = "faster-tests")) .editable(false) .build()?; @@ -114,7 +113,6 @@ pub fn test_workspace_cargo_lock() -> Result<()> { let build_context = options .into_build_context() - .release(false) .strip(false) .editable(false) .build()?; @@ -149,7 +147,6 @@ pub fn test_source_distribution( let mut build_context = build_options .into_build_context() - .release(false) .strip(false) .editable(false) .sdist_only(true) @@ -225,7 +222,6 @@ fn build_wheel_files(package: impl AsRef, unique_name: &str) -> Result Result<()> { ])?; let result = options .into_build_context() - .release(false) .strip(cfg!(feature = "faster-tests")) .editable(false) .build(); @@ -303,7 +298,6 @@ pub fn abi3_python_interpreter_args() -> Result<()> { ])?; let result = options .into_build_context() - .release(false) .strip(cfg!(feature = "faster-tests")) .editable(false) .build(); @@ -324,7 +318,6 @@ pub fn abi3_python_interpreter_args() -> Result<()> { ])?; let result = options .into_build_context() - .release(false) .strip(cfg!(feature = "faster-tests")) .editable(false) .build(); @@ -341,7 +334,6 @@ pub fn abi3_python_interpreter_args() -> Result<()> { ])?; let result = options .into_build_context() - .release(false) .strip(cfg!(feature = "faster-tests")) .editable(false) .build(); @@ -367,7 +359,6 @@ pub fn abi3_without_version() -> Result<()> { let options = BuildOptions::try_parse_from(cli)?; let result = options .into_build_context() - .release(false) .strip(cfg!(feature = "faster-tests")) .editable(false) .build();