-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Add brew specific message for uv self update
#16838
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
terror
wants to merge
3
commits into
astral-sh:main
Choose a base branch
from
terror:self-update-brew
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+95
−4
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| #![cfg(not(feature = "self-update"))] | ||
|
|
||
| use std::{ | ||
| ffi::OsStr, | ||
| path::{Path, PathBuf}, | ||
| }; | ||
|
|
||
| /// Known sources for uv installations. | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub(crate) enum InstallSource { | ||
| Homebrew, | ||
| } | ||
|
|
||
| impl InstallSource { | ||
| /// Attempt to infer the install source for the given executable path. | ||
| fn from_path(path: &Path) -> Option<Self> { | ||
| let canonical = path.canonicalize().unwrap_or_else(|_| PathBuf::from(path)); | ||
|
|
||
| let components = canonical | ||
| .components() | ||
| .map(|component| component.as_os_str().to_owned()) | ||
| .collect::<Vec<_>>(); | ||
|
|
||
| let cellar = OsStr::new("Cellar"); | ||
| let formula = OsStr::new("uv"); | ||
|
|
||
| if components | ||
| .windows(2) | ||
| .any(|window| window[0] == cellar && window[1] == formula) | ||
| { | ||
| return Some(Self::Homebrew); | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
| /// Detect how uv was installed by inspecting the current executable path. | ||
| pub(crate) fn detect() -> Option<Self> { | ||
| Self::from_path(&std::env::current_exe().ok()?) | ||
woodruffw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| pub(crate) fn description(self) -> &'static str { | ||
| match self { | ||
| Self::Homebrew => "Homebrew", | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn update_instructions(self) -> &'static str { | ||
terror marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| match self { | ||
| Self::Homebrew => "brew update && brew upgrade uv", | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn detects_homebrew_cellar() { | ||
| assert_eq!( | ||
| InstallSource::from_path(Path::new("/opt/homebrew/Cellar/uv/0.9.11/bin/uv")), | ||
| Some(InstallSource::Homebrew) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ignores_non_cellar_paths() { | ||
| assert_eq!( | ||
| InstallSource::from_path(Path::new("/usr/local/bin/uv")), | ||
| None | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @woodruffw on this approach
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some things to note:
OsStris case-sensitive, we may not work for case-insensitive file systems (someone would have to manually rename the path, unlikely, but something worth considering)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach LGTM -- there are definitely some edge cases with custom prefixes, but 99.99% of Homebrew users who use a brew installed uv should have
readlink -f $(which uv)match/<prefix>/Cellar/uv/....There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Another alternative to this approach is to do a PATH lookup on
brew, then match that prefix to the uncanonicalized prefix forcurrent_exe. But I think this approach is cleaner and involves fewer system calls.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note in the error case we're rarely concerned about performance. However, I agree we don't need to get fancy here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree the custom-prefix edge cases are possible (albeit rare); if we see reports there we could potentially add a fallback that compares against
brew --prefix/brew --cellar, this would add more overhead/failure modes which is why we aren't doing it already.