Merge PR #182: warn if multiple bd are in PATH

warn if multiple bd are in PATH
This commit is contained in:
Steve Yegge
2025-10-30 19:39:50 -07:00
committed by GitHub
2 changed files with 163 additions and 10 deletions

View File

@@ -70,14 +70,22 @@ function Install-WithGo {
Write-WarningMsg "go install failed: $_"
return $false
}
$gopath = (& go env GOPATH)
if (-not $gopath) {
return $true
# Prefer GOBIN if set, otherwise GOPATH/bin
$gobin = (& go env GOBIN) 2>$null
if ($gobin -and $gobin.Trim() -ne "") {
$binDir = $gobin.Trim()
} else {
$gopath = (& go env GOPATH)
if (-not $gopath) {
return $true
}
$binDir = Join-Path $gopath "bin"
}
$binDir = Join-Path $gopath "bin"
$bdPath = Join-Path $binDir "bd.exe"
# Record where we expect the binary to have been installed in this run
$Script:LastInstallPath = $bdPath
if (-not (Test-Path $bdPath)) {
Write-WarningMsg "bd.exe not found in $binDir after install"
}
@@ -142,6 +150,9 @@ function Install-FromSource {
Copy-Item -Path (Join-Path $repoPath "bd.exe") -Destination (Join-Path $installDir "bd.exe") -Force
Write-Success "bd installed to $installDir\bd.exe"
# Record where we installed the binary when building from source
$Script:LastInstallPath = Join-Path $installDir "bd.exe"
$pathEntries = [Environment]::GetEnvironmentVariable("PATH", "Process").Split([IO.Path]::PathSeparator) | ForEach-Object { $_.Trim() }
if (-not ($pathEntries -contains $installDir)) {
Write-WarningMsg "$installDir is not in your PATH. Add it with:`n setx PATH `"$Env:PATH;$installDir`""
@@ -153,8 +164,65 @@ function Install-FromSource {
return $true
}
function Get-BdPathsInPath {
$pathEntries = [Environment]::GetEnvironmentVariable("PATH", "Process").Split([IO.Path]::PathSeparator) | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" }
$found = @()
foreach ($entry in $pathEntries) {
try {
$candidate = Join-Path $entry "bd.exe"
} catch {
continue
}
if (Test-Path $candidate) {
try {
$resolved = (Resolve-Path $candidate -ErrorAction SilentlyContinue).ProviderPath
} catch {
$resolved = $candidate
}
if (-not ($found -contains $resolved)) { $found += $resolved }
}
}
return $found
}
function Warn-IfMultipleBd {
$paths = Get-BdPathsInPath
if ($paths.Count -le 1) { return }
Write-WarningMsg "Multiple 'bd' executables found on your PATH. This can cause an older version to be executed instead of the one we installed."
Write-Host "Found the following 'bd' executables (entries earlier in PATH take precedence):" -ForegroundColor Yellow
$i = 0
foreach ($p in $paths) {
$i++
$ver = $null
try {
$ver = & "$p" version 2>$null
if ($LASTEXITCODE -ne 0) { $ver = $null }
} catch { $ver = $null }
if (-not $ver) { $ver = "<unknown version>" }
Write-Host (" {0}. {1} -> {2}" -f $i, $p, $ver)
}
if ($Script:LastInstallPath) {
Write-Host "`nWe installed to: $($Script:LastInstallPath)" -ForegroundColor Cyan
$first = $paths[0]
if ($first -ne $Script:LastInstallPath) {
Write-WarningMsg "The 'bd' executable that appears first in your PATH is different from the one we installed. To make the newly installed 'bd' the one you get when running 'bd', either:"
Write-Host " - Remove the older $first from your PATH, or" -ForegroundColor Yellow
Write-Host " - Reorder your PATH so that $([System.IO.Path]::GetDirectoryName($Script:LastInstallPath)) appears before $([System.IO.Path]::GetDirectoryName($first))" -ForegroundColor Yellow
Write-Host "After updating PATH, restart your shell and run 'bd version' to confirm." -ForegroundColor Yellow
} else {
Write-Host "The installed 'bd' is first in your PATH." -ForegroundColor Green
}
} else {
Write-WarningMsg "We couldn't determine where we installed 'bd' during this run."
}
}
function Verify-Install {
Write-Info "Verifying installation..."
# If there are multiple bd binaries on PATH, warn the user before running the verification
try { Warn-IfMultipleBd } catch { }
try {
$versionOutput = & bd version 2>$null
if ($LASTEXITCODE -ne 0) {

View File

@@ -196,13 +196,23 @@ install_with_go() {
if go install github.com/steveyegge/beads/cmd/bd@latest; then
log_success "bd installed successfully via go install"
# Check if GOPATH/bin is in PATH
local gopath_bin="$(go env GOPATH)/bin"
if [[ ":$PATH:" != *":$gopath_bin:"* ]]; then
log_warning "GOPATH/bin is not in your PATH"
# Record where we expect the binary to have been installed
# Prefer GOBIN if set, otherwise GOPATH/bin
local gobin
gobin=$(go env GOBIN 2>/dev/null || true)
if [ -n "$gobin" ]; then
bin_dir="$gobin"
else
bin_dir="$(go env GOPATH)/bin"
fi
LAST_INSTALL_PATH="$bin_dir/bd"
# Check if GOPATH/bin (or GOBIN) is in PATH
if [[ ":$PATH:" != *":$bin_dir:"* ]]; then
log_warning "$bin_dir is not in your PATH"
echo ""
echo "Add this to your shell profile (~/.bashrc, ~/.zshrc, etc.):"
echo " export PATH=\"\$PATH:$gopath_bin\""
echo " export PATH=\"\$PATH:$bin_dir\""
echo ""
fi
@@ -246,6 +256,9 @@ build_from_source() {
log_success "bd installed to $install_dir/bd"
# Record where we installed the binary when building from source
LAST_INSTALL_PATH="$install_dir/bd"
# Check if install_dir is in PATH
if [[ ":$PATH:" != *":$install_dir:"* ]]; then
log_warning "$install_dir is not in your PATH"
@@ -276,6 +289,9 @@ build_from_source() {
# Verify installation
verify_installation() {
# If multiple 'bd' binaries exist on PATH, warn the user before verification
warn_if_multiple_bd || true
if command -v bd &> /dev/null; then
log_success "bd is installed and ready!"
echo ""
@@ -293,6 +309,75 @@ verify_installation() {
fi
}
# Returns a list of full paths to 'bd' found in PATH (earlier entries first)
get_bd_paths_in_path() {
local IFS=':'
local -a entries
read -ra entries <<< "$PATH"
local -a found
local p
for p in "${entries[@]}"; do
[ -z "$p" ] && continue
if [ -x "$p/bd" ]; then
# Resolve symlink if possible
if command -v readlink >/dev/null 2>&1; then
resolved=$(readlink -f "$p/bd" 2>/dev/null || printf '%s' "$p/bd")
else
resolved="$p/bd"
fi
# avoid duplicates
skip=0
for existing in "${found[@]:-}"; do
if [ "$existing" = "$resolved" ]; then skip=1; break; fi
done
if [ $skip -eq 0 ]; then
found+=("$resolved")
fi
fi
done
# print results, one per line
for item in "${found[@]:-}"; do
printf '%s\n' "$item"
done
}
warn_if_multiple_bd() {
mapfile -t bd_paths < <(get_bd_paths_in_path)
if [ "${#bd_paths[@]}" -le 1 ]; then
return 0
fi
log_warning "Multiple 'bd' executables found on your PATH. An older copy may be executed instead of the one we installed."
echo "Found the following 'bd' executables (entries earlier in PATH take precedence):"
local i=1
for p in "${bd_paths[@]}"; do
local ver
if [ -x "$p" ]; then
ver=$("$p" version 2>/dev/null || true)
fi
if [ -z "$ver" ]; then ver="<unknown version>"; fi
echo " $i. $p -> $ver"
i=$((i+1))
done
if [ -n "$LAST_INSTALL_PATH" ]; then
echo ""
echo "We installed to: $LAST_INSTALL_PATH"
# Compare first PATH entry vs installed path
first="${bd_paths[0]}"
if [ "$first" != "$LAST_INSTALL_PATH" ]; then
log_warning "The 'bd' executable that appears first in your PATH is different from the one we installed. To make the newly installed 'bd' the one you get when running 'bd', either:"
echo " - Remove or rename the older $first from your PATH, or"
echo " - Reorder your PATH so that $(dirname "$LAST_INSTALL_PATH") appears before $(dirname "$first")"
echo "After updating PATH, restart your shell and run 'bd version' to confirm."
else
echo "The installed 'bd' is first in your PATH.";
fi
else
log_warning "We couldn't determine where we installed 'bd' during this run.";
fi
}
# Main installation flow
main() {
echo ""