Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/System.Management.Automation/engine/parser/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ internal static class CachedReflectionInfo

internal static readonly MethodInfo ArgumentTransformationAttribute_Transform =
typeof(ArgumentTransformationAttribute).GetMethod(nameof(ArgumentTransformationAttribute.Transform), InstancePublicFlags);

internal static readonly MethodInfo MemberInvocationLoggingOps_LogMemberInvocation =
typeof(MemberInvocationLoggingOps).GetMethod(nameof(MemberInvocationLoggingOps.LogMemberInvocation), StaticFlags);
}
Expand Down Expand Up @@ -5617,7 +5617,9 @@ public object VisitReturnStatement(ReturnStatementAst returnStatementAst)
return Expression.Block(returnValue, returnExpr);
}

return returnExpr;
return Expression.Block(
UpdatePosition(returnStatementAst),
returnExpr);
}

public object VisitExitStatement(ExitStatementAst exitStatementAst)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,50 @@ elseif (Test-Path $PSCommandPath)
$Breakpoint.HitCount | Should -Be $HitCount
}
}

Context "Break point on return should hit" {
BeforeAll {
$return_script_1 = @'
return
'@
$return_script_2 = @'
return 10
'@

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you please add one more test case to cover return statement in a trap block?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done! Also added disassembly for traps in the description.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Probably should have waited for the tests to pass before saying that. Looking into the failure

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Okay it really does hit twice. Apparently when a trap is present that hits a breakpoint when it's registered. So even with this:

'something'

trap {
    'something else'
    return 'testing'
}

throw

If you put a breakpoint on every line and run it, line 4 hits a breakpoint first, then line 1, then 8, then 5. That seems like a bug but for another time. I've added another line before the return in the trap test so that doesn't interfere with the result.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the additional fix. Will take a look today.

$return_script_3 = @'
trap {
'statement to ignore trap registration sequence point'
return
}

throw
'@

$ReturnScript_1 = Setup -PassThru -File ReturnScript_1.ps1 -Content $return_script_1
$bp_1 = Set-PSBreakpoint -Script $ReturnScript_1 -Line 1 -Action { continue }

$ReturnScript_2 = Setup -PassThru -File ReturnScript_2.ps1 -Content $return_script_2
$bp_2 = Set-PSBreakpoint -Script $ReturnScript_2 -Line 1 -Action { continue }

$ReturnScript_3 = Setup -PassThru -File ReturnScript_3.ps1 -Content $return_script_3
$bp_3 = Set-PSBreakpoint -Script $ReturnScript_3 -Line 3 -Action { continue }

$testCases = @(
@{ Name = "return without pipeline should be hit once"; Path = $ReturnScript_1; Breakpoint = $bp_1; HitCount = 1 }
@{ Name = "return with pipeline should be hit once"; Path = $ReturnScript_2; Breakpoint = $bp_2; HitCount = 1 }
@{ Name = "return from trap should be hit once"; Path = $ReturnScript_3; Breakpoint = $bp_3; HitCount = 1 }
)
}

AfterAll {
Get-PSBreakpoint -Script $ReturnScript_1, $ReturnScript_2, $ReturnScript_3 | Remove-PSBreakpoint
}

It "Return statement <Name>" -TestCases $testCases {
param($Path, $Breakpoint, $HitCount)
$null = & $Path
$Breakpoint.HitCount | Should -Be $HitCount
}
}
}

Describe "It should be possible to reset runspace debugging" -Tag "Feature" {
Expand Down