-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Closed
Labels
In-PRIndicates that a PR is out for the issueIndicates that a PR is out for the issue
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Attempting modification of a collection while it is being enumerated should result in an error:
An error occurred while enumerating through a collection: Collection was modified; enumeration operation may not execute.
This error isn't consistently surfaced - note that this is a regression from Windows PowerShell:
# OK: Reports an error for the attempt to modify the collection being enumerated.
'--- foreach'
$l = [System.Collections.Generic.List[string]] @('one', 'two')
foreach ($e in $l) { $l.Remove($e) }
# !! Fails QUIETLY in PS CORE as of v7.4.0-preview.5
'--- pipeline'
$l = [System.Collections.Generic.List[string]] @('one', 'two')
$l | ForEach-Object { $l.Remove($_) }Important: The quiet failure only occurs when the code is executed via a script file; if you paste the last two statements into an interactive console, the error does surface.
[Update: NOT PowerShell's fault - see below] The following WinForms example affects Windows PowerShell too (both interactively and in a script):
using namespace System.Windows.Forms
Add-Type -Assembly System.Windows.Forms
$f = [Form]::new()
$f.Controls.AddRange((
[CheckBox]::new(),
[CheckBox]::new()
))
# !! Should fail with an error message, but quietly aborts after the first .Remove() call.
$f.Controls | ForEach-Object { $f.Controls.Remove($_) }
$f.Controls.Count # !! still nonzeroExpected behavior
First snippet:
- Both the
foreachand theForEach-Objectstatement should report the expected error.
Second snippet:
- The expected error should occur.
Actual behavior
First snippet:
- The
ForEach-Objectstatement doesn't report an error and quietly aborts the pipeline after the first.Remove()call
Second snippet:
- The expected error doesn't occur; quietly aborts the pipeline after the first
.Remove()call.
Error details
No response
Environment data
PowerShell 7.4.0-preview.5Visuals
No response
Metadata
Metadata
Assignees
Labels
In-PRIndicates that a PR is out for the issueIndicates that a PR is out for the issue