Skip to content

Commit 1a72352

Browse files
gavinbarronCopilot
andcommitted
fix: make Register-CfsFeed robust to non-terminating source collision
The 'already added' error is non-terminating and bypasses -ErrorAction Stop (so try/catch never fires) yet fails the PowerShell task under its default Stop preference. Pre-check Get-PackageSource (surfaces the collision that Get-PSRepository lazily misses in a fresh session), and suppress all streams on Register-PSRepository with a post-verify instead of relying on the call to not error. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 6ea00b50-c719-469b-bf9c-72120184bfe0
1 parent 11cede3 commit 1a72352

1 file changed

Lines changed: 13 additions & 14 deletions

File tree

‎tools/Get-CfsFeedCredential.ps1‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,20 @@ function Register-CfsFeed {
3535
# Registers the private feed as a Trusted PSRepository (idempotent). Persisted under the user's
3636
# PowerShellGet config, so a single registration per job is visible to later steps and runspaces.
3737
$cred = Get-CfsFeedCredential
38-
if (Get-PSRepository -Name $script:CfsFeedName -ErrorAction SilentlyContinue) {
38+
# Get-PSRepository can lazily return nothing in a fresh session even when the source is already
39+
# persisted (registered by an earlier step/runspace). Get-PackageSource surfaces that collision,
40+
# so check both before attempting to register.
41+
if ((Get-PSRepository -Name $script:CfsFeedName -ErrorAction SilentlyContinue) -or
42+
(Get-PackageSource -Name $script:CfsFeedName -ErrorAction SilentlyContinue)) {
43+
Write-Host "Package source '$($script:CfsFeedName)' is already registered; skipping."
3944
return
4045
}
41-
try {
42-
Register-PSRepository -Name $script:CfsFeedName -SourceLocation $script:CfsFeedUrl -InstallationPolicy Trusted -Credential $cred -ErrorAction Stop
43-
}
44-
catch {
45-
# A package source with this name may already exist at the PackageManagement layer (e.g. a
46-
# NuGet source, or a registration from an earlier step/runspace) that Get-PSRepository does
47-
# not surface. If a usable PSRepository now exists, the registration effectively succeeded, so
48-
# swallow the "already added" collision; otherwise rethrow the real failure.
49-
if (Get-PSRepository -Name $script:CfsFeedName -ErrorAction SilentlyContinue) {
50-
Write-Host "PSRepository '$($script:CfsFeedName)' is already registered; continuing."
51-
return
52-
}
53-
throw
46+
# The "already added" collision is a non-terminating error emitted by an internal PackageManagement
47+
# cmdlet that bypasses -ErrorAction Stop (and would otherwise fail the task under its default Stop
48+
# preference), so redirect every stream to null and verify the outcome instead of trusting the call.
49+
Register-PSRepository -Name $script:CfsFeedName -SourceLocation $script:CfsFeedUrl -InstallationPolicy Trusted -Credential $cred -ErrorAction SilentlyContinue *> $null
50+
if (-not (Get-PSRepository -Name $script:CfsFeedName -ErrorAction SilentlyContinue)) {
51+
throw "Failed to register PSRepository '$($script:CfsFeedName)'."
5452
}
53+
Write-Host "Registered PSRepository '$($script:CfsFeedName)'."
5554
}

0 commit comments

Comments
 (0)