-
Notifications
You must be signed in to change notification settings - Fork 380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Collection was modified; enumeration operation may not execute when running Invoke-ScriptAnalyzer #1516
Comments
Thank you for the detailed report. I can reproduce (only had to change the hard-coded path to the custom rule in the psd1 to match my local path) and can confirm this happens with 1.19.0 but not 1.18.3. As the origin of this bug is the
Full stack trace is:
About the other issue that you mentioned: It is not directly related to it because the origin of the other issue is reasonably well understood (Test-ModuleManifest is not thread safe due to PowerShell internals) and also its fix, which was to have a lock around it. Where the issue is similar is that there are elements of the PowerShell engine that are not thread safe. I need to spend more time looking into to see if a) a lock around the call would prevent it from happening and b) attach the debugger to PowerShell itself to see if there is something that can be fixed. cc @rjmholt Update: The exception inside PowerShell itself is coming from here when calling
|
@bergmeister Thanks for the update. As you suggested, I have reverted back to 1.18.3 for now. |
@bergmeister Just a quick question. If the issue is related to PowerShell, why does it not affect 1.18.3? |
Because |
@bergmeister Thanks for the info. |
It seems to be that the following issue in PowerShell is the root cause: PowerShell/PowerShell#4003 |
@bergmeister Thanks for the update. Please note that I use PSScriptAnalyzer from both VS Code and the commandline. I like the second option as I would rather have the rule there, but also implementing the first option would allow the disabling of the rule for those concerned about any performance issues. |
I found a better way of implementing option 2 by specifically catching the exception when it happens and then requesting a fresh, new object that does not come from the cache. Watch PR #1523 for that. 1..100 | ForEach-Object { $null = Invoke-ScriptAnalyzer -ScriptDefinition @'
Get-Content
Test-Path
Get-ChildItem
Get-Content
Test-Path
Get-ChildItem
'@ -Settings @{ 'Rules' = @{ 'PSUseCorrectCasing' = @{ 'Enable' = $true } } } } |
Very nice work! |
Before submitting a bug report:
This may be related to:
PSUseToExportFieldsInManifest throw System.InvalidOperationException: Collection was modified; enumeration operation may not execute #902
Steps to reproduce
PSScriptAnalyzer - Collection was modified.zip
$x = .\Test.ps1 -Run 10 -SAVersion '1.19.0'
Collection was modified; enumeration operation may not execute
exceptions. If run several times different files will have these exceptions.Expected behavior
No
Collection was modified; enumeration operation may not execute.
issues.If I change to a previous version of the module the issue goes away:
$x = .\Test.ps1 -Run 10 -SAVersion '1.18.3'
Collection was modified; enumeration operation may not execute
exceptions. If run several times there will never be any such exceptions.Actual behavior
Several issues are returned. If run multiple times different files show the issues. Sample error message:
If an unexpected error was thrown then please report the full error details using e.g.
$error[0] | Select-Object *
Environment data
The text was updated successfully, but these errors were encountered: