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

Invoke-ScriptAnalyzer: An item with the same key has already been added. What is the proper syntax? #1511

Closed
RylandDeGregory opened this issue May 22, 2020 · 13 comments

Comments

@RylandDeGregory
Copy link

RylandDeGregory commented May 22, 2020

I seem to be receiving a rule-less error whenever running PSScriptAnalyzer with default settings against a PowerShell script containing the following code block.

Environment

MacOS 10.15.4
PowerShell 7 LTS for MacOS
PSScriptAnalyzer 1.19.0

Code

$result = Invoke-Command -ComputerName $Computername -Credential $RemotingCred -Authentication Negotiate -ScriptBlock {
    $output=@()
    foreach ($item in $list) {
        $out = [PSCustomObject]@{
                    Property1   = $item.Property1
                    Property2   = $item.Property2
               }
        $output += $out
    }
    return $output
}

Command

Invoke-ScriptAnalyzer -path ./test.ps1

Expected Output

none

Actual Output

Invoke-ScriptAnalyzer: An item with the same key has already been added. Key: output

Screen Shot 2020-05-22 at 4 33 03 PM

Question

What is the proper PowerShell syntax to prevent this error from generating? Or, which Rule can I ignore to suppress it? The code executes fine, just for the record :)

Thanks!

@ghost ghost added the Needs: Triage 🔍 label May 22, 2020
@bergmeister
Copy link
Collaborator

bergmeister commented May 22, 2020

Can repro with PSSA 1.19.0 on PS 7 on Windows. Also happens with the current version of PSSA on master.

@rjmholt
Copy link
Contributor

rjmholt commented May 22, 2020

Would you be able to collect the full error print out with Get-Error and posting it here -- in particular the stack trace is important

@bergmeister
Copy link
Collaborator

bergmeister commented May 22, 2020

I've got it in the debugger already, it's happening here:

variableDictionary.Add(variableName);

Fix should be trivial, .Net 4.5.2 doesn't contain the .TryAdd method unfortuantely.

if (!variableDictionary.ContainsKey(variableName))
{
    variableDictionary.Add(variableName, variable);
}

I've opened a draft PR for it already, just need to write a regression test.

@bergmeister
Copy link
Collaborator

bergmeister commented May 22, 2020

Actually, my fork didn't have the last 3 commits, this issue was already fixed in PR #1493 and is effectively a duplicate of #1492

@jantari
Copy link

jantari commented May 28, 2020

Hello.

This critical bug is causing all my GitHub Actions CI that use PSScriptAnalyzer to hang indefinitely.
See screenshot:
image

And then it never does anything - doesn't quit either. I've had these running for over 45 minutes.

Using the ubuntu-latest runner with fresh PSSA installs from PSGallery every time.

Since this is such a severe bug, when can we expect a point release with the fix?

Thanks

@RylandDeGregory
Copy link
Author

Agreed.

@bergmeister
Copy link
Collaborator

@jantari A hang is unexpected and might be a different issue, can you provide more details please?
For the moment, we suggest to pin the version of PSScriptAnalyzer using the -MaximumVersion 1.18.3 parameter on Install-Module?
What do you think about a 1.19.1 release @JamesWTruher @rjmholt ? If, so then I'd like to have PR #1497 and optionally #1491 considered as well.

@DigitalNightmare
Copy link

I am also seeing this issue, it seems to only affect scripts with hash tables that have duplicates in them. I've pinned to version 1.18.3 and get the error, which breaks my pester tests. like above. I also have tried pinning to 1.91.1 this throws the error still but the pester step in my build finishes without error, sans any testing it would otherwise do....

@rjmholt
Copy link
Contributor

rjmholt commented Jun 29, 2020

I'm in favour of a 1.19.1 release

@thomasrayner
Copy link
Contributor

Hey, @rjmholt, is there a schedule for a 1.19.1 release, by any chance?

@rjmholt
Copy link
Contributor

rjmholt commented Jul 16, 2020

Best to ask @JamesWTruher and @SydneyhSmith

@SydneyhSmith
Copy link
Collaborator

@thomasrayner currently we are targeting a 1.19.1 release in about two weeks (likely the last week in July) however this is truly not a promise/commitment--just a good faith estimate....we are hoping to get another bug fix into this release, and then prioritize the release amongst other commitments

@jantari
Copy link

jantari commented Oct 23, 2020

I wanted to say that during some general CI maintenance I switched to using 1.19.1/latest again and my deadlock is not occuring anymore, so appears fixed.

As far as I'm concerned, this can be closed.

EDIT: nvm it was closed all along

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants