From 7645f22af7f4fa5a554dd7fcfabd0a217b5a60c8 Mon Sep 17 00:00:00 2001 From: j178 <10510431+j178@users.noreply.github.com> Date: Sat, 26 Oct 2024 22:07:16 +0800 Subject: [PATCH] uv init: Implies `--package` when using `--build-backend` --- crates/uv-cli/src/lib.rs | 8 +++++--- crates/uv/src/settings.rs | 3 ++- crates/uv/tests/it/init.rs | 39 ++++++++++++++++++++++++++++++++++++++ docs/reference/cli.md | 6 ++++-- 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/crates/uv-cli/src/lib.rs b/crates/uv-cli/src/lib.rs index 4aa2a0820cd1..84f270f14922 100644 --- a/crates/uv-cli/src/lib.rs +++ b/crates/uv-cli/src/lib.rs @@ -2473,7 +2473,7 @@ pub struct InitArgs { /// /// Defines a `[build-system]` for the project. /// - /// This is the default behavior when using `--lib`. + /// This is the default behavior when using `--lib` or `--build-backend`. /// /// When using `--app`, this will include a `[project.scripts]` entrypoint and use a `src/` /// project structure. @@ -2485,7 +2485,7 @@ pub struct InitArgs { /// Does not include a `[build-system]` for the project. /// /// This is the default behavior when using `--app`. - #[arg(long, overrides_with = "package", conflicts_with = "lib")] + #[arg(long, overrides_with = "package", conflicts_with_all = ["lib", "build_backend"])] pub r#no_package: bool, /// Create a project for an application. @@ -2515,7 +2515,7 @@ pub struct InitArgs { /// /// By default, adds a requirement on the system Python version; use `--python` to specify an /// alternative Python version requirement. - #[arg(long, alias="script", conflicts_with_all=["app", "lib", "package"])] + #[arg(long, alias="script", conflicts_with_all=["app", "lib", "package", "build_backend"])] pub r#script: bool, /// Initialize a version control system for the project. @@ -2526,6 +2526,8 @@ pub struct InitArgs { pub vcs: Option, /// Initialize a build-backend of choice for the project. + /// + /// Implicitly sets `--package`. #[arg(long, value_enum, conflicts_with_all=["script", "no_package"])] pub build_backend: Option, diff --git a/crates/uv/src/settings.rs b/crates/uv/src/settings.rs index 11237f6fcc56..5b731cb6ea34 100644 --- a/crates/uv/src/settings.rs +++ b/crates/uv/src/settings.rs @@ -202,7 +202,8 @@ impl InitSettings { (_, _, _) => unreachable!("`app`, `lib`, and `script` are mutually exclusive"), }; - let package = flag(package || r#virtual, no_package).unwrap_or(kind.packaged_by_default()); + let package = flag(package || build_backend.is_some() || r#virtual, no_package) + .unwrap_or(kind.packaged_by_default()); Self { path, diff --git a/crates/uv/tests/it/init.rs b/crates/uv/tests/it/init.rs index 808eb4796b1b..81d00164d2d9 100644 --- a/crates/uv/tests/it/init.rs +++ b/crates/uv/tests/it/init.rs @@ -2536,6 +2536,45 @@ fn init_library_flit() -> Result<()> { Ok(()) } +/// Run `uv init --build-backend flit` should be equivalent to `uv init --package --build-backend flit`. +#[test] +fn init_backend_implies_package() { + let context = TestContext::new("3.12"); + + uv_snapshot!(context.filters(), context.init().arg("project").arg("--build-backend").arg("flit"), @r#" + success: true + exit_code: 0 + ----- stdout ----- + + ----- stderr ----- + Initialized project `project` at `[TEMP_DIR]/project` + "#); + + let pyproject = context.read("project/pyproject.toml"); + insta::with_settings!({ + filters => context.filters(), + }, { + assert_snapshot!( + pyproject, @r#" + [project] + name = "project" + version = "0.1.0" + description = "Add your description here" + readme = "README.md" + requires-python = ">=3.12" + dependencies = [] + + [project.scripts] + project = "project:main" + + [build-system] + requires = ["flit_core>=3.2,<4"] + build-backend = "flit_core.buildapi" + "# + ); + }); +} + /// Run `uv init --app --package --build-backend maturin` to create a packaged application project #[test] #[cfg(feature = "crates-io")] diff --git a/docs/reference/cli.md b/docs/reference/cli.md index 30c9a13f118a..811b1c161af7 100644 --- a/docs/reference/cli.md +++ b/docs/reference/cli.md @@ -489,7 +489,9 @@ uv init [OPTIONS] [PATH]
  • none: Do not infer the author information
  • -
    --build-backend build-backend

    Initialize a build-backend of choice for the project

    +
    --build-backend build-backend

    Initialize a build-backend of choice for the project.

    + +

    Implicitly sets --package.

    Possible values:

    @@ -589,7 +591,7 @@ uv init [OPTIONS] [PATH]

    Defines a [build-system] for the project.

    -

    This is the default behavior when using --lib.

    +

    This is the default behavior when using --lib or --build-backend.

    When using --app, this will include a [project.scripts] entrypoint and use a src/ project structure.