Skip to content

Commit 830c40d

Browse files
committed
Fix FileNotFoundEx crash when Fold happens on untitled: scheme doc
1 parent 49cb3a3 commit 830c40d

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/PowerShellEditorServices.Protocol/Server/LanguageServer.cs

+15-5
Original file line numberDiff line numberDiff line change
@@ -1376,15 +1376,24 @@ protected Task HandleEvaluateRequest(
13761376

13771377
#region Event Handlers
13781378

1379-
private FoldingRange[] Fold(
1380-
string documentUri)
1379+
private FoldingRange[] Fold(string documentUri)
13811380
{
13821381
// TODO Should be using dynamic registrations
13831382
if (!this.currentSettings.CodeFolding.Enable) { return null; }
1383+
1384+
// Avoid crash when using untitled: scheme or any other scheme where the document doesn't
1385+
// have a backing file. https://github.com/PowerShell/vscode-powershell/issues/1676
1386+
// Perhaps a better option would be to parse the contents of the document as a string
1387+
// as opposed to reading a file but the senario of "no backing file" probably doesn't
1388+
// warrant the extra effort.
1389+
ScriptFile scriptFile = editorSession.Workspace.GetFile(documentUri);
1390+
if (scriptFile == null) { return null; }
1391+
13841392
var result = new List<FoldingRange>();
1385-
foreach (FoldingReference fold in TokenOperations.FoldableRegions(
1386-
editorSession.Workspace.GetFile(documentUri).ScriptTokens,
1387-
this.currentSettings.CodeFolding.ShowLastLine))
1393+
FoldingReference[] foldableRegions =
1394+
TokenOperations.FoldableRegions(scriptFile.ScriptTokens, this.currentSettings.CodeFolding.ShowLastLine);
1395+
1396+
foreach (FoldingReference fold in foldableRegions)
13881397
{
13891398
result.Add(new FoldingRange {
13901399
EndCharacter = fold.EndCharacter,
@@ -1394,6 +1403,7 @@ private FoldingRange[] Fold(
13941403
StartLine = fold.StartLine
13951404
});
13961405
}
1406+
13971407
return result.ToArray();
13981408
}
13991409

src/PowerShellEditorServices/Workspace/Workspace.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,11 @@ public ScriptFile GetFile(string filePath)
109109
string resolvedFilePath = this.ResolveFilePath(filePath);
110110
string keyName = resolvedFilePath.ToLower();
111111

112-
// Make sure the file isn't already loaded into the workspace
112+
// Make sure the file isn't already loaded into the workspace and that the filePath isn't an "in-memory" path.
113+
// There have been crashes caused by this method being called with an "untitled:" document uri. See:
114+
// https://github.com/PowerShell/vscode-powershell/issues/1676
113115
ScriptFile scriptFile = null;
114-
if (!this.workspaceFiles.TryGetValue(keyName, out scriptFile))
116+
if (!this.workspaceFiles.TryGetValue(keyName, out scriptFile) && !IsPathInMemory(filePath))
115117
{
116118
// This method allows FileNotFoundException to bubble up
117119
// if the file isn't found.

0 commit comments

Comments
 (0)