Invoke-ScriptAnalyzer fails intermittently with NullReferenceException in CommandInfo.ResolveParameter (ProvideCommentHelp / AvoidReservedCharInCmdlet race on Export-ModuleMember)
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 52/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- csharp, powershell
- Lĩnh vực
- developer-experience, testing-qa
Hướng nghiên cứu
Start by reading Helper.GetExportedFunction and its callers, ProvideCommentHelp and AvoidReservedCharInCmdlet; the issue names these entry points and the cross-thread call to CommandInfo.ResolveParameter. Reproduce with the minimal Probe.psm1 fixture and repeated fresh pwsh runs. Done means Invoke-ScriptAnalyzer completes consistently and returns the expected findings across repeated runs.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.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.").
Environment
- PSScriptAnalyzer 1.25.0
- PowerShell 7.6.5 (pwsh)
- Windows 11 Enterprise (x64)
- Each run is a fresh
pwsh -NoProfile -NonInteractiveprocess. No profile, no other modules.
Minimal repro
-
Create a folder with one file,
Probe.psm1:function Get-Probe { 'probe' } Export-ModuleMember -Function Get-Probe -
Run the analyzer in a fresh process, several times:
$fixture = '<path to the folder>' $child = @" `$ErrorActionPreference = 'Stop' try { Import-Module PSScriptAnalyzer -Force `$r = @(Invoke-ScriptAnalyzer -Path '$fixture' -Recurse) "OK count=`$(`$r.Count)" } catch { "EXC: `$(`$_.Exception.GetType().FullName): `$(`$_.Exception.Message)"; exit 3 } "@ 1..15 | ForEach-Object { pwsh -NoProfile -NonInteractive -Command $child }
A 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.
Expected
Every run completes and returns the same findings (here: 1 finding).
Actual
Some runs fail. The whole Invoke-ScriptAnalyzer call ends with FullyQualifiedErrorId: RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand.
Stack, variant 1 (most frequent; the rule frame is ProvideCommentHelp or
AvoidReservedCharInCmdlet):
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Management.Automation.CommandInfo.ResolveParameter(String name)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetExportedFunction(Ast ast)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.AvoidReservedCharInCmdlet.AnalyzeScript(Ast ast, String fileName)+MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass84_1.<AnalyzeSyntaxTree>b__2()
Stack, variant 2:
System.InvalidOperationException: You cannot have more than one dynamic module in each dynamic assembly in this version of the runtime.
at System.Reflection.Emit.RuntimeAssemblyBuilder.DefineDynamicModuleCore(String name)
at System.Management.Automation.PSLocalEventManager.ProcessNewSubscriber(PSEventSubscriber subscriber, Object source, String eventName, String sourceIdentifier, PSObject data, Boolean supportEvent, Boolean forwardEvent)
at System.Management.Automation.PSLocalEventManager.SubscribeEvent(Object source, String eventName, String sourceIdentifier, PSObject data, PSEventReceivedEventHandler handlerDelegate, Boolean supportEvent, Boolean forwardEvent, Int32 maxTriggerCount)
at System.Management.Automation.PSLocalEventManager.SubscribeEvent(Object source, String eventName, String sourceIdentifier, PSObject data, PSEventReceivedEventHandler handlerDelegate, Boolean supportEvent, Boolean forwardEvent, Boolean shouldQueueAndProcessInExecutionThread, Int32 maxTriggerCount)
at System.Management.Automation.CommandInfo.GetMergedCommandParameterMetadataSafely()
at System.Management.Automation.CommandInfo.ResolveParameter(String name)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.Helper.GetExportedFunction(Ast ast)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules.ProvideCommentHelp.AnalyzeScript(Ast ast, String fileName)+MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass84_1.<AnalyzeSyntaxTree>b__2()
Frequency
| Target | Runs | Failed |
|---|---|---|
Minimal module above (Export-ModuleMember -Function) |
15 | 6 (4 NullReferenceException, 2 InvalidOperationException) |
| Minimal module plus a class file | 30 | 8 |
A real multi-file module (one Export-ModuleMember call), -Recurse |
25 | 4 |
The same module, signed or unsigned, fails at about the same rate.
Analysis
- The analyzer runs the rules for one file in parallel, one task per rule.
ProvideCommentHelp(PSProvideCommentHelp) andAvoidReservedCharInCmdlet(PSReservedCmdletChar) both callHelper.GetExportedFunction. In 1.25.0 no other caller exists.GetExportedFunctioncallsCommandInfo.ResolveParameteron the cachedExport-ModuleMemberCmdletInfo. That object belongs to a runspace of the analyzer's own pool. The call comes from the rule's thread pool thread.- 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).
A possible fix: resolve the Export-ModuleMember parameter metadata once, on the runspace thread, or serialize GetExportedFunction across rules.
Workaround
Run the two rules in their own passes, so that nothing else uses the analyzer's runspace at the same time:
$isolated = 'PSProvideCommentHelp', 'PSReservedCmdletChar'
$r = @(Invoke-ScriptAnalyzer -Path $path -Recurse -ExcludeRule $isolated)
foreach ($rule in $isolated) {
# Keep only that rule's findings: each pass reports the parse diagnostics again.
$r += @(Invoke-ScriptAnalyzer -Path $path -Recurse -IncludeRule $rule |
Where-Object RuleName -eq $rule)
}
With 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.
- Ngôn ngữ chính
- C#
- Star
- 2.2k
- Fork
- 419
- Merge trung bình
- 17 giờ 27 phút
- Pull request đã merge (30 ngày)
- 14
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của PowerShell/PSScriptAnalyzer
-
Up-for-Grabs
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 78/100
PowerShell/PSScriptAnalyzer#2213 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 32/100
PowerShell/PSScriptAnalyzer#2222 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 48/100
PowerShell/PSScriptAnalyzer#2220 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Resolution - Answered
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
PowerShell/PSScriptAnalyzer#2219 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 72/100
PowerShell/PSScriptAnalyzer#2218 · 1 reaction · 2 người được giao ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của PowerShell/PSScriptAnalyzer
Issue tương tự
-
area-ai bug untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
dotnet/extensions#7893 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
devtools documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
area-System.ComponentModel untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
dotnet/runtime#136608 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
fix: Cannot reopen dialog after closing with escape key in FluentDialogCó thể đã có người làm @bryan-ramirez đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
microsoft/fluentui-blazor#5572 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
sillsdev/languageforge-lexbox#2846 ·
Maintainer thường phản hồi trong vòng 1 ngày