PowerShell / PowerShell/PSScriptAnalyzer

PSUseOutputTypeCorrectly is triggered when it shouldn't.

Aperta
#676 3 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Issue - Bug
Lingua principale
C#
Stelle
2.2k
Fork
414
Merge medio
13h 1m
PR unite (30g)
2

Descrizione

It looks to me like PSUseOutputTypeCorrectly check is not taking very basic, crucial, fundamental fact about PowerShell into account: by default it will not keep any simple collection, unless you explicitly tell it to.

Result? I have PInvoke code (taken from here) and I correctly defined OutputType as [Connection] (I've actually created namespace for it, but that's irrelevant).
As expected, Get-Member confirms that PowerShell function will return stream of connections, not collection as a whole. Yet, PSScriptAnalyzer will complain about it:

Line: 45 - The cmdlet 'Get-OPNetStat' returns an object of type 'Optiver.Connection[]' but this type is not declared in the OutputType attribute. 
Line: 54 - The cmdlet 'Get-OPNetStat' returns an object of type 'Optiver.Connection[]' but this type is not declared in the OutputType attribute. 

In fact, if I would follow instructions and declare my OutputType as a collection it would be conflicting with what my function actually returns:

Set-StrictMode -Version Latest
Get-OPNetStat | % IsFixedSize
% : The input name "IsFixedSize" cannot be resolved to a member.

IsFixedSize was tab-completed, based on OutputType that PSScriptAnalyzer told me to define. As expected, my Connection object doesn't have it. The only workaround I've found that satisfies both PSScriptAnalyzer and my hope to get nice tab-completion for command I wrote is to pass result of the command thru ForEach-Object:

[Optiver.NetworkUtil]::GetTCP() } | ForEach-Object { $_ }

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la regola PSUseOutputTypeCorrectly e riproduci l'esempio segnalato di Get-OPNetStat, inclusi l'output di [Optiver.NetworkUtil]::GetTCP() e il comportamento di Get-Member. Confronta il tipo di output dichiarato [Connection] con la diagnostica Connection[] dell'analizzatore; il lavoro è completato quando un output di stream valido non produce più questo falso avviso, mentre il completamento tramite tab del tipo di output rimane accurato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp, powershell
Ambito
tooling
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.