Skip to content

Commit bc123b4

Browse files
committed
Fix error handling in TryGetFile
1 parent 95c9afe commit bc123b4

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs

+15-8
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Collections.Concurrent;
56
using System.Collections.Generic;
6-
using System.Linq;
77
using System.IO;
8+
using System.Linq;
89
using System.Security;
910
using System.Text;
1011
using Microsoft.Extensions.FileSystemGlobbing;
1112
using Microsoft.Extensions.Logging;
12-
using Microsoft.PowerShell.EditorServices.Utility;
13-
using Microsoft.PowerShell.EditorServices.Services.Workspace;
1413
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
15-
using System.Collections.Concurrent;
14+
using Microsoft.PowerShell.EditorServices.Services.Workspace;
15+
using Microsoft.PowerShell.EditorServices.Utility;
1616
using OmniSharp.Extensions.LanguageServer.Protocol;
1717

1818
namespace Microsoft.PowerShell.EditorServices.Services
@@ -154,9 +154,16 @@ public ScriptFile GetFile(DocumentUri documentUri)
154154
/// <param name="scriptFile">The out parameter that will contain the ScriptFile object.</param>
155155
public bool TryGetFile(string filePath, out ScriptFile scriptFile)
156156
{
157-
scriptFile = null;
158-
return Uri.IsWellFormedUriString(filePath, UriKind.RelativeOrAbsolute)
159-
&& TryGetFile(new Uri(filePath), out scriptFile);
157+
// This might not have been given a file path, in which case the Uri constructor barfs.
158+
try
159+
{
160+
return TryGetFile(new Uri(filePath), out scriptFile);
161+
}
162+
catch (UriFormatException)
163+
{
164+
scriptFile = null;
165+
return false;
166+
}
160167
}
161168

162169
/// <summary>
@@ -305,7 +312,7 @@ public ScriptFile[] ExpandScriptReferences(ScriptFile scriptFile)
305312
referencedScriptFiles.Add(scriptFile.Id, scriptFile);
306313
RecursivelyFindReferences(scriptFile, referencedScriptFiles);
307314

308-
// remove original file from referened file and add it as the first element of the
315+
// remove original file from referenced file and add it as the first element of the
309316
// expanded referenced list to maintain order so the original file is always first in the list
310317
referencedScriptFiles.Remove(scriptFile.Id);
311318
expandedReferences.Add(scriptFile);

0 commit comments

Comments
 (0)