PowerShell / PowerShell/PowerShell

Write-Debug ignores Debug switch in Foreach-Object Parallel block

Open
#24,016 18 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs-Triage
Dominant language
C#
Stars
55.5k
Forks
8.5k
Avg merge
1d 2h
Merged PRs (30d)
88

Description

Prerequisites
Steps to reproduce

Run this script with two identical blocks of code except for the "Parallel" option.

Write-Host 'Test 1 - non-parallel'
$DebugPreference = 'SilentlyContinue'
1..2 |
ForEach-Object {
    Write-Debug "Item $_`: Write-Debug" 
    Write-Debug "Item $_`: Write-Debug -Debug" -Debug 
    Write-Debug "Item $_`: Write-Debug -Debug:`$true" -Debug:$true 
    Write-Debug "Item $_`: Write-Debug -Debug:`$false" -Debug:$false 
} 

Write-Host 'Test 2 - parallel'
$DebugPreference = 'SilentlyContinue'
1..2 |
ForEach-Object -Parallel {
    Write-Debug "Item $_`: Write-Debug"
    Write-Debug "Item $_`: Write-Debug -Debug" -Debug
    Write-Debug "Item $_`: Write-Debug -Debug:`$true" -Debug:$true
    Write-Debug "Item $_`: Write-Debug -Debug:`$false" -Debug:$false
}

Write-Host 'Done'
Expected behavior
Test 1 - non-parallel
DEBUG: Item 1: Write-Debug -Debug
DEBUG: Item 1: Write-Debug -Debug:$true
DEBUG: Item 2: Write-Debug -Debug
DEBUG: Item 2: Write-Debug -Debug:$true
Test 2 - parallel
DEBUG: Item 1: Write-Debug -Debug
DEBUG: Item 1: Write-Debug -Debug:$true
DEBUG: Item 2: Write-Debug -Debug
DEBUG: Item 2: Write-Debug -Debug:$true
Done
Actual behavior
Test 1 - non-parallel
DEBUG: Item 1: Write-Debug -Debug
DEBUG: Item 1: Write-Debug -Debug:$true
DEBUG: Item 2: Write-Debug -Debug
DEBUG: Item 2: Write-Debug -Debug:$true
Test 2 - parallel
Done
Error details
NA
Environment data
Name                           Value
----                           -----
PSVersion                      7.4.3
PSEdition                      Core
GitCommitId                    7.4.3
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Visuals

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided reproduction script and compare Write-Debug behavior in ordinary and ForEach-Object -Parallel blocks. Trace the ForEach-Object -Parallel and Write-Debug entry points to determine why the Debug switch is ignored, then verify that the parallel block produces the expected debug lines while respecting DebugPreference.

Written by the indexing model from the issue text.

Assessment

Tech stack
shell
Domain
cli
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.