Skip to content

Add preview native uv self installation - #1336

Draft
zaniebot wants to merge 9 commits into
mainfrom
zb/self-install-20260910
Draft

zaniebot wants to merge 9 commits into
mainfrom
zb/self-install-20260910

Conversation

@zaniebot

@zaniebot zaniebot commented Sep 10, 2026 •

Copy link
Copy Markdown
Collaborator

Standalone installation currently delegates executable placement and installation metadata to generated scripts. Add uv self install behind the self-management preview feature so a downloaded distribution can install its own uv, uvx, and Windows uvw executables. Managed installations record their location and PATH preference beside the executables; unmanaged installations omit the receipt and shell changes. An adjacent receipt can identify an installation even when its uv executable needs to be restored. The existing release and self-update paths remain unchanged.

@astral-automations-bot astral-automations-bot Bot added enhancement New feature or request preview Experimental behavior labels Sep 10, 2026
@zaniebot
zaniebot force-pushed the zb/self-install-20260910 branch from 199f1f5 to 2ed178b Compare September 10, 2026 17:27
@zaniebot zaniebot added priority:3 Normal: useful features, documentation, usability, or routine efficiency improvements. size:3 Substantial: interacting behaviors, difficult compatibility questions, or a careful migration. labels Sep 10, 2026
@zanieb
zanieb added this pull request to stack #1349 September 10, 2026 18:11
Comment on lines +47 to +48
source: String,
version: String,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these not typed?

@zaniebot zaniebot added the risk:3 Credible risk of meaningful incorrect behavior or regression; bounded or recoverable. label Sep 11, 2026
@zaniebot
zaniebot force-pushed the zb/self-install-20260910 branch from 2ed178b to e8fe93f Compare September 11, 2026 04:07
@codspeed

codspeed Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ 8 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 54 untouched benchmarks
⏩ 35 skipped benchmarks1


Comparing zb/self-install-20260910 (c1e30b2) with main (4efeeaf)

Open in CodSpeed

Footnotes

  1. 35 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@zaniebot zaniebot added the build:skip-release Disable building release binaries for a pull request label Oct 7, 2026
@astral-automations-bot
astral-automations-bot Bot force-pushed the zb/self-install-20260910 branch from 5f8060c to 935232e Compare October 7, 2026 22:07
Comment thread crates/uv-cli/src/lib.rs Outdated
Comment on lines +682 to +683
#[arg(long, env = EnvVars::UV_NO_MODIFY_PATH)]
pub no_modify_path: bool,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Accept the documented numeric PATH environment flag

UV_NO_MODIFY_PATH=1, the documented installer setting, is rejected because this boolean uses clap's default parser, which accepts only true and false. Installation therefore fails during argument parsing. Add BoolishValueParser::new(), following the existing environment-backed flags, and cover the numeric environment value in a focused test.

Comment thread crates/uv/src/commands/self_install.rs Outdated
Comment on lines +171 to +175
let _lock = LockedFile::acquire(
destination.join(".uv-install.lock"),
LockedFileMode::Exclusive,
"uv installation",
)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Hold the installation lock through receipt updates

The lock is released when install_binaries returns, before the receipt is written; receipt validation also happens before acquisition. Concurrent installers can therefore leave mismatched state: A installs its binaries and releases the lock, B installs its binaries and receipt, then A overwrites B's receipt. Move lock ownership into self_install and hold it from receipt validation through the final receipt write.

Comment thread crates/uv/src/commands/self_install.rs Outdated
Comment on lines +60 to +63
} else if let Some(path) = std::env::var_os(EnvVars::XDG_CONFIG_HOME)
&& Path::new(&path).is_absolute()
{
PathBuf::from(path).join("uv")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Share the existing legacy receipt discovery

This duplicates self_update::receipt_prefixes with different fallback behavior. When an absolute XDG_CONFIG_HOME is set but the receipt remains under ~/.config/uv, self-update can find it while this implementation cannot. The caller then discards the error and can replace a custom release source with the official default. Extract and reuse the existing legacy lookup so both commands recognize the same installations.

Comment thread crates/uv/src/commands/self_install.rs Outdated
self_replace::self_replace(&source)?;
continue;
}
uv_fs::copy_atomic_sync(&source, &target)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Rename the staged executables instead of copying again

Each executable has already been copied into a temporary directory under the destination. copy_atomic_sync creates another temporary file and copies the entire executable again before renaming it, adding redundant I/O and temporary storage. The staged files are already on the destination filesystem, so use uv_fs::rename_with_retry to commit them directly while retaining the Windows self-replacement branch.

Comment thread crates/uv/Cargo.toml Outdated
uv-dispatch = { workspace = true }
uv-distribution = { workspace = true }
uv-dirs = { workspace = true }
uv-distribution-filename = { workspace = true }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Remove the unused distribution-filename dependency

No code in the uv crate uses uv-distribution-filename. This adds an unnecessary direct dependency and lockfile entry contrary to the repository's unused-dependency checks. Remove it from this manifest and the corresponding uv dependency list in Cargo.lock.

@zaniebot
zaniebot deployed to automations October 9, 2026 03:08 — with GitHub Actions Active
@zaniebot
zaniebot deployed to automations October 9, 2026 03:12 — with GitHub Actions Active
Comment thread crates/uv/src/commands/self_install.rs Outdated
Comment on lines +229 to +230
if modify_path {
update_shell::update_shell(&destination, printer).await?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Treat unchanged shell configuration as a successful install

On Unix, repeating installation into a directory outside the current PATH before restarting the shell replaces the binaries successfully, then exits with an error. The shared helper deliberately errors when configuration files already contain the PATH entry, as covered by update_shell_tool_and_python. That diagnostic suits an explicit update-shell command but makes installation non-idempotent. Expose an unchanged outcome from the helper and accept it here while retaining genuine shell-update errors.

Comment on lines +126 to +130
let recorded = receipt
.install_prefix
.join(format!("uv{}", std::env::consts::EXE_SUFFIX));
anyhow::ensure!(
uv_fs::is_same_file_allow_missing(&recorded, &executable) == Some(true),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Normalize legacy receipt prefixes in both lookup paths

For a cargo-dist receipt recording <root> while the executable lives at <root>/bin/uv, for_installation accepts the legacy layout, but this method checks only <root>/uv. Installing that distribution into a fresh directory therefore rejects its valid source receipt; the caller's .ok() silently replaces custom release metadata with ReleaseSource::default(). Share the legacy-prefix normalization between both lookup paths before checking executable identity.

Comment thread crates/uv/tests/it/self_install.rs Outdated
Comment on lines +41 to +46
let output = command.output()?;
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Use command snapshots in the main installation test

This test hand-rolls output capture and stderr formatting, then switches to .status() for the installed executable and repeat installation. That duplicates assertion infrastructure and leaves those command outputs unspecified. Use explicit uv_snapshot! calls with context filters, following tests/python/python_install.rs::python_install and this module's legacy-receipt test; retain the focused filesystem and receipt assertions.

@zaniebot
zaniebot deployed to automations October 9, 2026 03:52 — with GitHub Actions Active
@zaniebot
zaniebot deployed to automations October 9, 2026 03:56 — with GitHub Actions Active
Comment thread crates/uv/src/commands/self_install.rs Outdated
Comment on lines +172 to +174
uv_fs::with_retry_sync(&source, &target, "renaming", || {
fs_err::rename(&source, &target)
})?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Handle the running Windows launcher during replacement

Running uvw self install into its own installation directory reaches this rename while uvw.exe is still running: uv_windows::spawn_child keeps the launcher alive until its child exits. The self-replacement branch handles only uv.exe, so replacing uvw.exe fails after retries, after the other binaries have been replaced and before the receipt is written. Retrying cannot resolve this parent-child dependency. Handle in-use Windows targets by renaming the old executable aside and deferring cleanup, following the pattern in execute_official_installer, before committing the staged replacement.

Comment thread crates/uv/src/commands/self_install.rs Outdated
Comment on lines +247 to +249
let source = source.join(name);
anyhow::ensure!(
fs_err::symlink_metadata(&source)?.is_file(),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Verify that the staged uv is the running executable

If [email protected] runs beside an older uv and uvx, this copies the older unsuffixed binaries while the receipt and success message report 0.13.0. The uvx and uvw launchers explicitly support versioned executable names. for_executable does not prevent this mismatch because its errors are discarded by the caller. Pass the running executable into staging and verify that the selected source uv identifies it before replacing anything; unsupported layouts should fail clearly instead of successfully installing a different version.

@zaniebot
zaniebot deployed to automations October 9, 2026 20:59 — with GitHub Actions Active
@zaniebot
zaniebot deployed to automations October 9, 2026 21:05 — with GitHub Actions Active
Comment on lines +299 to +301
let executable = std::env::current_exe()?;
fs_err::create_dir_all(&destination)?;
let lock = LockedFile::acquire(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Retain executable identity across the installation lock wait

If a process launched from the destination waits behind an installer of another uv version, this saved executable path can be replaced during lock acquisition. install_binaries then compares source/uv with the same saved pathname, so the identity check succeeds even though it now selects the other installer's binary. The waiting process copies that binary but records its own CARGO_PKG_VERSION in the receipt and success message. Retain the executable's file identity across the wait and reject a changed source before replacing files.

Comment thread crates/uv/tests/it/self_install.rs Outdated
#[cfg(any(not(windows), feature = "windows-gui-bin"))]
fn installs_running_distribution_with_numeric_no_modify_path() -> Result<()> {
let context = uv_test::test_context_with_versions!(&[])
.with_filter((r"uv [0-9]+\.[0-9]+\.[0-9]+[^ ]*", "uv [VERSION]"))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Filter only the expected installed version

This filter normalizes every uv version, so the installed executable's --version snapshot passes even if installation selects a different version. The receipt assertion does not catch that mismatch because receipt creation independently uses CARGO_PKG_VERSION. Filter the escaped expected version, following the existing self-update tests, and apply the same correction to installs_linked_running_distribution.

Comment thread crates/uv/src/commands/self_install.rs Outdated
Comment on lines +278 to +281
#[cfg(not(windows))]
uv_fs::with_retry_sync(&source, &target, "renaming", || {
fs_err::rename(&source, &target)
})?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P3] Call the synchronous rename directly on non-Windows

with_retry_sync only implements retries on Windows; under this cfg(not(windows)) branch it simply invokes the closure once. The wrapper therefore suggests retry behavior that cannot occur here and requires readers to inspect another layer. Call fs_err::rename directly: it already satisfies the synchronous-operation requirement, and the corresponding Windows-only hawk override can remain unchanged.

@zaniebot
zaniebot deployed to automations October 9, 2026 21:41 — with GitHub Actions Active
@zaniebot
zaniebot deployed to automations October 9, 2026 21:46 — with GitHub Actions Active
Comment thread hawk.toml
Comment on lines -146 to -151
lint = "hawk::unnecessary_public"
crate = "uv_fs"
item = "with_retry_sync"
kind = "function"
level = "expect"
target = "cfg(not(windows))"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Restore the non-Windows visibility override

The installer now calls fs_err::rename directly on non-Windows, so with_retry_sync still has no production callers outside uv_fs on Linux; its external callers in uv-install-wheel are under #[cfg(not(unix))]. Removing this expectation exposes hawk::unnecessary_public in the Linux CI job, which runs Hawk with -D warnings. Restore this platform-specific override, since the Windows callers still require public visibility.

@zaniebot
zaniebot deployed to automations October 9, 2026 22:06 — with GitHub Actions Active
@zaniebot
zaniebot deployed to automations October 9, 2026 22:11 — with GitHub Actions Active
Comment on lines +267 to +268
fs_err::copy(&source, staged.path().join(name))?;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Recheck source identity after staging the distribution

When /a/uv self install --install-dir /b races with another native installation into /a, the processes hold different destination locks. The other installer can replace /a/uv after the identity check but before this copy. fs_err::copy reopens the pathname, so the retained handle does not prevent copying the replacement binary while recording the running process's CARGO_PKG_VERSION in the receipt. Recheck the source pathname against the retained identity after staging, and reject a changed source before committing any destination files.

@zaniebot
zaniebot deployed to automations October 9, 2026 22:36 — with GitHub Actions Active
@zaniebot
zaniebot deployed to automations October 9, 2026 22:41 — with GitHub Actions Active
Comment thread crates/uv/src/commands/self_install.rs Outdated
Comment on lines +233 to +234
fs_err::rename(&previous, target)
.context("Failed to restore the previous executable")?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Preserve the backup when executable restoration fails

If publishing the staged executable fails and the rollback rename also fails, this ? drops backup before backup.keep() is reached. TempDir cleanup can then delete the previous executable, removing the recoverable copy precisely when restoration failed. Retain the backup on the rollback-error path and include its location in the error; allow automatic cleanup only after successful restoration.

@zaniebot
zaniebot deployed to automations October 9, 2026 23:04 — with GitHub Actions Active

This branch was successfully deployed

1 active deployment
automations — c1e30b2a Deployed Oct 9, 2026 by zaniebot via review-code-quality / code quality review #9425
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build:skip-release Disable building release binaries for a pull request enhancement New feature or request preview Experimental behavior priority:3 Normal: useful features, documentation, usability, or routine efficiency improvements. risk:3 Credible risk of meaningful incorrect behavior or regression; bounded or recoverable. size:3 Substantial: interacting behaviors, difficult compatibility questions, or a careful migration.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants