Skip to content

Commit

Permalink
Threw FileNotFoundException when file could not be retrieved from Pow…
Browse files Browse the repository at this point in the history
…erShell to match previous error handling. Removed artificial limitation on Uri schemes.
  • Loading branch information
dkattan committed Feb 13, 2024
1 parent ab53c2f commit 59af6f2
Showing 1 changed file with 45 additions and 19 deletions.
64 changes: 45 additions & 19 deletions src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,6 @@ public async Task<ScriptFile> GetFile(DocumentUri documentUri)
public async Task<ScriptFile?> TryGetFile(DocumentUri documentUri)
{
ScriptFile? scriptFile;
switch (documentUri.Scheme)
{
// List supported schemes here
case "file":
case "inmemory":
case "untitled":
case "vscode-notebook-cell":
break;

default:
scriptFile = null;
return scriptFile;
}

try
{
scriptFile = await GetFile(documentUri).ConfigureAwait(false);
Expand All @@ -233,7 +219,7 @@ IOException or
SecurityException or
UnauthorizedAccessException)
{
logger.LogWarning($"Failed to get file for fileUri: '{documentUri}'", e);
logger.LogWarning($"Failed to get file for Uri: '{documentUri}'", e);
scriptFile = null;
return scriptFile;
}
Expand Down Expand Up @@ -450,11 +436,51 @@ internal static StreamReader OpenStreamReader(DocumentUri uri)
internal async Task<string> ReadFileContents(DocumentUri uri)
{
PSCommand psCommand = new();
psCommand.AddCommand(@"Microsoft.PowerShell.Core\Get-Content")
.AddParameter("Path", uri)
string pspath;
if (uri.Scheme == Uri.UriSchemeFile)
{
pspath = uri.ToUri().LocalPath;
}
else
{
string PSProvider = uri.Authority;
string path = uri.Path;
pspath = $"{PSProvider}::{path}";

}
/* uri - "file:///c:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
* Authority = ""
* Fragment = ""
* Path = "/C:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
* Query = ""
* Scheme = "file"
* PSPath - "Microsoft.PowerShell.Core\FileSystem::C:\Users\dkattan\source\repos\immybot-ref\submodules\PowerShellEditorServices\test\PowerShellEditorServices.Test.Shared\Completion\CompletionExamples.psm1"
*
* Suggested Format:
* Authority = "Microsoft.PowerShell.Core\FileSystem"
* Scheme = "PSProvider"
* Path = "/C:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
* Result -> "PSProvider://Microsoft.PowerShell.Core/FileSystem::C:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
*
* Suggested Format 2:
* Authority = ""
* Scheme = "FileSystem"
* Path = "/C:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
* Result "FileSystem://c:/Users/dkattan/source/repos/immybot-ref/submodules/PowerShellEditorServices/test/PowerShellEditorServices.Test.Shared/Completion/CompletionExamples.psm1"
*/
psCommand.AddCommand("Get-Content")
.AddParameter("Path", pspath)
.AddParameter("Raw", true);
IEnumerable<string> result = await executionService.ExecutePSCommandAsync<string>(psCommand, CancellationToken.None).ConfigureAwait(false);
return result.First();
IEnumerable<string> result = await executionService.ExecutePSCommandAsync<string>(psCommand, CancellationToken.None, new PowerShell.Execution.PowerShellExecutionOptions()
{
ThrowOnError = true
}).ConfigureAwait(false);
if (result.Count() == 0)
{
throw new FileNotFoundException(pspath);
}
return result.FirstOrDefault();
}
internal Task<string> ResolveWorkspacePath(string path) => ResolveRelativeScriptPathAsync(InitialWorkingDirectory, path);

Expand Down

0 comments on commit 59af6f2

Please sign in to comment.