Skip to content

Commit 59af6f2

Browse files
committed
Threw FileNotFoundException when file could not be retrieved from PowerShell to match previous error handling. Removed artificial limitation on Uri schemes.
1 parent ab53c2f commit 59af6f2

File tree

1 file changed

+45
-19
lines changed

1 file changed

+45
-19
lines changed

src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs

+45-19
Original file line numberDiff line numberDiff line change
@@ -205,20 +205,6 @@ public async Task<ScriptFile> GetFile(DocumentUri documentUri)
205205
public async Task<ScriptFile?> TryGetFile(DocumentUri documentUri)
206206
{
207207
ScriptFile? scriptFile;
208-
switch (documentUri.Scheme)
209-
{
210-
// List supported schemes here
211-
case "file":
212-
case "inmemory":
213-
case "untitled":
214-
case "vscode-notebook-cell":
215-
break;
216-
217-
default:
218-
scriptFile = null;
219-
return scriptFile;
220-
}
221-
222208
try
223209
{
224210
scriptFile = await GetFile(documentUri).ConfigureAwait(false);
@@ -233,7 +219,7 @@ IOException or
233219
SecurityException or
234220
UnauthorizedAccessException)
235221
{
236-
logger.LogWarning($"Failed to get file for fileUri: '{documentUri}'", e);
222+
logger.LogWarning($"Failed to get file for Uri: '{documentUri}'", e);
237223
scriptFile = null;
238224
return scriptFile;
239225
}
@@ -450,11 +436,51 @@ internal static StreamReader OpenStreamReader(DocumentUri uri)
450436
internal async Task<string> ReadFileContents(DocumentUri uri)
451437
{
452438
PSCommand psCommand = new();
453-
psCommand.AddCommand(@"Microsoft.PowerShell.Core\Get-Content")
454-
.AddParameter("Path", uri)
439+
string pspath;
440+
if (uri.Scheme == Uri.UriSchemeFile)
441+
{
442+
pspath = uri.ToUri().LocalPath;
443+
}
444+
else
445+
{
446+
string PSProvider = uri.Authority;
447+
string path = uri.Path;
448+
pspath = $"{PSProvider}::{path}";
449+
450+
}
451+
/* uri - "file:///c:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
452+
* Authority = ""
453+
* Fragment = ""
454+
* Path = "/C:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
455+
* Query = ""
456+
* Scheme = "file"
457+
* PSPath - "Microsoft.PowerShell.Core\FileSystem::C:\Users\dkattan\source\repos\immybot-ref\submodules\PowerShellEditorServices\test\PowerShellEditorServices.Test.Shared\Completion\CompletionExamples.psm1"
458+
*
459+
* Suggested Format:
460+
* Authority = "Microsoft.PowerShell.Core\FileSystem"
461+
* Scheme = "PSProvider"
462+
* Path = "/C:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
463+
* Result -> "PSProvider://Microsoft.PowerShell.Core/FileSystem::C:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
464+
*
465+
* Suggested Format 2:
466+
* Authority = ""
467+
* Scheme = "FileSystem"
468+
* Path = "/C:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
469+
* Result "FileSystem://c:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
470+
471+
*/
472+
psCommand.AddCommand("Get-Content")
473+
.AddParameter("Path", pspath)
455474
.AddParameter("Raw", true);
456-
IEnumerable<string> result = await executionService.ExecutePSCommandAsync<string>(psCommand, CancellationToken.None).ConfigureAwait(false);
457-
return result.First();
475+
IEnumerable<string> result = await executionService.ExecutePSCommandAsync<string>(psCommand, CancellationToken.None, new PowerShell.Execution.PowerShellExecutionOptions()
476+
{
477+
ThrowOnError = true
478+
}).ConfigureAwait(false);
479+
if (result.Count() == 0)
480+
{
481+
throw new FileNotFoundException(pspath);
482+
}
483+
return result.FirstOrDefault();
458484
}
459485
internal Task<string> ResolveWorkspacePath(string path) => ResolveRelativeScriptPathAsync(InitialWorkingDirectory, path);
460486

0 commit comments

Comments
 (0)