Skip to content

$psEditor.GetEditorContext().CurrentFile.FileRange returns the number of lines +1 #5627

Open

Description

@TMA-2

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all open and closed issues to ensure it has not already been reported.
  • I have read the troubleshooting guide.
  • I am sure this issue is with the extension itself and does not reproduce in a standalone PowerShell instance.
  • I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.
  • If this is a security issue, I have read the security issue reporting guidance.

Summary

Note

Apologies if this should go to PowerShellEditorServices instead; I'm not sure where the issue lies. I realize the .NET BufferRange is coming from PSES, but the file information must be coming from the VS Code LSP client initially...

TL;DR: Basically, $psEditor.GetEditorContext().CurrentFile.FileRange.End.Line seems to be 1 too high, so it can't be passed to $psEditor.GetEditorContext().CurrentFile.GetText().

Context:
I was quite happy to see that GetText() and GetTextLines() now expose IFileRange / FileRange overloads. And, they work just fine when passed a BufferFileRange from e.g. $psEditor.GetEditorContext().SelectedRange, but when passed $psEditor.GetEditorContext().CurrentFile.FileRange, an out-of-range exception is thrown, specifically that FileRange.End.Line is 1 higher than the actual ending line. This is kind of odd, given it's 1-index, and the last line is clearly visible, and even on calling GetTextLines().Count or GetTextLines().GetUpperBound(0), it's right there.

So, perhaps the FileRange property is getting lost in translation somewhere, changing between 0-index and 1-index or something? Unless there's some technical reason for this that's over my head, which is very likely.

PowerShell Version

Name                           Value
----                           -----
PSVersion                      7.6.6
PSEdition                      Core
GitCommitId                    7.6.6
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.4
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Name             : Visual Studio Code Host
Version          : 2026.1.2
InstanceId       : ffb915bc-f12b-4cf8-8582-0a7f02602842
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-US
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

Visual Studio Code Version

1.140.0
07f806f999227108933c2e30515b26eecc1fda74
x64

Extension Version

ms-vscode.powershell@2026.1.2

tylerleonhardt.vscode-inline-values-powershell@0.0.7
justin-grote.powershell-specter@0.0.1
liamjpeters.powershell-ast-inspector@0.5.2

(love the Specter and AST Inspector extensions, guys!)

Steps to Reproduce

  1. Open any file in VS Code with the PowerShell extension started
  2. Run in the integrated terminal: $psEditor.GetEditorContext().CurrentFile.GetText($psEditor.GetEditorContext().CurrentFile.FileRange)
  3. Run in the integrated terminal: $psEditor.GetEditorContext().CurrentFile.GetTextLines($psEditor.GetEditorContext().CurrentFile.FileRange)
  4. Compare $psEditor.GetEditorContext().CurrentFile.FileRange.End.Line to the file's actual last line: $psEditor.GetEditorContext().CurrentFile.GetTextLines().Count

Visuals

Image

Logs

There were no relevent logs that I could see, despite being powershell.developer.editorServicesLogLevel set to Trace and powershell.trace.server set to Verbose. I'm not sure if I'm doing something wrong, but the only trace messages I see are in the integrated terminal on startup, but never in the output log. Likewise, the "Trace LSP" log only ever outputs:

2026-10-07 21:12:08.969 [info] No parameters provided.
2026-10-07 21:13:06.613 [info] No result returned.

In the ~4 years I've been using VS Code with PowerShell, I've never seen anything else in the LSP log. I may be missing something, though. Only the following error was thrown:

MethodInvocationException: Exception calling "GetText" with "1" argument(s): "Specified argument was out of the range of valid values. (Parameter 'Position 93:17 is outside of the line range of 1 to 92.')"

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugA bug to squash.Needs: TriageMaintainer attention needed!

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions