Skip to content

CompleteInput fails when passed an empty string/AST #5743

@SeeminglyScience

Description

@SeeminglyScience

Steps to reproduce

[System.Management.Automation.CommandCompletion]::CompleteInput('', 0, @{})

Or

[System.Management.Automation.CommandCompletion]::CompleteInput(
	{}.Ast,
	@(),
	{}.Ast.Extent.StartScriptPosition,
	@{})

Expected behavior

CurrentMatchIndex ReplacementIndex ReplacementLength CompletionMatches
----------------- ---------------- ----------------- -----------------
               -1                0                 0 {}

Alternatively if throwing is a design choice, throw an ArgumentException instead of letting Enumerable.Last fail.

Actual behavior

> [System.Management.Automation.CommandCompletion]::CompleteInput('', 0, @{})
Exception calling "CompleteInput" with "3" argument(s): "Sequence contains no elements"
At line:1 char:1
+ [System.Management.Automation.CommandCompletion]::CompleteInput('', 0 ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException

> $Error[0].Exception.InnerException | Format-List * -Force
Message        : Sequence contains no elements
Data           : {}
InnerException :
TargetSite     : TSource Last[TSource](System.Collections.Generic.IEnumerable`1[TSource])
StackTrace     :    at System.Linq.Enumerable.Last[TSource](IEnumerable`1 source)
                    at
                 System.Management.Automation.CompletionAnalysis.InitializeCompletionContext(TypeInferenceContext
                 typeInferenceContext)
                    at System.Management.Automation.CompletionAnalysis.GetResults(PowerShell powerShell, Int32&
                 replacementIndex, Int32& replacementLength)
                    at System.Management.Automation.CommandCompletion.CompleteInputImpl(Ast ast, Token[] tokens,
                 IScriptPosition positionOfCursor, Hashtable options)
                    at System.Management.Automation.CommandCompletion.CompleteInput(String input, Int32 cursorIndex,
                 Hashtable options)
                    at CallSite.Target(Closure , CallSite , Type , String , Int32 , Hashtable )
HelpLink       :
Source         : System.Linq
HResult        : -2146233079

Environment data

> $PSVersionTable
Name                           Value
----                           -----
PSVersion                      6.0.0-rc.2
PSEdition                      Core
GitCommitId                    v6.0.0-rc.2
OS                             Microsoft Windows 10.0.15063
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions