Skip to content

Commit d5c04c5

Browse files
committed
Fix PowerShell#342: Handle unsupported file URI schemes reliably
1 parent 8f0dd69 commit d5c04c5

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/PowerShellEditorServices/Workspace/Workspace.cs

+24-12
Original file line numberDiff line numberDiff line change
@@ -358,18 +358,30 @@ private string ResolveFilePath(string filePath)
358358

359359
internal static bool IsPathInMemory(string filePath)
360360
{
361-
// When viewing PowerShell files in the Git diff viewer, VS Code
362-
// sends the contents of the file at HEAD with a URI that starts
363-
// with 'inmemory'. Untitled files which have been marked of
364-
// type PowerShell have a path starting with 'untitled'.
365-
return
366-
filePath.StartsWith("inmemory") ||
367-
filePath.StartsWith("untitled") ||
368-
filePath.StartsWith("private") ||
369-
filePath.StartsWith("git");
370-
371-
// TODO #342: Remove 'private' and 'git' and then add logic to
372-
// throw when any unsupported file URI scheme is encountered.
361+
bool isInMemory = false;
362+
363+
// In cases where a "virtual" file is displayed in the editor,
364+
// we need to treat the file differently than one that exists
365+
// on disk. A virtual file could be something like a diff
366+
// view of the current file or an untitled file.
367+
try
368+
{
369+
// NotSupportedException or ArgumentException gets thrown when
370+
// given an invalid path. Since any non-standard path will
371+
// trigger this, assume that it means it's an in-memory file
372+
// unless the path starts with file:
373+
Path.GetFullPath(filePath);
374+
}
375+
catch (ArgumentException)
376+
{
377+
isInMemory = true;
378+
}
379+
catch (NotSupportedException)
380+
{
381+
isInMemory = true;
382+
}
383+
384+
return !filePath.ToLower().StartsWith("file:") && isInMemory;
373385
}
374386

375387
private string GetBaseFilePath(string filePath)

0 commit comments

Comments
 (0)