Skip to content

LanguagePrimitives: fix second enum value in wildcard duplicate-match error#27104

Open
cuiweixie wants to merge 1 commit intoPowerShell:masterfrom
cuiweixie:fix-EnumSingleTypeConverter-wildcard-second-value
Open

LanguagePrimitives: fix second enum value in wildcard duplicate-match error#27104
cuiweixie wants to merge 1 commit intoPowerShell:masterfrom
cuiweixie:fix-EnumSingleTypeConverter-wildcard-second-value

Conversation

@cuiweixie
Copy link
Copy Markdown

Summary

Ensures the InvalidCastEnumTwoStringsFoundAndNoFlags error lists the correct second enumerator when a wildcard string matches two names on a non-flags enumeration (the message previously repeated the first match).

Changes

  • In EnumSingleTypeConverter.BaseConvertFrom, use values.GetValue(j) for the second conflicting match instead of values.GetValue(i).
  • Add a Pester regression test that calls EnumSingleTypeConverter.ConvertFrom (via reflection) so the wildcard branch is exercised.

Testing

  • Local: Invoke-Pester on test/powershell/engine/Api/LanguagePrimitive.Tests.ps1 (new It block).

… error

EnumSingleTypeConverter used values.GetValue(i) when two enumerator names matched a wildcard; the outer index i is wrong for the second match (use j).

Add a regression test that invokes EnumSingleTypeConverter.ConvertFrom so the wildcard path is exercised (ConvertTo(string, enum) may resolve via Enum.Parse first).
Copilot AI review requested due to automatic review settings March 27, 2026 18:05
@cuiweixie cuiweixie requested a review from a team as a code owner March 27, 2026 18:05
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes an enum wildcard duplicate-match error so the “two matches found” cast exception reports the correct second enum value, and adds a regression test to exercise the wildcard matching path in LanguagePrimitives enum conversion.

Changes:

  • Fix EnumSingleTypeConverter.BaseConvertFrom to use the correct enum member index when building the second conflicting match value.
  • Add a Pester regression test that invokes EnumSingleTypeConverter.ConvertFrom via reflection to hit the wildcard branch and validate distinct matches are reported.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
src/System.Management.Automation/engine/LanguagePrimitives.cs Corrects the second conflicting enum value used in the duplicate-match error path.
test/powershell/engine/Api/LanguagePrimitive.Tests.ps1 Adds a regression test for wildcard enum-name matching that previously repeated the first match.

Comment on lines +255 to +257
$ex | Should -BeOfType [System.Management.Automation.PSInvalidCastException]
$ex.Message | Should -Match '\(Alpha, Beta\)'
$ex.Message | Should -Not -Match '\(Alpha, Alpha\)'
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

The assertions on $ex.Message are tied to the exact punctuation/formatting of the localized resource string (e.g., parentheses and comma+space). This can make the test brittle if wording/punctuation changes or if the test runs under a different UI culture. Prefer asserting on the presence/uniqueness of the enum value tokens themselves (e.g., that Alpha and Beta each appear, and Alpha appears only once) rather than matching the full (Alpha, Beta) substring.

Copilot uses AI. Check for mistakes.
Comment on lines +245 to +247
$asm = [System.Management.Automation.LanguagePrimitives].Assembly
$converterType = $asm.GetType('System.Management.Automation.LanguagePrimitives+EnumSingleTypeConverter')
$inst = [Activator]::CreateInstance($converterType, $true)
Copy link

Copilot AI Mar 27, 2026

Choose a reason for hiding this comment

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

$converterType = $asm.GetType(...) can return $null if the nested type name changes, which would make the test fail with a less-informative exception when creating the instance or calling ConvertFrom. Consider adding explicit assertions that $converterType (and $inst) are not $null so failures are clearer.

Copilot uses AI. Check for mistakes.
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.

2 participants