SeeminglyScience / EditorServicesCommandSuite

Collection of editor commands for use in PowerShell Editor Services.
Other
152 stars 13 forks source link

Exception calling "ProcessRecord" with "1" argument(s): "An item with the same key has already been added. Key: Join-String" #62

Closed ninmonkey closed 3 years ago

ninmonkey commented 4 years ago

Description

Some calls to Join-String throws a key error. Sometimes the exact same command that works, then breaks, relative the comment names on the line before.

Script to reproduce

# error
$files | Join-String -sep "`n- " -Property Name -OutputPrefix "- "
Join-String -sep "`n- " -Property Name -OutputPrefix "- "
Join-String -OutputPrefix '-'

# will splat, drops -outputprefix, but that's missing a value so that seems ok
# maybe that's helpful?
$files | Join-String -sep "`n" -prop Name -OutputPrefix

# will splat
$files = Get-ChildItem .
$files | Join-String "`n- " -Property Name -OutputPrefix "- "
$files | Join-String -sep ''
Join-String -sep "`n"
$files | Join-String -sep "`n"
$files | Join-String -sep "`n" -prop Name
0 | Join-String -sep ', ' -prop maxValue
0..2 | Join-String -sep "`n" -prop { $_::maxvalue }

## this works as expected
0..2 | Join-String -sep "`n" -prop { $_::maxvalue }

$joinStringSplat = @{
    Property  = { $_::maxvalue }
    Separator = "`n"
}

0..2 | Join-String -sep 'bar'

Previous errors.log

Logs

$error[0]

System.Management.Automation.MethodInvocationException: Exception calling "ProcessRecord" with "1" argument(s): "An item with the same key has already been added. Key: Join-String"
 ---> System.ArgumentException: An item with the same key has already been added. Key: Join-String
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at System.Management.Automation.Language.StaticBindingResult.CreateBindingResultForSuccessfulBind(CommandAst commandAst, PseudoBindingInfo bindingInfo)
   at System.Management.Automation.Language.StaticBindingResult..ctor(CommandAst commandAst, PseudoBindingInfo bindingInfo)
   at System.Management.Automation.Language.StaticParameterBinder.BindCommand(CommandAst commandAst, Boolean resolve, String[] desiredParameters)
   at System.Management.Automation.Language.StaticParameterBinder.BindCommand(CommandAst commandAst, Boolean resolve)
   at EditorServicesCommandSuite.CodeGeneration.Refactors.CommandSplatRefactor.<>c__DisplayClass28_0.<SplatCommandAsync>b__0() in D:\a\1\s\src\EditorServicesCommandSuite\CodeGeneration\Refactors\CommandSplatRefactor.cs:line 122
   at EditorServicesCommandSuite.Utility.ThreadController.ExecutionRequest`1.ExecuteImpl() in D:\a\1\s\src\EditorServicesCommandSuite\Utility\ThreadController.cs:line 499
   at EditorServicesCommandSuite.Utility.ThreadController.ThreadExecutionRequestBase`1.EditorServicesCommandSuite.Utility.ThreadController.IThreadExecutionRequest.Execute() in D:\a\1\s\src\EditorServicesCommandSuite\Utility\ThreadController.cs:line 390
--- End of stack trace from previous location where exception was thrown ---
   at EditorServicesCommandSuite.Utility.ThreadController.InvokeAsync[TResult](Func`1 func, CancellationToken cancellationToken) in D:\a\1\s\src\EditorServicesCommandSuite\Utility\ThreadController.cs:line 280
   at EditorServicesCommandSuite.CodeGeneration.Refactors.CommandSplatRefactor.SplatCommandAsync(DocumentContextBase context, CommandAst command, AdditionalParameterTypes parameterTypes, IRefactorUI ui) in D:\a\1\s\src\EditorServicesCommandSuite\CodeGeneration\Refactors\CommandSplatRefactor.cs:line 121
   at EditorServicesCommandSuite.CodeGeneration.Refactors.RefactorProvider.ProcessActionForInvoke(DocumentContextBase context, CodeAction action) in D:\a\1\s\src\EditorServicesCommandSuite\CodeGeneration\Refactors\RefactorProvider.cs:line 65
   at EditorServicesCommandSuite.CodeGeneration.Refactors.CommandSplatRefactor.Invoke(DocumentContextBase context) in D:\a\1\s\src\EditorServicesCommandSuite\CodeGeneration\Refactors\CommandSplatRefactor.cs:line 75
   at EditorServicesCommandSuite.Internal.RefactorCmdletAdapter.<>c__DisplayClass12_0.<<InvokeRefactor>b__0>d.MoveNext() in D:\a\1\s\src\EditorServicesCommandSuite\Internal\RefactorCmdletAdapter.cs:line 146
--- End of stack trace from previous location where exception was thrown ---
   at EditorServicesCommandSuite.Utility.ThreadController.GiveControl(Task requestTask, CancellationToken cancellationToken) in D:\a\1\s\src\EditorServicesCommandSuite\Utility\ThreadController.cs:line 58
   at EditorServicesCommandSuite.Internal.RefactorCmdletAdapter.InvokeRefactor(String className, PSCmdlet psCmdlet) in D:\a\1\s\src\EditorServicesCommandSuite\Internal\RefactorCmdletAdapter.cs:line 150
   at EditorServicesCommandSuite.Internal.RefactorCmdletAdapter.ProcessRecord(MethodInvocationInfo methodInvocationInfo) in D:\a\1\s\src\EditorServicesCommandSuite\Internal\RefactorCmdletAdapter.cs:line 77
   at CallSite.Target(Closure , CallSite , Object , Object )
   --- End of inner exception stack trace ---
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke[T](IEnumerable input, PSInvocationSettings settings)
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.PowerShell.EditorServices.Services.PowerShellContextService.ExecuteCommandAsync[TResult](PSCommand psCommand, StringBuilder errorMessages, ExecutionOptions executionOptions) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShellContext\PowerShellContextService.cs:line 927
   at Microsoft.PowerShell.EditorServices.Services.PowerShellContextService.ExecuteCommandAsync[TResult](PSCommand psCommand, StringBuilder errorMessages, ExecutionOptions executionOptions) in D:\a\1\s\src\PowerShellEditorServices\Services\PowerShellContext\PowerShellContextService.cs:line 927

$error[0].InnerException

System.ArgumentException: An item with the same key has already been added. Key: Join-String
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
   at System.Management.Automation.Language.StaticBindingResult.CreateBindingResultForSuccessfulBind(CommandAst commandAst, PseudoBindingInfo bindingInfo)
   at System.Management.Automation.Language.StaticBindingResult..ctor(CommandAst commandAst, PseudoBindingInfo bindingInfo)
   at System.Management.Automation.Language.StaticParameterBinder.BindCommand(CommandAst commandAst, Boolean resolve, String[] desiredParameters)
   at System.Management.Automation.Language.StaticParameterBinder.BindCommand(CommandAst commandAst, Boolean resolve)
   at EditorServicesCommandSuite.CodeGeneration.Refactors.CommandSplatRefactor.<>c__DisplayClass28_0.<SplatCommandAsync>b__0() in D:\a\1\s\src\EditorServicesCommandSuite\CodeGeneration\Refactors\CommandSplatRefactor.cs:line 122
   at EditorServicesCommandSuite.Utility.ThreadController.ExecutionRequest`1.ExecuteImpl() in D:\a\1\s\src\EditorServicesCommandSuite\Utility\ThreadController.cs:line 499
   at EditorServicesCommandSuite.Utility.ThreadController.ThreadExecutionRequestBase`1.EditorServicesCommandSuite.Utility.ThreadController.IThreadExecutionRequest.Execute() in D:\a\1\s\src\EditorServicesCommandSuite\Utility\ThreadController.cs:line 390
--- End of stack trace from previous location where exception was thrown ---
   at EditorServicesCommandSuite.Utility.ThreadController.InvokeAsync[TResult](Func`1 func, CancellationToken cancellationToken) in D:\a\1\s\src\EditorServicesCommandSuite\Utility\ThreadController.cs:line 280
   at EditorServicesCommandSuite.CodeGeneration.Refactors.CommandSplatRefactor.SplatCommandAsync(DocumentContextBase context, CommandAst command, AdditionalParameterTypes parameterTypes, IRefactorUI ui) in D:\a\1\s\src\EditorServicesCommandSuite\CodeGeneration\Refactors\CommandSplatRefactor.cs:line 121
   at EditorServicesCommandSuite.CodeGeneration.Refactors.RefactorProvider.ProcessActionForInvoke(DocumentContextBase context, CodeAction action) in D:\a\1\s\src\EditorServicesCommandSuite\CodeGeneration\Refactors\RefactorProvider.cs:line 65
   at EditorServicesCommandSuite.CodeGeneration.Refactors.CommandSplatRefactor.Invoke(DocumentContextBase context) in D:\a\1\s\src\EditorServicesCommandSuite\CodeGeneration\Refactors\CommandSplatRefactor.cs:line 75
   at EditorServicesCommandSuite.Internal.RefactorCmdletAdapter.<>c__DisplayClass12_0.<<InvokeRefactor>b__0>d.MoveNext() in D:\a\1\s\src\EditorServicesCommandSuite\Internal\RefactorCmdletAdapter.cs:line 146
--- End of stack trace from previous location where exception was thrown ---
   at EditorServicesCommandSuite.Utility.ThreadController.GiveControl(Task requestTask, CancellationToken cancellationToken) in D:\a\1\s\src\EditorServicesCommandSuite\Utility\ThreadController.cs:line 58
   at EditorServicesCommandSuite.Internal.RefactorCmdletAdapter.InvokeRefactor(String className, PSCmdlet psCmdlet) in D:\a\1\s\src\EditorServicesCommandSuite\Internal\RefactorCmdletAdapter.cs:line 150
   at EditorServicesCommandSuite.Internal.RefactorCmdletAdapter.ProcessRecord(MethodInvocationInfo methodInvocationInfo) in D:\a\1\s\src\EditorServicesCommandSuite\Internal\RefactorCmdletAdapter.cs:line 77
   at CallSite.Target(Closure , CallSite , Object , Object )
ninmonkey commented 4 years ago

Potential part

In some cases a lint, like a trailing space seemed to prevent splatting. This still gives the error Exception calling "ProcessRecord" with "1" argument(s): "An item with the same key has already been added. Key: Join-String"

This will splat

0..2 | Join-String -sep "n" -prop { $_::maxvalue }`

will not splat

(has a trailing space) 0..2 | Join-String -sep "n" -prop { $_::maxvalue } `

But a blank file with only this, no linting errors still breaks

Join-String -OutputPrefix '-'