Skip to content

Commit 6e79013

Browse files
zaniebotzanieb
andcommitted
Package explicitly requested applications by default (#20773)
With package-by-default enabled, `uv init --app` still implicitly selected the unpackaged application layout, so explicitly requesting an application produced a different project than `uv init`. Apply the packaged default to explicitly requested applications as well, while retaining `--no-package` for applications that should remain unpackaged. --------- Co-authored-by: Zanie Blue <[email protected]>
1 parent e4602de commit 6e79013

4 files changed

Lines changed: 45 additions & 28 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3275,6 +3275,8 @@ pub struct InitArgs {
32753275
/// Create a project for an application.
32763276
///
32773277
/// This project kind is for web servers, scripts, and command-line interfaces.
3278+
///
3279+
/// Applications are packaged by default. Use `--no-package` to create an unpackaged application.
32783280
#[arg(long, alias = "application", conflicts_with_all = ["lib", "script"])]
32793281
pub r#app: bool,
32803282

‎crates/uv/src/settings.rs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,10 @@ impl InitSettings {
534534
(InitProjectKind::ApplicationWithLibrary, Some(false)) => {
535535
InitProjectKind::Application
536536
}
537-
// The user specifically asked for `--app`, so no library.
538-
(InitProjectKind::Application, None | Some(false)) => InitProjectKind::Application,
539-
(InitProjectKind::Application, Some(true)) => {
537+
(InitProjectKind::Application, None | Some(true)) => {
540538
InitProjectKind::ApplicationWithLibrary
541539
}
540+
(InitProjectKind::Application, Some(false)) => InitProjectKind::Application,
542541
(InitProjectKind::Library, None | Some(true)) => InitProjectKind::Library,
543542
(InitProjectKind::Library, Some(false)) => {
544543
bail!("`lib` and `no_package` are mutually exclusive");

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

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ fn init_bare() {
109109
});
110110
}
111111

112-
/// Run `uv init --app` to create an application project
112+
/// Run `uv init --app` to create a packaged application project
113113
#[test]
114114
fn init_application() -> Result<()> {
115115
let context = uv_test::test_context!("3.12");
@@ -118,7 +118,7 @@ fn init_application() -> Result<()> {
118118
child.create_dir_all()?;
119119

120120
let pyproject_toml = child.join("pyproject.toml");
121-
let main_py = child.join("main.py");
121+
let init_py = child.join("src").join("foo").join("__init__.py");
122122

123123
uv_snapshot!(context.filters(), context.init().current_dir(&child).arg("--app"), @"
124124
exit_code: 0 (success)
@@ -139,27 +139,30 @@ fn init_application() -> Result<()> {
139139
readme = "README.md"
140140
requires-python = ">=3.12"
141141
dependencies = []
142+
143+
[project.scripts]
144+
foo = "foo:main"
145+
146+
[build-system]
147+
requires = ["uv_build>=[CURRENT_VERSION],<[NEXT_BREAKING]"]
148+
build-backend = "uv_build"
142149
"#
143150
);
144151
});
145152

146-
let hello = fs_err::read_to_string(main_py)?;
153+
let init = fs_err::read_to_string(init_py)?;
147154
insta::with_settings!({
148155
filters => context.filters(),
149156
}, {
150157
assert_snapshot!(
151-
hello, @r#"
152-
def main():
158+
init, @r#"
159+
def main() -> None:
153160
print("Hello from foo!")
154-
155-
156-
if __name__ == "__main__":
157-
main()
158161
"#
159162
);
160163
});
161164

162-
uv_snapshot!(context.filters(), context.run().current_dir(&child).arg("main.py"), @"
165+
uv_snapshot!(context.filters(), context.run().current_dir(&child).arg("foo"), @"
163166
exit_code: 0 (success)
164167
----- stdout -----
165168
Hello from foo!
@@ -169,15 +172,17 @@ fn init_application() -> Result<()> {
169172
Using CPython 3.12.[X] interpreter at: [PYTHON-3.12]
170173
Creating virtual environment at: .venv
171174
Resolved 1 package in [TIME]
172-
Checked in [TIME]
175+
Prepared 1 package in [TIME]
176+
Installed 1 package in [TIME]
177+
+ foo==0.1.0 (from file://[TEMP_DIR]/foo)
173178
");
174179

175180
Ok(())
176181
}
177182

178-
/// When `main.py` already exists, we don't create it again
183+
/// When `main.py` already exists in an unpackaged application, we don't create it again
179184
#[test]
180-
fn init_application_hello_exists() -> Result<()> {
185+
fn init_application_no_package_main_exists() -> Result<()> {
181186
let context = uv_test::test_context!("3.12");
182187

183188
let child = context.temp_dir.child("foo");
@@ -187,7 +192,7 @@ fn init_application_hello_exists() -> Result<()> {
187192
let main_py = child.child("main.py");
188193
main_py.touch()?;
189194

190-
uv_snapshot!(context.filters(), context.init().current_dir(&child).arg("--app"), @"
195+
uv_snapshot!(context.filters(), context.init().current_dir(&child).arg("--app").arg("--no-package"), @"
191196
exit_code: 0 (success)
192197
----- stderr -----
193198
Initialized project `foo`
@@ -222,9 +227,9 @@ fn init_application_hello_exists() -> Result<()> {
222227
Ok(())
223228
}
224229

225-
/// When other Python files already exists, we still create `main.py`
230+
/// When other Python files already exist in an unpackaged application, we still create `main.py`
226231
#[test]
227-
fn init_application_other_python_exists() -> Result<()> {
232+
fn init_application_no_package_other_python_exists() -> Result<()> {
228233
let context = uv_test::test_context!("3.12");
229234

230235
let child = context.temp_dir.child("foo");
@@ -235,7 +240,7 @@ fn init_application_other_python_exists() -> Result<()> {
235240
let other_py = child.child("foo.py");
236241
other_py.touch()?;
237242

238-
uv_snapshot!(context.filters(), context.init().current_dir(&child).arg("--app"), @"
243+
uv_snapshot!(context.filters(), context.init().current_dir(&child).arg("--app").arg("--no-package"), @"
239244
exit_code: 0 (success)
240245
----- stderr -----
241246
Initialized project `foo`
@@ -1232,7 +1237,7 @@ fn init_application_current_dir() -> Result<()> {
12321237
");
12331238

12341239
let pyproject = fs_err::read_to_string(dir.join("pyproject.toml"))?;
1235-
let main_py = fs_err::read_to_string(dir.join("main.py"))?;
1240+
let init_py = fs_err::read_to_string(dir.join("src/foo/__init__.py"))?;
12361241

12371242
insta::with_settings!({
12381243
filters => context.filters(),
@@ -1246,6 +1251,13 @@ fn init_application_current_dir() -> Result<()> {
12461251
readme = "README.md"
12471252
requires-python = ">=3.12"
12481253
dependencies = []
1254+
1255+
[project.scripts]
1256+
foo = "foo:main"
1257+
1258+
[build-system]
1259+
requires = ["uv_build>=[CURRENT_VERSION],<[NEXT_BREAKING]"]
1260+
build-backend = "uv_build"
12491261
"#
12501262
);
12511263
});
@@ -1254,13 +1266,9 @@ fn init_application_current_dir() -> Result<()> {
12541266
filters => context.filters(),
12551267
}, {
12561268
assert_snapshot!(
1257-
main_py, @r#"
1258-
def main():
1269+
init_py, @r#"
1270+
def main() -> None:
12591271
print("Hello from foo!")
1260-
1261-
1262-
if __name__ == "__main__":
1263-
main()
12641272
"#
12651273
);
12661274
});
@@ -1734,6 +1742,7 @@ fn init_normalized_names() -> Result<()> {
17341742

17351743
let child = context.temp_dir.child("bar_baz");
17361744
let pyproject = fs_err::read_to_string(child.join("pyproject.toml"))?;
1745+
let _ = fs_err::read_to_string(child.join("src/bar_baz/__init__.py"))?;
17371746

17381747
insta::with_settings!({
17391748
filters => context.filters(),
@@ -1747,6 +1756,13 @@ fn init_normalized_names() -> Result<()> {
17471756
readme = "README.md"
17481757
requires-python = ">=3.12"
17491758
dependencies = []
1759+
1760+
[project.scripts]
1761+
bar-baz = "bar_baz:main"
1762+
1763+
[build-system]
1764+
requires = ["uv_build>=[CURRENT_VERSION],<[NEXT_BREAKING]"]
1765+
build-backend = "uv_build"
17501766
"#
17511767
);
17521768
});

‎docs/concepts/projects/init.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ with an error.
2929

3030
Application projects are suitable for web servers, scripts, and command-line interfaces.
3131

32-
Applications are the default target for `uv init`:
32+
Applications are the default target for `uv init`, but can also be specified with the `--app` flag:
3333

3434
```console
3535
$ uv init example-app

0 commit comments

Comments
 (0)