Title: Invoke-ScriptAnalyzer fails intermittently with NullReferenceException in CommandInfo.ResolveParameter (ProvideCommentHelp / AvoidReservedCharInCmdlet race on Export-ModuleMember) · Issue #2229 · PowerShell/PSScriptAnalyzer · GitHub
Open Graph Title: Invoke-ScriptAnalyzer fails intermittently with NullReferenceException in CommandInfo.ResolveParameter (ProvideCommentHelp / AvoidReservedCharInCmdlet race on Export-ModuleMember) · Issue #2229 · PowerShell/PSScriptAnalyzer
X Title: Invoke-ScriptAnalyzer fails intermittently with NullReferenceException in CommandInfo.ResolveParameter (ProvideCommentHelp / AvoidReservedCharInCmdlet race on Export-ModuleMember) · Issue #2229 · PowerShell/PSScriptAnalyzer
Description: Summary Invoke-ScriptAnalyzer fails intermittently on a file that calls Export-ModuleMember -Function .... The whole call fails with a RULE_ERROR. Usually the exception is a NullReferenceException in System.Management.Automation.CommandI...
Open Graph Description: Summary Invoke-ScriptAnalyzer fails intermittently on a file that calls Export-ModuleMember -Function .... The whole call fails with a RULE_ERROR. Usually the exception is a NullReferenceException ...
X Description: Summary Invoke-ScriptAnalyzer fails intermittently on a file that calls Export-ModuleMember -Function .... The whole call fails with a RULE_ERROR. Usually the exception is a NullReferenceException ...
Opengraph URL: https://github.com/PowerShell/PSScriptAnalyzer/issues/2229
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Invoke-ScriptAnalyzer fails intermittently with NullReferenceException in CommandInfo.ResolveParameter (ProvideCommentHelp / AvoidReservedCharInCmdlet race on Export-ModuleMember)","articleBody":"## Summary\n\n`Invoke-ScriptAnalyzer` fails intermittently on a file that calls `Export-ModuleMember -Function ...`. The whole call fails with a `RULE_ERROR`. Usually the exception is a `NullReferenceException` in `System.Management.Automation.CommandInfo.ResolveParameter`. Less often it is an `InvalidOperationException` from `PSLocalEventManager.SubscribeEvent` (\"You cannot have more than one dynamic module in each dynamic assembly in this version of the runtime.\").\n\n## Environment\n\n- PSScriptAnalyzer 1.25.0\n- PowerShell 7.6.5 (pwsh)\n- Windows 11 Enterprise (x64)\n- Each run is a fresh `pwsh -NoProfile -NonInteractive` process. No profile, no other modules.\n\n## Minimal repro\n\n1. Create a folder with one file, `Probe.psm1`:\n\n ```powershell\n function Get-Probe { 'probe' }\n Export-ModuleMember -Function Get-Probe\n ```\n\n2. Run the analyzer in a fresh process, several times:\n\n ```powershell\n $fixture = '\u003cpath to the folder>'\n $child = @\"\n `$ErrorActionPreference = 'Stop'\n try {\n Import-Module PSScriptAnalyzer -Force\n `$r = @(Invoke-ScriptAnalyzer -Path '$fixture' -Recurse)\n \"OK count=`$(`$r.Count)\"\n }\n catch { \"EXC: `$(`$_.Exception.GetType().FullName): `$(`$_.Exception.Message)\"; exit 3 }\n \"@\n 1..15 | ForEach-Object { pwsh -NoProfile -NonInteractive -Command $child }\n ```\n\nA second fixture adds a class file beside the module (`ProbeBox.ps1`: `class ProbeBox { [NoSuchProbeType] $Value }`). It fails at a similar rate. The class file is not needed to trigger the fault.\n\n## Expected\n\nEvery run completes and returns the same findings (here: 1 finding).\n\n## Actual\n\nSome runs fail. The whole `Invoke-ScriptAnalyzer` call ends with `FullyQualifiedErrorId: RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand`.\n\nStack, variant 1 (most frequent; the rule frame is `ProvideCommentHelp` or\n`AvoidReservedCharInCmdlet`):\n\n```\nSystem.NullReferenceException: Object reference not set to an instance of an object.\n at System.Management.Automation.CommandInfo.ResolveParameter(String name)\n at Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetExportedFunction(Ast ast)\n at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AvoidReservedCharInCmdlet.AnalyzeScript(Ast ast, String fileName)+MoveNext()\n at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\n at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\n at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.\u003c>c__DisplayClass84_1.\u003cAnalyzeSyntaxTree>b__2()\n```\n\nStack, variant 2:\n\n```\nSystem.InvalidOperationException: You cannot have more than one dynamic module in each dynamic assembly in this version of the runtime.\n at System.Reflection.Emit.RuntimeAssemblyBuilder.DefineDynamicModuleCore(String name)\n at System.Management.Automation.PSLocalEventManager.ProcessNewSubscriber(PSEventSubscriber subscriber, Object source, String eventName, String sourceIdentifier, PSObject data, Boolean supportEvent, Boolean forwardEvent)\n at System.Management.Automation.PSLocalEventManager.SubscribeEvent(Object source, String eventName, String sourceIdentifier, PSObject data, PSEventReceivedEventHandler handlerDelegate, Boolean supportEvent, Boolean forwardEvent, Int32 maxTriggerCount)\n at System.Management.Automation.PSLocalEventManager.SubscribeEvent(Object source, String eventName, String sourceIdentifier, PSObject data, PSEventReceivedEventHandler handlerDelegate, Boolean supportEvent, Boolean forwardEvent, Boolean shouldQueueAndProcessInExecutionThread, Int32 maxTriggerCount)\n at System.Management.Automation.CommandInfo.GetMergedCommandParameterMetadataSafely()\n at System.Management.Automation.CommandInfo.ResolveParameter(String name)\n at Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetExportedFunction(Ast ast)\n at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.ProvideCommentHelp.AnalyzeScript(Ast ast, String fileName)+MoveNext()\n at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)\n at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)\n at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.\u003c>c__DisplayClass84_1.\u003cAnalyzeSyntaxTree>b__2()\n```\n\n## Frequency\n\n| Target | Runs | Failed |\n|---|---|---|\n| Minimal module above (`Export-ModuleMember -Function`) | 15 | 6 (4 NullReferenceException, 2 InvalidOperationException) |\n| Minimal module plus a class file | 30 | 8 |\n| A real multi-file module (one `Export-ModuleMember` call), `-Recurse` | 25 | 4 |\n\nThe same module, signed or unsigned, fails at about the same rate.\n\n## Analysis\n\n- The analyzer runs the rules for one file in parallel, one task per rule.\n- `ProvideCommentHelp` (`PSProvideCommentHelp`) and `AvoidReservedCharInCmdlet` (`PSReservedCmdletChar`) both call `Helper.GetExportedFunction`. In 1.25.0 no other caller exists.\n- `GetExportedFunction` calls `CommandInfo.ResolveParameter` on the cached `Export-ModuleMember` `CmdletInfo`. That object belongs to a runspace of the analyzer's own pool. The call comes from the rule's thread pool thread.\n- PowerShell sends such a cross-thread call through a one-shot engine event (`GetMergedCommandParameterMetadataSafely`). That path does not support two concurrent callers. So one rule gets a null result (variant 1), or the two subscriptions collide (variant 2).\n\nA possible fix: resolve the `Export-ModuleMember` parameter metadata once, on the runspace thread, or serialize `GetExportedFunction` across rules.\n\n## Workaround\n\nRun the two rules in their own passes, so that nothing else uses the analyzer's runspace at the same time:\n\n```powershell\n$isolated = 'PSProvideCommentHelp', 'PSReservedCmdletChar'\n$r = @(Invoke-ScriptAnalyzer -Path $path -Recurse -ExcludeRule $isolated)\nforeach ($rule in $isolated) {\n # Keep only that rule's findings: each pass reports the parse diagnostics again.\n $r += @(Invoke-ScriptAnalyzer -Path $path -Recurse -IncludeRule $rule |\n Where-Object RuleName -eq $rule)\n}\n```\n\nWith the workaround: 0 failures in 40 runs of the minimal module, 0 in 30 runs of the module with a class file, and 0 in 15 runs over three real modules. The findings are the same as those of a complete single pass.","author":{"url":"https://github.com/ondrejtucny","@type":"Person","name":"ondrejtucny"},"datePublished":"2026-10-19T18:23:21Z","url":"https://github.com/PowerShell/PSScriptAnalyzer/issues/2229"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:5c46d723-2d81-acf5-436e-d574d66a6614 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EDF0:C0CAB:6E7C:92EA:6AFCD7B4 |
| html-safe-nonce | cbb256d03c8fad8a3b505572f362c478a75af56690d8a3e0eadd3bcb8fd000a4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFREYwOkMwQ0FCOjZFN0M6OTJFQTo2QUZDRDdCNCIsInZpc2l0b3JfaWQiOiI1ODU0ODk4Njg1MjYyNzQ3NTcyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | f51e5acac52722370d0955b835804b37e970bfe33da8b51bd37432024d95eb7a |
| hovercard-subject-tag | issue:5929230172 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PSScriptAnalyzer/2229/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b7b73e082a634084f6b087e743bdcd733562213737a31573e3253533967e8c9b/PowerShell/PSScriptAnalyzer/issues/2229 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b7b73e082a634084f6b087e743bdcd733562213737a31573e3253533967e8c9b/PowerShell/PSScriptAnalyzer/issues/2229 |
| og:image:alt | Summary Invoke-ScriptAnalyzer fails intermittently on a file that calls Export-ModuleMember -Function .... The whole call fails with a RULE_ERROR. Usually the exception is a NullReferenceException ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | ondrejtucny |
| hostname | github.com |
| expected-hostname | github.com |
| None | b5b7bc7e41e0a604bbdb5035c7ed9620c2d28b9c8a18747182e2262a75abfd91 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PSScriptAnalyzer git https://github.com/PowerShell/PSScriptAnalyzer.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 33149177 |
| octolytics-dimension-repository_nwo | PowerShell/PSScriptAnalyzer |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 33149177 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PSScriptAnalyzer |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ff5022cc3aa767e6115451d9c322dd975506d40e |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width