Skip to content

Commit

Permalink
uv init: Implies --package when using --build-backend
Browse files Browse the repository at this point in the history
  • Loading branch information
j178 committed Oct 26, 2024
1 parent d362e03 commit a5a8584
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
8 changes: 5 additions & 3 deletions crates/uv-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -2526,6 +2526,8 @@ pub struct InitArgs {
pub vcs: Option<VersionControlSystem>,

/// 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<ProjectBuildBackend>,

Expand Down
3 changes: 2 additions & 1 deletion crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(), no_package || r#virtual)
.unwrap_or(kind.packaged_by_default());

Self {
path,
Expand Down
39 changes: 39 additions & 0 deletions crates/uv/tests/it/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
6 changes: 4 additions & 2 deletions docs/reference/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,9 @@ uv init [OPTIONS] [PATH]

<li><code>none</code>: Do not infer the author information</li>
</ul>
</dd><dt><code>--build-backend</code> <i>build-backend</i></dt><dd><p>Initialize a build-backend of choice for the project</p>
</dd><dt><code>--build-backend</code> <i>build-backend</i></dt><dd><p>Initialize a build-backend of choice for the project.</p>

<p>Implicitly sets <code>--package</code>.</p>

<p>Possible values:</p>

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

<p>Defines a <code>[build-system]</code> for the project.</p>

<p>This is the default behavior when using <code>--lib</code>.</p>
<p>This is the default behavior when using <code>--lib</code> or <code>--build-backend</code>.</p>

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

Expand Down

0 comments on commit a5a8584

Please sign in to comment.