Skip to content

Commit 7b5d227

Browse files
Let --no-package and --script take precedence over UV_INIT_BUILD_BACKEND (#22907)
## Summary with `UV_INIT_BUILD_BACKEND` set, `uv init --app --no-package` and `uv init --script` failed with "the argument '--no-package' cannot be used with '--build-backend'" even though no `--build-backend` was passed, since clap applies `conflicts_with` to values from env vars too. since `uv init` creates a package by default now, `--no-package` is the way to get the non packaged app from this issue. `EnvironmentOptions` now parses `UV_INIT_BUILD_BACKEND` alongside the other typed environment settings. `uv init` uses that default when it needs a backend. An explicit `--build-backend` takes precedence, and `--script`, `--no-package`, and `--virtual` ignore a valid default. An invalid value fails any command that loads environment settings, including `uv sync`. this isnt the conflict on `--app` suggested earlier in the thread, with packaged init as the default that would break valid commands like `uv init --app --build-backend hatchling`. Closes #17335 ## Test Plan added `init_application_no_package_build_backend_env`, `init_script_build_backend_env` and `init_build_backend_env_invalid`. `init::` tests pass, checked by hand that the env var still sets the backend for `--app`, `--lib` and `--bare`. --------- Co-authored-by: Charlie Marsh <[email protected]>
1 parent c3bcf4b commit 7b5d227

6 files changed

Lines changed: 124 additions & 7 deletions

File tree

‎clippy.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ doc-valid-idents = [
1414
"AArch64",
1515
"UV_DEV",
1616
"UV_FROZEN",
17+
"UV_INIT_BUILD_BACKEND",
1718
"UV_ISOLATED",
1819
"UV_LOCKED",
1920
"UV_MANAGED_PYTHON",

‎crates/uv-cli/src/lib.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3179,10 +3179,10 @@ pub struct InitArgs {
31793179
#[arg(long, value_enum, conflicts_with = "script")]
31803180
vcs: Option<VersionControlSystem>,
31813181

3182-
/// Initialize a build-backend of choice for the project.
3182+
/// Initialize a build-backend of choice for the project [env: UV_INIT_BUILD_BACKEND=]
31833183
///
31843184
/// Implicitly sets `--package`.
3185-
#[arg(long, value_enum, conflicts_with_all=["script", "no_package"], env = EnvVars::UV_INIT_BUILD_BACKEND)]
3185+
#[arg(long, value_enum, conflicts_with_all=["script", "no_package"])]
31863186
build_backend: Option<ProjectBuildBackend>,
31873187

31883188
/// Invalid option name for build backend.

‎crates/uv-cli/src/settings.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,14 @@ impl InitSettings {
522522

523523
let no_description = no_description || (bare && description.is_none());
524524

525+
// `UV_INIT_BUILD_BACKEND` only sets a default, so it yields to `--no-package` and
526+
// `--script` instead of conflicting with them like `--build-backend` does.
527+
let build_backend = match build_backend {
528+
Some(build_backend) => Some(build_backend),
529+
None if script || no_package || r#virtual => None,
530+
None => environment.init_build_backend,
531+
};
532+
525533
if r#virtual && lib {
526534
bail!("`--virtual` and `--lib` are mutually exclusive");
527535
}

‎crates/uv-settings/Cargo.toml‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ thiserror = { workspace = true }
4949
toml = { workspace = true }
5050
tracing = { workspace = true }
5151

52-
[package.metadata.cargo-shear]
53-
ignored = ["clap"]
54-
5552
[features]
5653
schemars = [
5754
"dep:schemars",

‎crates/uv-settings/src/lib.rs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@ use std::ops::Deref;
33
use std::path::{Path, PathBuf};
44
use std::str::FromStr;
55
use std::time::Duration;
6+
7+
use clap::ValueEnum;
68
use tracing::info_span;
79
use uv_client::{DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT, DEFAULT_READ_TIMEOUT_UPLOAD};
8-
use uv_configuration::{RequiredVersion, RequirementsInput, RequirementsInputError};
10+
use uv_configuration::{
11+
ProjectBuildBackend, RequiredVersion, RequirementsInput, RequirementsInputError,
12+
};
913
use uv_dirs::{system_config_file, user_config_dir};
1014
use uv_distribution_types::{IndexUrlError, Origin};
1115
use uv_flags::EnvironmentFlags;
@@ -806,6 +810,7 @@ pub struct EnvironmentOptions {
806810
pub venv_clear: EnvFlag,
807811
pub venv_relocatable: EnvFlag,
808812
pub init_bare: EnvFlag,
813+
pub init_build_backend: Option<ProjectBuildBackend>,
809814
pub malware_check: EnvFlag,
810815
pub malware_check_url: Option<SafeUrl>,
811816
pub run_recursion_depth: Option<u32>,
@@ -962,6 +967,17 @@ impl EnvironmentOptions {
962967
venv_clear: EnvFlag::new(EnvVars::UV_VENV_CLEAR)?,
963968
venv_relocatable: EnvFlag::new(EnvVars::UV_VENV_RELOCATABLE)?,
964969
init_bare: EnvFlag::new(EnvVars::UV_INIT_BARE)?,
970+
init_build_backend: parse_string_environment_variable(EnvVars::UV_INIT_BUILD_BACKEND)?
971+
.map(|value| {
972+
ProjectBuildBackend::from_str(&value, false).map_err(|err| {
973+
InvalidEnvironmentVariable {
974+
name: EnvVars::UV_INIT_BUILD_BACKEND.to_string(),
975+
value,
976+
err,
977+
}
978+
})
979+
})
980+
.transpose()?,
965981
malware_check: EnvFlag::new(EnvVars::UV_MALWARE_CHECK)?,
966982
malware_check_url: parse_string_environment_variable(EnvVars::UV_MALWARE_CHECK_URL)?
967983
.map(|value| {

‎crates/uv/tests/project/init.rs‎

Lines changed: 96 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,94 @@ fn init_application_no_package_other_python_exists() -> Result<()> {
276276
Ok(())
277277
}
278278

279+
/// `--no-package` takes precedence over `UV_INIT_BUILD_BACKEND`, but not over `--build-backend`.
280+
///
281+
/// See: <https://github.com/astral-sh/uv/issues/17335>
282+
#[test]
283+
fn init_application_no_package_build_backend_env() -> Result<()> {
284+
let context = uv_test::test_context!("3.12");
285+
286+
let child = context.temp_dir.child("foo");
287+
child.create_dir_all()?;
288+
289+
uv_snapshot!(context.filters(), context.init().current_dir(&child).arg("--app").arg("--no-package").arg("--build-backend").arg("setuptools"), @"
290+
exit_code: 2 (failure)
291+
----- stderr -----
292+
error: the argument '--no-package' cannot be used with '--build-backend <BUILD_BACKEND>'
293+
294+
Usage: uv init --cache-dir [CACHE_DIR] --app --no-package [PATH]
295+
296+
For more information, try '--help'.
297+
");
298+
299+
uv_snapshot!(context.filters(), context.init().current_dir(&child).arg("--app").arg("--no-package").env(EnvVars::UV_INIT_BUILD_BACKEND, "setuptools"), @"
300+
exit_code: 0 (success)
301+
----- stderr -----
302+
Initialized project `foo`
303+
");
304+
305+
let pyproject = context.read("foo/pyproject.toml");
306+
insta::with_settings!({
307+
filters => context.filters(),
308+
}, {
309+
assert_snapshot!(
310+
pyproject, @r#"
311+
[project]
312+
name = "foo"
313+
version = "0.1.0"
314+
description = "Add your description here"
315+
readme = "README.md"
316+
requires-python = ">=3.12"
317+
dependencies = []
318+
"#
319+
);
320+
});
321+
322+
child.child("main.py").assert(predicate::path::is_file());
323+
child.child("src").assert(predicate::path::missing());
324+
325+
Ok(())
326+
}
327+
328+
/// `--script` takes precedence over `UV_INIT_BUILD_BACKEND`.
329+
#[test]
330+
fn init_script_build_backend_env() -> Result<()> {
331+
let context = uv_test::test_context!("3.12");
332+
333+
let child = context.temp_dir.child("foo");
334+
child.create_dir_all()?;
335+
336+
uv_snapshot!(context.filters(), context.init().current_dir(&child).arg("--script").arg("main.py").env(EnvVars::UV_INIT_BUILD_BACKEND, "setuptools"), @"
337+
exit_code: 0 (success)
338+
----- stderr -----
339+
Initialized script at `main.py`
340+
");
341+
342+
child
343+
.child("pyproject.toml")
344+
.assert(predicate::path::missing());
345+
346+
Ok(())
347+
}
348+
349+
/// Environment settings are validated even when the command does not use a build backend.
350+
#[test]
351+
fn init_build_backend_env_invalid() {
352+
let context = uv_test::test_context!("3.12");
353+
354+
uv_snapshot!(context.filters(), context.init().arg("--no-package").env(EnvVars::UV_INIT_BUILD_BACKEND, "unknown"), @"
355+
exit_code: 2 (failure)
356+
----- stderr -----
357+
error: Failed to parse environment variable `UV_INIT_BUILD_BACKEND` with invalid value `unknown`: invalid variant: unknown
358+
");
359+
360+
uv_snapshot!(context.filters(), context.sync().env(EnvVars::UV_INIT_BUILD_BACKEND, "unknown"), @"
361+
exit_code: 2 (failure)
362+
----- stderr -----
363+
error: Failed to parse environment variable `UV_INIT_BUILD_BACKEND` with invalid value `unknown`: invalid variant: unknown
364+
");
365+
}
366+
279367
/// Run `uv init --app --package` to create a packaged application project
280368
#[test]
281369
fn init_application_package() -> Result<()> {
@@ -3188,7 +3276,7 @@ fn init_library_flit() -> Result<()> {
31883276
fn init_backend_implies_package() {
31893277
let context = uv_test::test_context!("3.12");
31903278

3191-
uv_snapshot!(context.filters(), context.init().arg("project").arg("--build-backend").arg("flit"), @"
3279+
uv_snapshot!(context.filters(), context.init().arg("project").arg("--build-backend").arg("flit").env(EnvVars::UV_INIT_BUILD_BACKEND, "setuptools"), @"
31923280
exit_code: 0 (success)
31933281
----- stderr -----
31943282
Initialized project `project` at `[TEMP_DIR]/project`
@@ -3217,6 +3305,13 @@ fn init_backend_implies_package() {
32173305
"#
32183306
);
32193307
});
3308+
3309+
uv_snapshot!(context.filters(), context.init().arg("from-env/project").env(EnvVars::UV_INIT_BUILD_BACKEND, "flit-core"), @"
3310+
exit_code: 0 (success)
3311+
----- stderr -----
3312+
Initialized project `project` at `[TEMP_DIR]/from-env/project`
3313+
");
3314+
assert_eq!(context.read("from-env/project/pyproject.toml"), pyproject);
32203315
}
32213316

32223317
/// Run `uv init --build-backend poetry` to create a project with poetry-core build backend

0 commit comments

Comments
 (0)