Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Cannot find an overload for ".ctor" and the argument count: "1"" error in Invoke-Analyzer (OSOE-560) #28

Closed
Piedone opened this issue Feb 3, 2023 · 25 comments · Fixed by #34 or #35
Assignees
Labels
bug Something isn't working

Comments

@Piedone
Copy link
Member

Piedone commented Feb 3, 2023

  1. Rebuild the OSOCE solution.
  2. Notice this error in the built output coming from Invoke-Analyzer:
65>�[31;1mInvoke-ScriptAnalyzer: �[0mE:\ShortPath\OSOCE1\tools\Lombiq.Analyzers.PowerShell\Lombiq.Analyzers.PowerShell\Invoke-Analyzer.ps1:119�[0m
65>�[31;1m�[0m�[36;1mLine |�[0m
65>�[31;1m�[0m�[36;1m�[36;1m 119 | �[0m . ch-Object { �[36;1mInvoke-ScriptAnalyzer -Path $PSItem.FullName @analyzerPar�[0m .�[0m
65>�[31;1m�[0m�[36;1m�[36;1m�[0m�[36;1m�[0m�[36;1m     | �[31;1m               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~�[0m
65>�[31;1m�[0m�[36;1m�[36;1m�[0m�[36;1m�[0m�[36;1m�[31;1m�[31;1m�[36;1m     | �[31;1mCannot find an overload for ".ctor" and the argument count: "1".�[0m

This is the same error as the one fixed in #24, but the offending line has nothing to do with SuppressMessage:

https://github.com/Lombiq/PowerShell-Analyzers/blob/dev/Lombiq.Analyzers.PowerShell/Invoke-Analyzer.ps1#L119

Jira issue

@Piedone Piedone added the bug Something isn't working label Feb 3, 2023
@github-actions github-actions bot changed the title "Cannot find an overload for ".ctor" and the argument count: "1"" error in Invoke-Analyzer "Cannot find an overload for ".ctor" and the argument count: "1"" error in Invoke-Analyzer (OSOE-560) Feb 3, 2023
@sarahelsaig
Copy link
Member

It's related to PowerShell/PSScriptAnalyzer#1881 and should've been fixed as of 21203aa.
Yet I have the latest PSScriptAnalyzer version (1.21) and see this error too.

@Piedone
Copy link
Member Author

Piedone commented Feb 3, 2023

I see, thanks! @BenedekFarkas could you please chime in too?

@BenedekFarkas
Copy link
Member

I don't have a repro on the latest dev branch, even with a purged repo. Running the analysis manually in pwsh is also error-free.
BTW PS analysis runs in the workflow only (not in the build anymore) since Lombiq/Open-Source-Orchard-Core-Extensions#390.

The base library was updated to 1.21 in #20 - do you have an earlier version installed too? If so, please try removing it (check both powershell and pwsh).

@Piedone
Copy link
Member Author

Piedone commented Feb 8, 2023

I get the same with the latest OSOCE dev fa00032518d5de90af474e21c4d6f9cbb2c35903 when running it from a PS console:

image

.\tools\Lombiq.Analyzers.PowerShell\Lombiq.Analyzers.PowerShell\Invoke-Analyzer.ps1

I have PSScriptAnalyzer v1.21.0 installed, i.e. the latest.

@BenedekFarkas
Copy link
Member

So, you don't have earlier versions installed anymore? From either Windows PS or PS Core?

@BenedekFarkas
Copy link
Member

Oh, wait. What you linked above (#24) is not actually a bugfix, just a cosmetic change. There was a bugfix related to suppress messages, but in Utility Scripts: Lombiq/Utility-Scripts@96027b5

So, make sure that you use the latest dev of Infrastructure/Utility Scripts.

If that's still not working, then you can modify Invoke-Analyzer.ps1 to print out the name of the file it's checking before calling Invoke-ScriptAnalyzer on it, so you'll see which file causes the problem.

@Piedone
Copy link
Member Author

Piedone commented Feb 8, 2023

I had 1.20.0 installed in Windows PowerShell, but I don't see how that would make a difference. In any case, uninstalling it and running the command in Pwsh 7 as I did above still yields the same result.

I don't understand why Utility Scripts affects this, but pulling in its latest does change the error to:

image

Changing

$results = Find-Recursively -IncludeFile '*.ps1', '*.psm1', '*.psd1' -ExcludeDirectory node_modules |
    Where-Object { # Exclude /TestSolutions/Violate-Analyzers.ps1 and /TestSolutions/*/Violate-Analyzers.ps1
        $IncludeTestSolutions -or -not (
            $PSItem.Name -eq 'Violate-Analyzers.ps1' -and
            ($PSItem.Directory.Name -eq 'TestSolutions' -or $PSItem.Directory.Parent.Name -eq 'TestSolutions')) } |
    ForEach-Object { Invoke-ScriptAnalyzer -Path $PSItem.FullName @analyzerParameters }

foreach ($result in $results)
{
    $message = $result.RuleName + ': ' + $result.Message
    Write-FileError -Path $result.ScriptPath -Line $result.Line -Column $result.Column $message
}

to

$results = Find-Recursively -IncludeFile '*.ps1', '*.psm1', '*.psd1' -ExcludeDirectory node_modules |
    Where-Object { # Exclude /TestSolutions/Violate-Analyzers.ps1 and /TestSolutions/*/Violate-Analyzers.ps1
        $IncludeTestSolutions -or -not (
            $PSItem.Name -eq 'Violate-Analyzers.ps1' -and
            ($PSItem.Directory.Name -eq 'TestSolutions' -or $PSItem.Directory.Parent.Name -eq 'TestSolutions')) }

foreach ($result in $results)
{
    Write-Output $result.FullName
    Invoke-ScriptAnalyzer -Path $result.FullName @analyzerParameters
}

as a test yields this:

image

But! If I then change the script back to the original version, it runs to completion without errors (or without any output, really). Cleaning the repo doesn't change this either. WTF?

@BenedekFarkas
Copy link
Member

BenedekFarkas commented Feb 8, 2023

I had 1.20.0 installed in Windows PowerShell, but I don't see how that would make a difference. In any case, uninstalling it and running the command in Pwsh 7 as I did above still yields the same result.

To eliminate the differences between our environments and I've seen inconsistent behavior with multiple versions installed (especially with the two different PS versions). You don't need the older version installed anyway.

I don't understand why Utility Scripts affects this, but pulling in its latest does change the error to:

Utility Scripts is related due to the issue Dávid linked above that I opened: PowerShell/PSScriptAnalyzer#1881

The symptom there was that we were analyzing scripts in OSOCE and one of them calls Update-VisualStudioSolutionNuGetPackages (which is in Utility Scripts) and the same error was thrown (due to the SuppressMessage being incorrectly parameterized in Update-VisualStudioSolutionNuGetPackages - see what I linked above), even though that script is not actually involved in the analysis.

But! If I then change the script back to the original version, it runs to completion without errors (or without any output, really). Cleaning the repo doesn't change this either. WTF?

Now that sort of inconsistency is not something I've seen. Can you please try changing the evaulation of $results in the last line like this instead?

ForEach-Object { Write-Output $PSItem.FullName; Invoke-ScriptAnalyzer -Path $PSItem.FullName @analyzerParameters }

@Piedone
Copy link
Member Author

Piedone commented Feb 8, 2023

Thanks for explaining.

Changing the code yields something similar to above, but without any file being listed before the error:

image

This is actually repeatable, happens again and again.

@BenedekFarkas
Copy link
Member

BenedekFarkas commented Feb 16, 2023

I still don't have a local repro, but I've seen this pop up in GHA for Utility Scripts and Infrastructure Scripts. Re-running the failed workflow "fixed" it though, so it's hard to infer anything from it...

@Piedone
Copy link
Member Author

Piedone commented Oct 12, 2023

The NRE I mentioned above happens semi-regularly in OSOCE. Most recent is here.

As a band-aid solution, we could have a catch around the invoke for this exception and retry two more times if it happens. Because I've only ever seen this being solved by a rerun.

@tteguayco tteguayco self-assigned this Oct 20, 2023
@tteguayco
Copy link
Contributor

tteguayco commented Oct 22, 2023

After some time since the issue was reported, are you still able to reproduce it, @Piedone?

I've been trying to do so but without success. The Invoke-Analyzer.ps1 script seems to be running fine for me, returning an empty output.

image

Some details of my environment:

  • PSScriptAnalyzer version: 1.21.0
  • PowerShell version: 7.3.8
  • Ran from commits:
    • fa00032518d5de90af474e21c4d6f9cbb2c35903 (referenced above)
    • 824136915ef42b3947fcb717694d2640e8558635 (one of the latest)

I suspect that the set of files being analyzed may differ, being one of them a possible trigger of the issue. If I run the following snippet from the original script:

Find-Recursively -IncludeFile '*.ps1', '*.psm1', '*.psd1' -ExcludeDirectory node_modules |
    Where-Object { # Exclude /TestSolutions/Violate-Analyzers.ps1 and /TestSolutions/*/Violate-Analyzers.ps1
        $IncludeTestSolutions -or -not (
            $PSItem.Name -eq 'Violate-Analyzers.ps1' -and
            ($PSItem.Directory.Name -eq 'TestSolutions' -or $PSItem.Directory.Parent.Name -eq 'TestSolutions')) }

I get this list of files subject to analysis:

PS C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions> Find-Recursively -IncludeFile '*.ps1', '*.psm1', '*.psd1' -ExcludeDirectory node_modules |
>>     Where-Object { # Exclude /TestSolutions/Violate-Analyzers.ps1 and /TestSolutions/*/Violate-Analyzers.ps1
>>         $IncludeTestSolutions -or -not (
>>             $PSItem.Name -eq 'Violate-Analyzers.ps1' -and
>>             ($PSItem.Directory.Name -eq 'TestSolutions' -or $PSItem.Directory.Parent.Name -eq 'TestSolutions')) }

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\NuGetTest

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:37            126 Reset-Local.ps1
-a---          20/10/2023    11:37            172 Update-LombiqNuGetPackages.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.Analyzers

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55           1335 ConvertTo-Nuspec.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.Analyzers.PowerShell\Lombiq.Analyzers.PowerShell\Rules\Measure-AutomaticVariableAlias

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55           2961 Measure-AutomaticVariableAlias.psm1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.Analyzers.PowerShell\Lombiq.Analyzers.PowerShell\Rules\Measure-LineContinuation

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55           2293 Measure-LineContinuation.psm1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.Analyzers.PowerShell\Lombiq.Analyzers.PowerShell

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          22/10/2023    07:55           5329 Invoke-Analyzer.ps1
-a---          22/10/2023    07:56           1790 PSScriptAnalyzerSettings.psd1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\.github\actions\add-azure-application-insights-release-annotation

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55           1047 Add-ReleaseAnnotation.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\.github\actions\auto-resolve-done-jira-issue

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55            838 Initialize-IssueParameters.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\.github\actions\build-dotnet

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55           4632 Build-DotNetSolutionOrProject.ps1
-a---          20/10/2023    11:55            569 Test-Build.ps1
-a---          20/10/2023    11:55            867 Write-CacheConfiguration.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\.github\actions\check-pull-request-labels

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55            921 Test-PullRequestLabels.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\.github\actions\check-pull-request-reviews

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55            490 Test-PullRequestReviews.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\.github\actions\create-jira-issues-for-community-activities

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55           2131 Initialize-IssueDetails.ps1
-a---          20/10/2023    11:55           2218 Initialize-IssueTemplates.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\.github\actions\get-changed-gha-items

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55           1006 Get-GHA-Items-From-File-List.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\.github\actions\msbuild

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55           3039 Build-DotNetFrameworkSolutionOrProject.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\.github\actions\publish-nuget

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55           2306 Add-SourceLinkPackage.ps1
-a---          20/10/2023    11:55           6440 New-NuGetPackage.ps1
-a---          20/10/2023    11:55            601 Update-ManifestVersion.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\.github\actions\setup-sql-server

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55            547 Initialize-SqlServer.ps1
-a---          20/10/2023    11:55            713 Wait-SqlServer.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\.github\actions\spelling

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55           1050 Merge-SpellCheckingDictionaryFile.ps1
-a---          20/10/2023    11:55           1001 Optimize-SpellCheckingDictionaryFile.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\.github\actions\test-dotnet

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55           4540 Invoke-SolutionTests.ps1
-a---          20/10/2023    11:55           2217 Merge-BlameHangDumps.ps1
-a---          20/10/2023    11:55            433 Merge-FailureDumps.ps1
-a---          20/10/2023    11:55           1174 Set-XUnitMaxParallelThreads.ps1
-a---          20/10/2023    11:55            360 Test-SolutionTests.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\.github\actions\update-github-issue-and-pull-request

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55           1956 Update-GitHubIssueAndPullRequest.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\.github\actions\verify-gha-refs

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55           2454 Check-Called-GHA-Refs.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\.github\actions\verify-submodule-pull-request

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55            614 Check-Parent.ps1
-a---          20/10/2023    11:55            289 functions.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions\tools\Lombiq.GitHub.Actions\Scripts

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          20/10/2023    11:55            162 Confirm-PullRequestTitle.ps1
-a---          20/10/2023    11:55            179 ConvertFrom-MultiLineStringToStringArray.ps1
-a---          20/10/2023    11:55            160 Get-JiraIssueKeyFromPullRequestTitle.ps1
-a---          20/10/2023    11:55            972 Get-SolutionOrProjectPath.ps1
-a---          20/10/2023    11:55            567 Initialize-ArtifactNameSuffix.ps1
-a---          20/10/2023    11:55           1287 Install-DotNetTool.ps1
-a---          20/10/2023    11:55            270 Set-GitHubOutput.ps1
-a---          20/10/2023    11:55            203 Set-GitHubState.ps1
-a---          20/10/2023    11:55            362 Stop-DotNetBuildServers.ps1

    Directory: C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          22/10/2023    07:50            104 Reset-Local.ps1

PS C:\Repos\Lombiq\Open-Source-Orchard-Core-Extensions>

Can you spot any difference, @Piedone?

PD: Invoke-Analyzer.ps1 seems to be analyzing itself. Probably the changes introduced here are something that doesn't adhere to the defined set of rules, being the cause of the errors displayed in the screenshot.

EDIT

At least file Add-JiraIssueCodeToPullRequest.ps1 is one that I don't have comparing with screenshot here. I guess that output is not showing all the files subject to analysis.

@Piedone
Copy link
Member Author

Piedone commented Oct 22, 2023

Yep, it happens randomly in OSOCE GHA builds. Seen it like two days ago too.

@DemeSzabolcs
Copy link
Member

For one try (building locally), I couldn't reproduce it. But I guess this is because it happens randomly as Zoltán said.

@Piedone
Copy link
Member Author

Piedone commented Oct 23, 2023

Here's the recent example I mentioned: https://github.com/Lombiq/Open-Source-Orchard-Core-Extensions/actions/runs/6589654527/job/17904585730#step:12:20

@tteguayco
Copy link
Contributor

tteguayco commented Oct 24, 2023

Here's the recent example I mentioned: https://github.com/Lombiq/Open-Source-Orchard-Core-Extensions/actions/runs/6589654527/job/17904585730#step:12:20

Nice, thank you @Piedone.

Regarding running it locally, is it still reproducible for you like it was here? If so, does it happen every time you run the script or also "randomly" like in GHA?

Would like to involve @DAud-IcI as well, since he's the other person experiencing this locally back then.

@Piedone
Copy link
Member Author

Piedone commented Oct 24, 2023

I never tried to reproduce this specifically locally but it did happen randomly before, see my earlier comments. However, I don't think it's much use trying to do that: this is a PSScriptAnalyzer bug (see here: PowerShell/PSScriptAnalyzer#1867), that only bothers our CI builds, and does this in a random manner. I think the only thing that needs to be done here is what I've written under #28 (comment). Alternatively, we can just try the Verbose switch as suggested under the issue, if it doesn't produce too much useless output.

@sarahelsaig
Copy link
Member

sarahelsaig commented Oct 25, 2023

Sorry, to be honest I don't remember anything about this. At the moment I can't reproduce it either. I'll remember to write more context in the future.
But I see I mentioned the PSScriptAnalyzer version in my previous comment. So if you are adding more diagnostic output then calling Get-Module could help to verify that indeed the correct version is initialized.

@Piedone Piedone reopened this Nov 8, 2023
@Piedone
Copy link
Member Author

Piedone commented Nov 8, 2023

@tteguayco
Copy link
Contributor

Hmm. Despiste having the retries working when running the script locally, it seems the execution stops on GHA when encountering the first Write-Error statement.

May GHA be configured somewhere to immediately stop when a Write-Error statement is reached? If it ends up being an issue, we can switch to another type of command to write those messages on a retry.

@BenedekFarkas
Copy link
Member

Hmm, that could be down to ErrorActionPreference.

@tteguayco
Copy link
Contributor

Before changing any configuration related to ErrorActionPreference in any GHA, would it make more sense to replace these two references to Write-Error here and here to something less severe, like Write-Warning?

And then see if any error comes up again after a couple of days when running from GHA.

@BenedekFarkas
Copy link
Member

BenedekFarkas commented Nov 14, 2023

Now that you mention it, it makes more sense to me anyway to use Write-Warning for retries and Write-Error only if we run out of retry attempts, even though the default ErrorActionPreference is Continue.

@DemeSzabolcs
Copy link
Member

Looks good, I'm merging the things, we will wait and see if any error comes up again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
5 participants