AvoidReservedCharInCmdlet intermittently throws NullReferenceException from CommandInfo.ResolveParameter
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 45/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 静か
- 技術スタック
- csharp, powershell
- 領域
- cli, testing-qa
調査の方向性
まず、提供された repro.ps1 ループを Invoke-ScriptAnalyzer で実行し、成功したセッションと失敗したセッションを比較します。AvoidReservedCharInCmdlet と Helper.GetExportedFunction から CommandInfo.ResolveParameter までの名前付きパスを、レポートに記載されたルールの並行呼び出しも含めて追跡します。分析が例外なく完了し、ファイルの診断結果を返せば完了です。
索引モデルが issue の本文から書いたものです。
説明
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
Invoke-ScriptAnalyzer intermittently fails with a NullReferenceException when analysing a file that both defines a function and calls Export-ModuleMember. The exception surfaces in AvoidReservedCharInCmdlet, by way of Helper.GetExportedFunction calling CommandInfo.ResolveParameter, and reproduces on the current release in 14/19 of 100 fresh sessions against a four-line file. Under -ErrorAction Stop it terminates the call; under the default preference it can discard the file's diagnostics and still exit zero.
Related: PowerShell/PSScriptAnalyzer#1538 (closed without a reproducer; identical stack), PowerShell/PSScriptAnalyzer#1708 (open; same CommandInfo root, different rule), PowerShell/PSScriptAnalyzer#1351 (open; transient RULE_ERROR, rule unidentified).
Steps to reproduce
- Requires the
PSScriptAnalyzer module ('Install-Module PSScriptAnalyzer'). - Analysing a file that both defines a function and calls
Export-ModuleMemberintermittently fails with aNullReferenceExceptionthrown fromSystem.Management.Automation.CommandInfo.ResolveParameter. - Neither element on its own reproduces it.
This writes repro.ps1 into the current directory and analyses it there.
# Four lines are enough; nothing else in the file matters.
@'
function Get-Thing {
return 1
}
Export-ModuleMember -Function Get-Thing
'@ | Set-Content ./repro.ps1
# One analysis per fresh session: the fault surfaces far more readily on the
# first invocation in a process than on later ones.
$threw = 0
1..100 | ForEach-Object {
$out = pwsh -NoProfile -Command {
Import-Module PSScriptAnalyzer -ErrorAction Stop
$ErrorActionPreference = 'Stop'
try { $null = @(Invoke-ScriptAnalyzer -Path ./repro.ps1); 'OK' } catch { 'THREW' }
}
if ($out -match 'THREW') { $threw++ }
}
"threw $threw / 100"
Observations
One hundred fresh sessions per row, on 1.25.0 except the last:
| Input or option | Threw |
|---|---|
| The four-line file above | 14 / 100; 19 / 100 |
Function only, no Export-ModuleMember |
0 / 100 |
Export-ModuleMember only, no function |
0 / 100 |
-ExcludeRule PSReservedCmdletChar |
0 / 100 |
-ExcludeRule PSProvideCommentHelp |
0 / 100 |
-Severity Warning |
0 / 100 |
| The same file on 1.24.0 | 11 / 100; 13 / 100 |
- The extension is irrelevant:
.ps1and.psm1reproduce alike, in any directory.Get-Thingcontains no reserved character, so the rule reports nothing on this file even on a successful run. - Under
$ErrorActionPreference = 'Stop'the exception terminates the call, rather than being reported as a non-terminating error. - Under the default preference the run continues and the diagnostic can be lost silently.
- An error-free run returns one finding,
PSProvideCommentHelp. - The rate is not stable between measurement runs on identical input, which seems consistent with a race condition.
- The stack names
AvoidReservedCharInCmdlet, yet excludingPSProvideCommentHelpalso stops it. Both rules reachHelper.GetExportedFunction, so a second concurrent caller appears to be required rather than the throwing rule alone. -Severity Warningmasks it because that filter suppresses rule selection rather than filtering diagnostics, soPSProvideCommentHelpnever runs. See PowerShell/PSScriptAnalyzer#2156.
Expected behavior
Analysis completes and returns the file's diagnostics. No error is written.
Actual behavior
On a failing run, `Invoke-ScriptAnalyzer` writes:
Invoke-ScriptAnalyzer: Object reference not set to an instance of an object.
- Under `$ErrorActionPreference = 'Stop'` the exception terminates the call.
- Under the default preference the run continues and the diagnostic can be lost silently: an error-free run returns one finding, `PSProvideCommentHelp`.
- The rate is not stable across measurement runs on identical input, consistent with a
race condition.
- Repeated 100-session runs gave 14 and 19 on PSScriptAnalyzer 1.25.0, with 11 and 13 on
1.24.0.
- Excluding either `PSReservedCmdletChar` or `PSProvideCommentHelp` gives 0/100;
both rules reach `Helper.GetExportedFunction`, so a second concurrent caller appears to
be required rather than the throwing rule alone.
Error details
**Error details** — `Get-Error`, verbatim, paths sanitised:
Exception :
Type : System.NullReferenceException
TargetSite :
Name : ResolveParameter
DeclaringType : [System.Management.Automation.CommandInfo]
MemberType : Method
Module : System.Management.Automation.dll
Message : Object reference not set to an instance of an object.
Source : System.Management.Automation
HResult : -2147467261
StackTrace :
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.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Microsoft.Windows.PowerShell.ScriptAnalyzer.ScriptAnalyzer.<>c__DisplayClass84_1.<AnalyzeSyntaxTree>b__2()
TargetObject : /path/to/repro.ps1
CategoryInfo : InvalidOperation: (/path/to/repro.ps1:String) [Invoke-ScriptAnalyzer], NullReferenceException
FullyQualifiedErrorId : RULE_ERROR,Microsoft.Windows.PowerShell.ScriptAnalyzer.Commands.InvokeScriptAnalyzerCommand
InvocationInfo :
MyCommand : Invoke-ScriptAnalyzer
ScriptLineNumber : 3
OffsetInLine : 15
InvocationName : Invoke-ScriptAnalyzer
CommandOrigin : Internal
ScriptStackTrace : at <ScriptBlock>, <No file>: line 3
Environment data
> $PSVersionTable
Name Value
---- -----
PSVersion 7.6.4
PSEdition Core
GitCommitId 7.6.4
OS Debian GNU/Linux 13 (trixie)
Platform Unix
- `PSScriptAnalyzer` 1.24.0 installed
- 1.25.0 (current PSGallery release) tested via `Save-Module` and an explicit manifest import.
- Both versions affected.
- Not tested on Windows or macOS.
Visuals
No response
- 主要言語
- C#
- スター
- 55.5k
- フォーク
- 8.5k
- 平均マージ
- 1日 17時間
- マージ済み PR(30日)
- 97
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
PowerShell/PowerShell のほかの issue
-
Issue-Enhancement Needs-Triage
難易度 1/5 1時間未満 初心者へのやさしさ 86/100
PowerShell/PowerShell#28061 · リアクション 2 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
PowerShell/PowerShell#28037 ·
-
Needs-Triage
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
PowerShell/PowerShell#27973 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
PowerShell/PowerShell#27720 ·
-
Issue-Bug Up-for-Grabs WG-Cmdlets WG-Reviewed
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
PowerShell/PowerShell#27670 · コメント 3 件 ·
PowerShell/PowerShell の issue をすべて見る
似ている issue
-
:watch: Not Triaged dotnet-fsharp/svc
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
-
Client customer-reported needs-team-attention question Service Attention WebPubSub
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
Azure/azure-sdk-for-net#63292 · コメント 3 件 · リアクション 1 件 ·
-
dependencies needs-team-triage server-Azure.Mcp
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
-
aspnet-core/svc aspnetcore-signalr/subsvc doc-enhancement Pri2 SignalR
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
dotnet/AspNetCore.Docs#37729 ·
-
Status: Waiting triage Type: Bug
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
nanoframework/Home#1857 ·