Skip to content

Commit 420d9d2

Browse files
committed
fix(search): Store all bucket variants for duplicate app names
Previously [] stored a single path/bucket entry, overwriting duplicates when the same app existed in multiple buckets. If that bucket's file was later removed, the cache lookup failed silently. Now stores an array of entries per app name, and search_by_index iterates all variants. Also adds Write-Debug diagnostics to previously empty catch blocks.
1 parent c089c3a commit 420d9d2

1 file changed

Lines changed: 30 additions & 24 deletions

File tree

libexec/scoop-search.ps1

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ function init_search_cache {
3939
if ($cache.maxWriteUtc -ne $currentMaxWrite.ToString('o')) { return $false }
4040
$script:searchIndexApps = @{}
4141
foreach ($prop in $cache.apps.PSObject.Properties) {
42-
$script:searchIndexApps[$prop.Name] = @{ path = $prop.Value.path; bucket = $prop.Value.bucket }
42+
$entries = @()
43+
foreach ($entry in $prop.Value) {
44+
$entries += @{ path = $entry.path; bucket = $entry.bucket }
45+
}
46+
$script:searchIndexApps[$prop.Name] = $entries
4347
}
4448
$script:searchIndexBins = @{}
4549
foreach ($prop in $cache.bins.PSObject.Properties) {
@@ -68,7 +72,8 @@ function build_search_cache {
6872
foreach ($bucket in $allPathsByBucket.Keys) {
6973
foreach ($filePath in $allPathsByBucket[$bucket]) {
7074
$appName = [System.IO.Path]::GetFileNameWithoutExtension($filePath)
71-
$newApps[$appName] = @{ path = $filePath; bucket = $bucket }
75+
if (-not $newApps.ContainsKey($appName)) { $newApps[$appName] = @() }
76+
$newApps[$appName] += @{ path = $filePath; bucket = $bucket }
7277
try {
7378
$manifest = Get-Content -Path $filePath -Raw | ConvertFrom-Json -ErrorAction Stop
7479
if (-not $manifest.bin) { continue }
@@ -91,7 +96,7 @@ function build_search_cache {
9196
}
9297
}
9398
}
94-
} catch { }
99+
} catch { Write-Debug "cache-build parse failed for $($filePath): $($_.Exception.Message)" }
95100
}
96101
}
97102
$cacheData = [PSCustomObject]@{ timestamp = (Get-Date).ToString('o'); fileCount = $totalCount; maxWriteUtc = $maxWriteUtc.ToString('o'); apps = [PSCustomObject]$newApps; bins = [PSCustomObject]$newBins }
@@ -120,29 +125,30 @@ function search_by_index($query) {
120125
}
121126
# Parse manifests only for matched apps
122127
foreach ($appName in $matched.Keys) {
123-
$info = $searchIndexApps[$appName]
124-
try {
125-
$manifest = Get-Content -Path $info.path -Raw | ConvertFrom-Json -ErrorAction Stop
126-
$binaries = ''
127-
$binMatches = @()
128-
if (-not $manifest) { continue }
129-
if ($manifest.bin) {
130-
foreach ($binEntry in $manifest.bin) {
131-
$exe = $null; $alias = $null
132-
if ($binEntry -is [System.Object[]]) {
133-
$exe = $binEntry[0]
134-
$alias = if ($binEntry.Count -gt 1) { $binEntry[1] } else { $null }
135-
} else { $exe = $binEntry }
136-
$exeName = [System.IO.Path]::GetFileNameWithoutExtension([string]$exe)
137-
if ($exeName.IndexOf($query, [StringComparison]::OrdinalIgnoreCase) -ge 0 -or
138-
($alias -and $alias.IndexOf($query, [StringComparison]::OrdinalIgnoreCase) -ge 0)) {
139-
$binMatches += [System.IO.Path]::GetFileName([string]$exe)
128+
foreach ($entry in $searchIndexApps[$appName]) {
129+
try {
130+
$manifest = Get-Content -Path $entry.path -Raw | ConvertFrom-Json -ErrorAction Stop
131+
$binaries = ''
132+
$binMatches = @()
133+
if (-not $manifest) { continue }
134+
if ($manifest.bin) {
135+
foreach ($binEntry in $manifest.bin) {
136+
$exe = $null; $alias = $null
137+
if ($binEntry -is [System.Object[]]) {
138+
$exe = $binEntry[0]
139+
$alias = if ($binEntry.Count -gt 1) { $binEntry[1] } else { $null }
140+
} else { $exe = $binEntry }
141+
$exeName = [System.IO.Path]::GetFileNameWithoutExtension([string]$exe)
142+
if ($exeName.IndexOf($query, [StringComparison]::OrdinalIgnoreCase) -ge 0 -or
143+
($alias -and $alias.IndexOf($query, [StringComparison]::OrdinalIgnoreCase) -ge 0)) {
144+
$binMatches += [System.IO.Path]::GetFileName([string]$exe)
145+
}
140146
}
147+
if ($binMatches) { $binaries = $binMatches -join ' | ' }
141148
}
142-
if ($binMatches) { $binaries = $binMatches -join ' | ' }
143-
}
144-
$list.Add([PSCustomObject]@{ Name = $appName; Version = $manifest.version; Source = $info.bucket; Binaries = $binaries })
145-
} catch { }
149+
$list.Add([PSCustomObject]@{ Name = $appName; Version = $manifest.version; Source = $entry.bucket; Binaries = $binaries })
150+
} catch { Write-Debug "index search parse failed for $($entry.path): $($_.Exception.Message)" }
151+
}
146152
}
147153
}
148154

0 commit comments

Comments
 (0)