PSUseOutputTypeCorrectly complains "... type is not declared in the OutputType attribute" even though it is
See original GitHub issueSteps to reproduce
PSScriptAnalyzer is complaining about a missing OutputType attribute, even though I did set it correctly. See PSScriptAnalyzer CI comment under my commits: https://github.com/jantari/LSUClient/commit/a6c2fcc1622aa221d0be73debb4a97baea7b36b5
Easiest way to reproduce is test with my exact file:
mkdir test-pssa
curl.exe "https://raw.githubusercontent.com/jantari/LSUClient/develop/public/Install-LSUpdate.ps1" -o .\test-pssa\Install-LSUpdate.ps1
Import-Module PSScriptAnalyzer -RequiredVersion 1.20.0 # Latest
Invoke-ScriptAnalyzer -Path .\test-pssa -ExcludeRule PSAvoidTrailingWhitespace -Recurse -Verbose
Expected behavior
PSScriptAnalyzer should have no complaints.
Actual behavior
RuleName Severity ScriptName Line Message
-------- -------- ---------- ---- -------
PSUseOutputTypeCorrectly Information Install-LS 93 The cmdlet 'Install-LSUpdate' returns an object of type
Update.ps1 'PackageInstallResult' but this type is not declared in the
OutputType attribute.
PSUseOutputTypeCorrectly Information Install-LS 125 The cmdlet 'Install-LSUpdate' returns an object of type
Update.ps1 'PackageInstallResult' but this type is not declared in the
OutputType attribute.
Environment data
Issue is reproducible under both:
> $PSVersionTable
Name Value
---- -----
PSVersion 7.2.1
PSEdition Core
GitCommitId 7.2.1
OS Microsoft Windows 10.0.22538
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.20.0
1.19.1
and:
> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.22538.1000
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.22538.1000
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.19.0
1.20.0
1.19.1
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
OutputType attribute warning in PowerShell with ...
The cmdlet 'Test-Function' returns an object of type 'System.Object[]' but this type is not declared in the OutputType attribute.
Read more >PSUseOutputTypeCorrectly/[OutputType()] fires ...
As a work around, I'm having to declare an incorrect return type, ie System.string in [OutputType()], and put in a comment to indicate...
Read more >about Functions OutputTypeAttribute - PowerShell
Describes an attribute that reports the type of object that the function returns.
Read more >Understanding the OutputType keyword in PowerShell
When used with a function, the OutputType keyword defines what type of object a function will return. If PowerShell knows this ahead of...
Read more >OutputType Attribute Declaration - PowerShell
The OutputType attribute identifies the .NET Framework types returned by a cmdlet, function, or script. Syntax. C# Copy.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Woah, that is silly. I had picked up the “specify the type name as a string” syntax somewhere a long time ago and this was the first time ever that PSSA complained about / didn’t accept it.
I’ve been doing this wrong the entire time (and many many more times in other projects / scripts …)
well damn … but thanks a lot for clearing that up. I can’t remember where I got this syntax from, but you’ve got me questioning everything now haha
SOLVED 🎉
Fair enough, it seems that if I added
[CmdletBinding()]
in my examples, it would’ve worked. However, what is happening here I think is that at the time of analysing it, it does not know about the type and cannot resolve it. If you imported the module that defined this type before runningInvoke-ScriptAnalyzer
you would not be getting this false positive, simple test by runningclass PackageInstallResult {}
to define this type proves that. Using brackets instead of single quotes make ScriptAnalyzer not return a false positive either so definitely a workaround as well for now. I will take a detailed look later whether this rule could be tweaked.