Fix transcript logging for exit flow control#27526
Conversation
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds a regression test and a runtime change to prevent PowerShell transcription from logging terminating errors when execution ends via flow control (e.g., exit from an advanced function).
Changes:
- Add Pester coverage for transcription behavior when an advanced function calls
exit. - Update pipeline error logging to ignore
FlowControlExceptionduring execution exception logging.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 | Adds a regression test ensuring transcripts don’t include a terminating error record when a function exits via exit. |
| src/System.Management.Automation/engine/pipeline.cs | Skips execution-exception logging for FlowControlException to avoid emitting terminating error records for flow-control exits. |
| Exit-Script | ||
| "@ | Set-Content -Path $scriptFilePath | ||
|
|
||
| & "$PSHOME/pwsh" -NoProfile -File $scriptFilePath *> $null |
There was a problem hiding this comment.
Addressed in 405e5b6: the test now builds an OS-aware executable name and uses Join-Path $PSHOME ... before invoking the child process. Tested with Start-PSBuild -Configuration Debug -Runtime win7-x64 and Start-PSPester -Path test\powershell\Modules\Microsoft.Powershell.Host\Start-Transcript.Tests.ps1 (23 passed).
|
|
||
| $LASTEXITCODE | Should -Be 255 | ||
| $exitTranscriptFilePath | Should -Exist | ||
| Get-Content -Path $exitTranscriptFilePath -Raw | Should -Not -Match 'TerminatingError\(\): "System error\."' |
There was a problem hiding this comment.
Addressed in 405e5b6: the assertion now checks for absence of TerminatingError() in the transcript instead of matching the exact localized message text. Tested with Start-PSBuild -Configuration Debug -Runtime win7-x64 and Start-PSPester -Path test\powershell\Modules\Microsoft.Powershell.Host\Start-Transcript.Tests.ps1 (23 passed).
| if (exception is FlowControlException) | ||
| return; |
There was a problem hiding this comment.
Addressed in 405e5b6: added braces around the early-return paths in LogExecutionError / LogExecutionException. Tested with Start-PSBuild -Configuration Debug -Runtime win7-x64 and Start-PSPester -Path test\powershell\Modules\Microsoft.Powershell.Host\Start-Transcript.Tests.ps1 (23 passed).
Problem
Using
exitinside an advanced function can cause transcript logging to recordTerminatingError(): "System error.", even thoughexitis a flow-control operation rather than an error that should be written to the transcript.Root cause
PipelineProcessor.LogExecutionExceptionlogs every exception it receives as a terminating pipeline error.ExitExceptionderives fromFlowControlException, so the transcript path treats the internal control-flow exception as a user-visible terminating error.Solution
Skip transcript terminating-error logging for
FlowControlExceptioninstances, and add a regression test that launches a child PowerShell process where an advanced function exits with code 255 while transcription is active.Tests run
pwsh: transcript containedPS>TerminatingError(): "System error."Start-PSBuild -UseNuGetOrg -SkipExperimentalFeatureGenerationStart-PSPester -Path ./test/powershell/Modules/Microsoft.Powershell.Host/Start-Transcript.Tests.ps1 -UseNuGetOrg -SkipTestToolBuild -ThrowOnFailure(23 passed)git diff --check(only existing Windows line-ending warnings)Fixes #26625