Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions home-manager/modules/npm-globals/install-npm-globals.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

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 and head, making the code more concise.

Note: Since sort -V is a GNU extension, please ensure that coreutils is explicitly included in the PATH within the home.activation block in default.nix. This prevents potential failures on systems where the default sort does not support the -V flag (e.g., macOS).

Suggested change
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
if [ -n "$installed_ver" ] && [ -n "$wanted_ver" ] && \
printf '%s\n%s\n' "$wanted_ver" "$installed_ver" | sort -VC; then
echo "$dep@$installed_ver already installed, skipping"
continue
fi
References
  1. Maintain consistency with established patterns for writing scripts that are extracted from Nix expressions.

Comment on lines +60 to +66

Copilot AI Apr 6, 2026

Copy link

Choose a reason for hiding this comment

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

Same portability concern here: sort -V is not POSIX and will fail on platforms with BSD sort (notably macOS). If cross-platform execution is required, use a guaranteed GNU sort (or a repo-controlled tool) or implement a fallback comparator when sort -V isn’t supported.

Copilot uses AI. Check for mistakes.
if [ -n "$installed_ver" ]; then
echo "$dep@$installed_ver installed, want $wanted_ver, updating"
MISSING+=("$dep")
else
Expand Down
8 changes: 5 additions & 3 deletions home-manager/modules/uv-globals/install-uv-globals.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copilot AI Apr 6, 2026

Copy link

Choose a reason for hiding this comment

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

sort -V is a GNU extension and is not available with BSD sort (e.g., default macOS). If this script is expected to run on Darwin, this will fail at runtime. Consider pinning to a known GNU sort (e.g., via an explicit coreutils path in the Nix/home-manager context) or adding a fallback version-compare implementation when sort -V is unsupported.

Copilot uses AI. Check for mistakes.
fi
Comment on lines +53 to 58

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The use of grep -qx for version comparison is redundant and potentially problematic, as the . characters in version strings are interpreted as regex wildcards. Using sort -VC is a more robust and efficient way to verify that the installed version meets the requirement.

Note: Since sort -V is a GNU extension, please ensure that coreutils is explicitly included in the PATH within the home.activation block in default.nix. This prevents potential failures on systems where the default sort does not support the -V flag (e.g., macOS).

Suggested change
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
fi
if [ -n "$installed_version" ] && [ -n "$req_version" ] && \
printf '%s\n%s\n' "$req_version" "$installed_version" | sort -VC; then
echo "$name $installed_version already installed, skipping"
continue
fi
References
  1. Maintain consistency with established patterns for writing scripts that are extracted from Nix expressions.

echo "Installing $pkg..."
uv tool install "$pkg" --python "$PYTHON_VERSION" --force 2>/dev/null || echo "Failed to install $pkg, skipping..."
Expand Down
23 changes: 23 additions & 0 deletions spec/npm_globals_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,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 wanted'
# Simulate: installed=2.9.4, wanted=2.9.3 -> should skip (min is 2.9.3)
When run bash -c "printf '2.9.3\n2.9.4\n' | sort -V | head -n1"
The output should eq '2.9.3'
End

It 'skips when installed version equals wanted'
# Simulate: installed=2.1.92, wanted=2.1.92 -> should skip
When run bash -c "printf '2.1.92\n2.1.92\n' | sort -V | head -n1"
The output should eq '2.1.92'
End

It 'detects when installed version is older than wanted'
# Simulate: installed=0.65.0, wanted=0.65.2 -> min is 0.65.0, not 0.65.2
When run bash -c "printf '0.65.2\n0.65.0\n' | sort -V | head -n1"
The output should eq '0.65.0'
End

It 'detects when update is needed'
When run bash -c "grep 'updating' '$SCRIPT'"
The output should include 'updating'
Expand Down
23 changes: 23 additions & 0 deletions spec/uv_globals_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copilot AI Apr 6, 2026

Copy link

Choose a reason for hiding this comment

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

These examples validate sort -V behavior in isolation, but they don’t exercise the script’s actual skip/update branching (they would still pass even if the script’s condition is incorrect, as long as sort -V exists). Consider adding tests that run the install script with a mocked/controlled INSTALLED + required version input and assert it prints already installed, skipping for newer/equal installed versions and performs the update path for older versions.

Suggested change
# 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'

Copilot uses AI. Check for mistakes.
End

It 'parses dependency-groups.tools'
When run bash -c "grep 'dependency-groups' '$SCRIPT'"
The output should include 'dependency-groups'
Expand Down
Loading