Add UV_INIT_BARE environment variable for uv init (#18210)

Resolves https://github.com/astral-sh/uv/issues/18202

This diff adds a `UV_INIT_BARE` environment variable that makes `--bare`
the default behavior for `uv init`. The command-line flag takes
precedence over the environment variable, following the same pattern
used by `UV_FROZEN`, `UV_LOCKED`, and other boolean environment
variables.
This commit is contained in:
liam
2026-02-27 17:37:28 -05:00
committed by GitHub
parent c0e7c21980
commit ef1045668c
4 changed files with 55 additions and 0 deletions
+2
View File
@@ -657,6 +657,7 @@ pub struct EnvironmentOptions {
pub no_env_file: EnvFlag,
pub venv_seed: EnvFlag,
pub venv_clear: EnvFlag,
pub init_bare: EnvFlag,
}
impl EnvironmentOptions {
@@ -749,6 +750,7 @@ impl EnvironmentOptions {
no_env_file: EnvFlag::new(EnvVars::UV_NO_ENV_FILE)?,
venv_seed: EnvFlag::new(EnvVars::UV_VENV_SEED)?,
venv_clear: EnvFlag::new(EnvVars::UV_VENV_CLEAR)?,
init_bare: EnvFlag::new(EnvVars::UV_INIT_BARE)?,
})
}
}
+5
View File
@@ -380,6 +380,11 @@ impl EnvVars {
#[attr_added_in("0.3.0")]
pub const UV_TOOL_BIN_DIR: &'static str = "UV_TOOL_BIN_DIR";
/// Equivalent to the `--bare` argument for `uv init`. If set, uv will only create a
/// `pyproject.toml`.
#[attr_added_in("0.10.7")]
pub const UV_INIT_BARE: &'static str = "UV_INIT_BARE";
/// Equivalent to the `--build-backend` argument for `uv init`. Determines the default backend
/// to use when creating a new project.
#[attr_added_in("0.8.2")]
+2
View File
@@ -431,6 +431,8 @@ impl InitSettings {
)
.unwrap_or(kind.packaged_by_default());
let bare = resolve_flag(bare, "bare", environment.init_bare).is_enabled();
let filesystem_install_mirrors = filesystem
.map(|fs| fs.install_mirrors.clone())
.unwrap_or_default();
+46
View File
@@ -647,6 +647,52 @@ fn init_bare_opt_in() {
});
}
#[test]
fn init_bare_env_var() {
let context = uv_test::test_context!("3.12");
uv_snapshot!(context.filters(), context.init().arg("foo").env(EnvVars::UV_INIT_BARE, "true"), @"
success: true
exit_code: 0
----- stdout -----
----- stderr -----
Initialized project `foo` at `[TEMP_DIR]/foo`
");
context
.temp_dir
.child("foo/README.md")
.assert(predicate::path::missing());
context
.temp_dir
.child("foo/hello.py")
.assert(predicate::path::missing());
context
.temp_dir
.child("foo/.python-version")
.assert(predicate::path::missing());
context
.temp_dir
.child("foo/.git")
.assert(predicate::path::missing());
let pyproject = context.read("foo/pyproject.toml");
insta::with_settings!({
filters => context.filters(),
}, {
assert_snapshot!(
pyproject, @r#"
[project]
name = "foo"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = []
"#
);
});
}
// General init --script correctness test
#[test]
fn init_script() -> Result<()> {