Skip to content

Commit 3697d36

Browse files
rkeithhillTylerLeonhardt
authored andcommitted
Fix FileNotFoundEx crash when Fold happens on untitled: scheme doc (#839)
* Fix FileNotFoundEx crash when Fold happens on untitled: scheme doc * Address PR feedback, use TryGetFile() instead of modifying GetFile
1 parent 73044d1 commit 3697d36

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
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;
1390+
if (!editorSession.Workspace.TryGetFile(documentUri, out scriptFile)) { 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-5
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ public ScriptFile CreateScriptFileFromFileBuffer(string filePath, string initial
9191
}
9292

9393
/// <summary>
94-
/// Gets an open file in the workspace. If the file isn't open but
95-
/// exists on the filesystem, load and return it.
94+
/// Gets an open file in the workspace. If the file isn't open but exists on the filesystem, load and return it.
95+
/// <para>IMPORTANT: Not all documents have a backing file e.g. untitled: scheme documents. Consider using
96+
/// <see cref="Workspace.TryGetFile(string, out ScriptFile)"/> instead.</para>
9697
/// </summary>
9798
/// <param name="filePath">The file path at which the script resides.</param>
9899
/// <exception cref="FileNotFoundException">
@@ -154,9 +155,7 @@ e is DirectoryNotFoundException ||
154155
e is PathTooLongException ||
155156
e is UnauthorizedAccessException)
156157
{
157-
this.logger.WriteException(
158-
$"Failed to set breakpoint on file: {filePath}",
159-
e);
158+
this.logger.WriteHandledException($"Failed to get file for {nameof(filePath)}: '{filePath}'", e);
160159
scriptFile = null;
161160
return false;
162161
}

0 commit comments

Comments
 (0)