Skip to content

Commit 8dc830e

Browse files
committed
Fix BinarySearch type issue in ConvertTo-SourceLineNumber
Fixes #131 This type issue was introduced since Pwsh 7.5.0 is based on .Net 9. This solution is to type cast the variable and align it to be the same type. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/PoshCode/ModuleBuilder/issues/131?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent 0ade8f4 commit 8dc830e

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

Source/Public/ConvertTo-SourceLineNumber.ps1

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function ConvertTo-SourceLineNumber {
2222
# The SourceLineNumber (from an InvocationInfo) is the module line number
2323
[Parameter(Mandatory, ValueFromPipelineByPropertyName, Position=1, ParameterSetName="FromInvocationInfo")]
2424
[Alias("LineNumber", "Line", "ScriptLineNumber")]
25-
[int]$SourceLineNumber,
25+
$SourceLineNumber,
2626

2727
# The actual InvocationInfo
2828
[Parameter(ValueFromPipeline, DontShow, ParameterSetName="FromInvocationInfo")]
@@ -72,7 +72,8 @@ function ConvertTo-SourceLineNumber {
7272
# These are all negative, because BinarySearch returns the match *after* the line we're searching for
7373
# We need the match *before* the line we're searching for
7474
# And we need it as a zero-based index:
75-
$index = -2 - [Array]::BinarySearch($hit.StartLineNumber, $SourceLineNumber)
75+
# Cast $SourceLineNumber to the type of the first item in $hit.StartLineNumber
76+
$index = -2 - [Array]::BinarySearch($hit.StartLineNumber, $($SourceLineNumber -as $hit.StartLineNumber[0].GetType()) )
7677
$Source = $hit[$index]
7778

7879
if($Passthru) {

Tests/Public/ConvertTo-SourceLineNumber.Tests.ps1

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ Describe "ConvertTo-SourceLineNumber" {
2020
Pop-Location -StackName ConvertTo-SourceLineNumber
2121
}
2222

23-
2423
It "Should map line <outputLine> in the Module to line <sourceLine> in the source of <sourceFile>" -TestCases $TestCases {
2524
param($outputLine, $sourceFile, $sourceLine)
2625

@@ -79,4 +78,14 @@ Describe "ConvertTo-SourceLineNumber" {
7978
$SourceLocation.SourceLineNumber | Should -Be 5
8079
$SourceLocation.Function | Should -Be 'Get-Source'
8180
}
81+
82+
It 'Should handle type differences correctly' {
83+
$SourceLocation = ConvertTo-SourceLineNumber -SourceFile $Convert_LineNumber_ModulePath -SourceLineNumber [System.UInt64]48
84+
$SourceLocation.SourceFile | Should -Be ".${\}Public${\}Get-Source.ps1"
85+
$SourceLocation.SourceLineNumber | Should -Be 5
86+
87+
$SourceLocation = ConvertTo-SourceLineNumber -SourceFile $Convert_LineNumber_ModulePath -SourceLineNumber [System.Int32]48
88+
$SourceLocation.SourceFile | Should -Be ".${\}Public${\}Get-Source.ps1"
89+
$SourceLocation.SourceLineNumber | Should -Be 5
90+
}
8291
}

0 commit comments

Comments
 (0)