From 8dc830e7a72c5b572cd708a3917fd0b832f86d1b Mon Sep 17 00:00:00 2001 From: Mike Bijl Date: Mon, 3 Feb 2025 14:55:51 +0100 Subject: [PATCH 1/7] 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). --- Source/Public/ConvertTo-SourceLineNumber.ps1 | 5 +++-- Tests/Public/ConvertTo-SourceLineNumber.Tests.ps1 | 11 ++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Source/Public/ConvertTo-SourceLineNumber.ps1 b/Source/Public/ConvertTo-SourceLineNumber.ps1 index 6c2e8a9..7b9f48e 100644 --- a/Source/Public/ConvertTo-SourceLineNumber.ps1 +++ b/Source/Public/ConvertTo-SourceLineNumber.ps1 @@ -22,7 +22,7 @@ function ConvertTo-SourceLineNumber { # The SourceLineNumber (from an InvocationInfo) is the module line number [Parameter(Mandatory, ValueFromPipelineByPropertyName, Position=1, ParameterSetName="FromInvocationInfo")] [Alias("LineNumber", "Line", "ScriptLineNumber")] - [int]$SourceLineNumber, + $SourceLineNumber, # The actual InvocationInfo [Parameter(ValueFromPipeline, DontShow, ParameterSetName="FromInvocationInfo")] @@ -72,7 +72,8 @@ function ConvertTo-SourceLineNumber { # These are all negative, because BinarySearch returns the match *after* the line we're searching for # We need the match *before* the line we're searching for # And we need it as a zero-based index: - $index = -2 - [Array]::BinarySearch($hit.StartLineNumber, $SourceLineNumber) + # Cast $SourceLineNumber to the type of the first item in $hit.StartLineNumber + $index = -2 - [Array]::BinarySearch($hit.StartLineNumber, $($SourceLineNumber -as $hit.StartLineNumber[0].GetType()) ) $Source = $hit[$index] if($Passthru) { diff --git a/Tests/Public/ConvertTo-SourceLineNumber.Tests.ps1 b/Tests/Public/ConvertTo-SourceLineNumber.Tests.ps1 index 0b423a9..8c582e6 100644 --- a/Tests/Public/ConvertTo-SourceLineNumber.Tests.ps1 +++ b/Tests/Public/ConvertTo-SourceLineNumber.Tests.ps1 @@ -20,7 +20,6 @@ Describe "ConvertTo-SourceLineNumber" { Pop-Location -StackName ConvertTo-SourceLineNumber } - It "Should map line in the Module to line in the source of " -TestCases $TestCases { param($outputLine, $sourceFile, $sourceLine) @@ -79,4 +78,14 @@ Describe "ConvertTo-SourceLineNumber" { $SourceLocation.SourceLineNumber | Should -Be 5 $SourceLocation.Function | Should -Be 'Get-Source' } + + It 'Should handle type differences correctly' { + $SourceLocation = ConvertTo-SourceLineNumber -SourceFile $Convert_LineNumber_ModulePath -SourceLineNumber [System.UInt64]48 + $SourceLocation.SourceFile | Should -Be ".${\}Public${\}Get-Source.ps1" + $SourceLocation.SourceLineNumber | Should -Be 5 + + $SourceLocation = ConvertTo-SourceLineNumber -SourceFile $Convert_LineNumber_ModulePath -SourceLineNumber [System.Int32]48 + $SourceLocation.SourceFile | Should -Be ".${\}Public${\}Get-Source.ps1" + $SourceLocation.SourceLineNumber | Should -Be 5 + } } From f79e38a6af21e2a7a8a510492e6372ee1207870b Mon Sep 17 00:00:00 2001 From: Mike Bijl Date: Tue, 4 Feb 2025 10:16:25 +0100 Subject: [PATCH 2/7] Update ConvertTo-SourceLineNumber.ps1 Changed line 61 instead of 76 due to cleaner coding and accomplishing the same --- Source/Public/ConvertTo-SourceLineNumber.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Public/ConvertTo-SourceLineNumber.ps1 b/Source/Public/ConvertTo-SourceLineNumber.ps1 index 7b9f48e..2785e11 100644 --- a/Source/Public/ConvertTo-SourceLineNumber.ps1 +++ b/Source/Public/ConvertTo-SourceLineNumber.ps1 @@ -58,7 +58,7 @@ function ConvertTo-SourceLineNumber { [PSCustomObject]@{ PSTypeName = "BuildSourceMapping" SourceFile = $_.Matches[0].Groups["SourceFile"].Value.Trim("'") - StartLineNumber = $_.LineNumber + StartLineNumber = [System.Int32] $_.LineNumber # This offset is added when calculating the line number # because of the new line we're adding prior to the content # of each script file in the built module. @@ -73,7 +73,7 @@ function ConvertTo-SourceLineNumber { # We need the match *before* the line we're searching for # And we need it as a zero-based index: # Cast $SourceLineNumber to the type of the first item in $hit.StartLineNumber - $index = -2 - [Array]::BinarySearch($hit.StartLineNumber, $($SourceLineNumber -as $hit.StartLineNumber[0].GetType()) ) + $index = -2 - [Array]::BinarySearch($hit.StartLineNumber, $SourceLineNumber ) $Source = $hit[$index] if($Passthru) { From 1625ac08840faecb8ece282897f776ee9e822331 Mon Sep 17 00:00:00 2001 From: Mike Bijl Date: Tue, 4 Feb 2025 10:17:22 +0100 Subject: [PATCH 3/7] Update ConvertTo-SourceLineNumber.ps1 removed comment from previous commit --- Source/Public/ConvertTo-SourceLineNumber.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/Source/Public/ConvertTo-SourceLineNumber.ps1 b/Source/Public/ConvertTo-SourceLineNumber.ps1 index 2785e11..dfed209 100644 --- a/Source/Public/ConvertTo-SourceLineNumber.ps1 +++ b/Source/Public/ConvertTo-SourceLineNumber.ps1 @@ -72,7 +72,6 @@ function ConvertTo-SourceLineNumber { # These are all negative, because BinarySearch returns the match *after* the line we're searching for # We need the match *before* the line we're searching for # And we need it as a zero-based index: - # Cast $SourceLineNumber to the type of the first item in $hit.StartLineNumber $index = -2 - [Array]::BinarySearch($hit.StartLineNumber, $SourceLineNumber ) $Source = $hit[$index] From 5a7633685fb16102f014f53ec31473e9f3c35c8d Mon Sep 17 00:00:00 2001 From: Mike Bijl Date: Tue, 4 Feb 2025 10:18:13 +0100 Subject: [PATCH 4/7] Update ConvertTo-SourceLineNumber.ps1 removed unnecessary whitespace --- Source/Public/ConvertTo-SourceLineNumber.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Public/ConvertTo-SourceLineNumber.ps1 b/Source/Public/ConvertTo-SourceLineNumber.ps1 index dfed209..f9ea85c 100644 --- a/Source/Public/ConvertTo-SourceLineNumber.ps1 +++ b/Source/Public/ConvertTo-SourceLineNumber.ps1 @@ -72,7 +72,7 @@ function ConvertTo-SourceLineNumber { # These are all negative, because BinarySearch returns the match *after* the line we're searching for # We need the match *before* the line we're searching for # And we need it as a zero-based index: - $index = -2 - [Array]::BinarySearch($hit.StartLineNumber, $SourceLineNumber ) + $index = -2 - [Array]::BinarySearch($hit.StartLineNumber, $SourceLineNumber) $Source = $hit[$index] if($Passthru) { From 43f2a9b5ba80abf9dfc4e8d187570a69b185cea1 Mon Sep 17 00:00:00 2001 From: Mike Bijl Date: Tue, 4 Feb 2025 10:19:31 +0100 Subject: [PATCH 5/7] Update ConvertTo-SourceLineNumber.ps1 undo change to parameter type --- Source/Public/ConvertTo-SourceLineNumber.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Public/ConvertTo-SourceLineNumber.ps1 b/Source/Public/ConvertTo-SourceLineNumber.ps1 index f9ea85c..b764c94 100644 --- a/Source/Public/ConvertTo-SourceLineNumber.ps1 +++ b/Source/Public/ConvertTo-SourceLineNumber.ps1 @@ -22,7 +22,7 @@ function ConvertTo-SourceLineNumber { # The SourceLineNumber (from an InvocationInfo) is the module line number [Parameter(Mandatory, ValueFromPipelineByPropertyName, Position=1, ParameterSetName="FromInvocationInfo")] [Alias("LineNumber", "Line", "ScriptLineNumber")] - $SourceLineNumber, + [Int]$SourceLineNumber, # The actual InvocationInfo [Parameter(ValueFromPipeline, DontShow, ParameterSetName="FromInvocationInfo")] From 1e75ab59aaf9a2cba1b005fb4b2bebe7f7278660 Mon Sep 17 00:00:00 2001 From: Mike Bijl Date: Tue, 4 Feb 2025 10:20:38 +0100 Subject: [PATCH 6/7] Update ConvertTo-SourceLineNumber.ps1 undo uppercase to lower case change --- Source/Public/ConvertTo-SourceLineNumber.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Public/ConvertTo-SourceLineNumber.ps1 b/Source/Public/ConvertTo-SourceLineNumber.ps1 index b764c94..41de7f0 100644 --- a/Source/Public/ConvertTo-SourceLineNumber.ps1 +++ b/Source/Public/ConvertTo-SourceLineNumber.ps1 @@ -22,7 +22,7 @@ function ConvertTo-SourceLineNumber { # The SourceLineNumber (from an InvocationInfo) is the module line number [Parameter(Mandatory, ValueFromPipelineByPropertyName, Position=1, ParameterSetName="FromInvocationInfo")] [Alias("LineNumber", "Line", "ScriptLineNumber")] - [Int]$SourceLineNumber, + [int]$SourceLineNumber, # The actual InvocationInfo [Parameter(ValueFromPipeline, DontShow, ParameterSetName="FromInvocationInfo")] From d2e2f26e7dfb7e97d50fcd9e118f0dd7a9740a0e Mon Sep 17 00:00:00 2001 From: Joel Bennett Date: Wed, 5 Feb 2025 01:55:09 -0500 Subject: [PATCH 7/7] Fix the syntax for casting SourceLineNumber --- Tests/Public/ConvertTo-SourceLineNumber.Tests.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Public/ConvertTo-SourceLineNumber.Tests.ps1 b/Tests/Public/ConvertTo-SourceLineNumber.Tests.ps1 index 8c582e6..886f993 100644 --- a/Tests/Public/ConvertTo-SourceLineNumber.Tests.ps1 +++ b/Tests/Public/ConvertTo-SourceLineNumber.Tests.ps1 @@ -80,11 +80,11 @@ Describe "ConvertTo-SourceLineNumber" { } It 'Should handle type differences correctly' { - $SourceLocation = ConvertTo-SourceLineNumber -SourceFile $Convert_LineNumber_ModulePath -SourceLineNumber [System.UInt64]48 + $SourceLocation = ConvertTo-SourceLineNumber -SourceFile $Convert_LineNumber_ModulePath -SourceLineNumber ([System.UInt64]48) $SourceLocation.SourceFile | Should -Be ".${\}Public${\}Get-Source.ps1" $SourceLocation.SourceLineNumber | Should -Be 5 - $SourceLocation = ConvertTo-SourceLineNumber -SourceFile $Convert_LineNumber_ModulePath -SourceLineNumber [System.Int32]48 + $SourceLocation = ConvertTo-SourceLineNumber -SourceFile $Convert_LineNumber_ModulePath -SourceLineNumber ([System.Int32]48) $SourceLocation.SourceFile | Should -Be ".${\}Public${\}Get-Source.ps1" $SourceLocation.SourceLineNumber | Should -Be 5 }