Repository navigation
fix: use sort -V for globals version comparison to prevent redundant installs #1383
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,9 +57,14 @@ if [ -n "$DEPS" ]; then | |
| if [ -f "$pkg_json" ]; then | ||
| installed_ver=$(jq -r '.version // empty' "$pkg_json" 2>/dev/null || true) | ||
| fi | ||
| if [ -n "$installed_ver" ] && [ -n "$wanted_ver" ] && [ "$installed_ver" = "$wanted_ver" ]; then | ||
| echo "$dep@$installed_ver already installed, skipping" | ||
| elif [ -n "$installed_ver" ]; then | ||
| if [ -n "$installed_ver" ] && [ -n "$wanted_ver" ]; then | ||
| min_ver=$(printf '%s\n%s\n' "$wanted_ver" "$installed_ver" | sort -V | head -n1) | ||
| if [ "$min_ver" = "$wanted_ver" ]; then | ||
| echo "$dep@$installed_ver already installed, skipping" | ||
| continue | ||
| fi | ||
| fi | ||
|
Comment on lines
+60
to
+66
|
||
| if [ -n "$installed_ver" ]; then | ||
| echo "$dep@$installed_ver installed, want $wanted_ver, updating" | ||
| MISSING+=("$dep") | ||
| else | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -50,9 +50,11 @@ echo "$DEPS" | while read -r pkg; do | |||||||||||||||||||||||
| req_version=$(echo "$pkg" | sed -n 's/.*>=\([0-9][0-9.]*\).*/\1/p') | ||||||||||||||||||||||||
| installed_version=$(echo "$INSTALLED" | sed -n "s/^${name} v\([0-9][0-9.]*\).*/\1/p") | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if [ -n "$installed_version" ] && [ -n "$req_version" ] && [ "$installed_version" = "$req_version" ]; then | ||||||||||||||||||||||||
| echo "$name $installed_version already installed, skipping" | ||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||
| if [ -n "$installed_version" ] && [ -n "$req_version" ]; then | ||||||||||||||||||||||||
| if printf '%s\n%s\n' "$req_version" "$installed_version" | sort -V | head -n1 | grep -qx "$req_version"; then | ||||||||||||||||||||||||
| echo "$name $installed_version already installed, skipping" | ||||||||||||||||||||||||
| continue | ||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||
|
Comment on lines
+53
to
+57
|
||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||
|
Comment on lines
+53
to
58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of Note: Since
Suggested change
References
|
||||||||||||||||||||||||
| echo "Installing $pkg..." | ||||||||||||||||||||||||
| uv tool install "$pkg" --python "$PYTHON_VERSION" --force 2>/dev/null || echo "Failed to install $pkg, skipping..." | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -109,6 +109,29 @@ When run bash -c "grep 'already installed, skipping' '$SCRIPT'" | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The output should include 'already installed, skipping' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| End | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| It 'uses sort -V for version comparison instead of exact match' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| When run bash -c "grep 'sort -V' '$SCRIPT'" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The output should include 'sort -V' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| End | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| It 'skips when installed version is newer than required' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Simulate: installed=0.15.9, required=0.15.8 -> should skip | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| When run bash -c "printf '0.15.8\n0.15.9\n' | sort -V | head -n1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The output should eq '0.15.8' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| End | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| It 'skips when installed version equals required' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Simulate: installed=0.86.2, required=0.86.2 -> should skip | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| When run bash -c "printf '0.86.2\n0.86.2\n' | sort -V | head -n1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The output should eq '0.86.2' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| End | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| It 'detects when installed version is older than required' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Simulate: installed=2.6.9, required=2.7.0 -> min is 2.6.9, not 2.7.0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| When run bash -c "printf '2.7.0\n2.6.9\n' | sort -V | head -n1" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| The output should eq '2.6.9' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+118
to
+132
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Simulate: installed=0.15.9, required=0.15.8 -> should skip | |
| When run bash -c "printf '0.15.8\n0.15.9\n' | sort -V | head -n1" | |
| The output should eq '0.15.8' | |
| End | |
| It 'skips when installed version equals required' | |
| # Simulate: installed=0.86.2, required=0.86.2 -> should skip | |
| When run bash -c "printf '0.86.2\n0.86.2\n' | sort -V | head -n1" | |
| The output should eq '0.86.2' | |
| End | |
| It 'detects when installed version is older than required' | |
| # Simulate: installed=2.6.9, required=2.7.0 -> min is 2.6.9, not 2.7.0 | |
| When run bash -c "printf '2.7.0\n2.6.9\n' | sort -V | head -n1" | |
| The output should eq '2.6.9' | |
| # Simulate the install script branch: installed=0.15.9, required=0.15.8 -> should skip | |
| When run bash -c ' | |
| INSTALLED=0.15.9 | |
| REQUIRED=0.15.8 | |
| if [ "$INSTALLED" != "$REQUIRED" ] && [ "$(printf "%s\n%s\n" "$REQUIRED" "$INSTALLED" | sort -V | head -n1)" = "$INSTALLED" ]; then | |
| echo "uv tool install" | |
| else | |
| echo "already installed, skipping" | |
| fi | |
| ' | |
| The output should eq 'already installed, skipping' | |
| End | |
| It 'skips when installed version equals required' | |
| # Simulate the install script branch: installed=0.86.2, required=0.86.2 -> should skip | |
| When run bash -c ' | |
| INSTALLED=0.86.2 | |
| REQUIRED=0.86.2 | |
| if [ "$INSTALLED" != "$REQUIRED" ] && [ "$(printf "%s\n%s\n" "$REQUIRED" "$INSTALLED" | sort -V | head -n1)" = "$INSTALLED" ]; then | |
| echo "uv tool install" | |
| else | |
| echo "already installed, skipping" | |
| fi | |
| ' | |
| The output should eq 'already installed, skipping' | |
| End | |
| It 'updates when installed version is older than required' | |
| # Simulate the install script branch: installed=2.6.9, required=2.7.0 -> should update | |
| When run bash -c ' | |
| INSTALLED=2.6.9 | |
| REQUIRED=2.7.0 | |
| if [ "$INSTALLED" != "$REQUIRED" ] && [ "$(printf "%s\n%s\n" "$REQUIRED" "$INSTALLED" | sort -V | head -n1)" = "$INSTALLED" ]; then | |
| echo "uv tool install" | |
| else | |
| echo "already installed, skipping" | |
| fi | |
| ' | |
| The output should eq 'uv tool install' |
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.
The version comparison logic can be simplified by using
sort -VC, which checks if the input is already sorted according to version rules. This avoids the need for a subshell assignment andhead, making the code more concise.Note: Since
sort -Vis a GNU extension, please ensure thatcoreutilsis explicitly included in thePATHwithin thehome.activationblock indefault.nix. This prevents potential failures on systems where the defaultsortdoes not support the-Vflag (e.g., macOS).References