Skip to content

Fix mock bootstrap proxy for cmdlets with no DefaultParameterSetName#2887

Merged
nohwnd merged 3 commits into
mainfrom
nohwnd-diagnose-bugs-1531-1575-2113
Jul 18, 2026
Merged

Fix mock bootstrap proxy for cmdlets with no DefaultParameterSetName#2887
nohwnd merged 3 commits into
mainfrom
nohwnd-diagnose-bugs-1531-1575-2113

Conversation

@nohwnd

@nohwnd nohwnd commented Jul 17, 2026

Copy link
Copy Markdown
Member

Fix #1531

Mocking a cmdlet with multiple non-mandatory parameter sets and no DefaultParameterSetName (e.g. Get-PackageSource with its NuGet and PowerShellGet dynamic sets) failed at call time with:

ParameterBindingException: Parameter set cannot be resolved using the specified named parameters.
One or more parameters issued cannot be used together or an insufficient number of parameters were provided.

When Create-MockHook builds the bootstrap proxy it always injects PositionalBinding=$false into the generated [CmdletBinding()]. For a cmdlet whose dynamic parameters add several named sets with no default, PowerShell has no set to fall back on when the proxy is called with no arguments, hence the ambiguity. The real cmdlet handles this in C# and is fine, the PS-level proxy is not.

Fix: when $metadata.DefaultParameterSetName is null or empty, inject DefaultParameterSetName='__AllParameterSets' into the proxy's [CmdletBinding()]. Cmdlets that already have a default set are untouched, the existing value is preserved and the injection is skipped.

Added a regression test in Mock.Tests.ps1 that mocks Get-PackageSource (ships with PS7) and calls it with no arguments, skipped when the cmdlet is not available.

…o default set

When a cmdlet has no DefaultParameterSetName and its dynamic parameters introduce
multiple named parameter sets (e.g. Get-PackageSource with NuGet/PowerShellGet),
calling the mock with no arguments fails with 'Parameter set cannot be resolved'.

Fix: when $metadata.DefaultParameterSetName is null/empty for a cmdlet, inject
DefaultParameterSetName='__AllParameterSets' into the generated [CmdletBinding()]
of the bootstrap proxy so PowerShell can resolve the parameter set.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@nohwnd nohwnd added this to the 6.1.0 milestone Jul 17, 2026
Comment thread src/functions/Mock.ps1
nohwnd and others added 2 commits July 17, 2026 22:01
Co-authored-by: Frode Flaten <3436158+fflaten@users.noreply.github.com>
…me is injected

The suggestion from @fflaten moved the comma guard to the top of the Cmdlet block,
but when DefaultParameterSetName='__AllParameterSets' is also injected the comma
between DefaultParameterSetName and PositionalBinding=$false was missing, causing
a ParseException at scriptblock creation time.

Add the comma immediately after the DefaultParameterSetName insertion so both
attributes are correctly separated.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@nohwnd
nohwnd merged commit 00b40ca into main Jul 18, 2026
13 checks passed
@nohwnd
nohwnd deleted the nohwnd-diagnose-bugs-1531-1575-2113 branch July 18, 2026 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot execute mock for Get-PackageSource cmdlet

2 participants