diff --git a/src/PowerShellEditorServices/Services/Extension/EditorOperationsService.cs b/src/PowerShellEditorServices/Services/Extension/EditorOperationsService.cs index 7a7c6e6e7..607d38720 100644 --- a/src/PowerShellEditorServices/Services/Extension/EditorOperationsService.cs +++ b/src/PowerShellEditorServices/Services/Extension/EditorOperationsService.cs @@ -192,7 +192,8 @@ public async Task SaveFileAsync(string currentPath, string newSavePath) } // NOTE: This name is now outdated since we don't have a way to distinguish one workspace - // from another for the extension API. + // from another for the extension API. TODO: Should this be an empty string if we have no + // workspaces? public string GetWorkspacePath() => _workspaceService.InitialWorkingDirectory; public string[] GetWorkspacePaths() => _workspaceService.WorkspacePaths.ToArray(); diff --git a/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs b/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs index e8f702321..54a1f2894 100644 --- a/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs +++ b/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs @@ -104,9 +104,7 @@ public WorkspaceService(ILoggerFactory factory) #region Public Methods - public IEnumerable WorkspacePaths => WorkspaceFolders.Count == 0 - ? new List { InitialWorkingDirectory } - : WorkspaceFolders.Select(i => i.Uri.GetFileSystemPath()); + public IEnumerable WorkspacePaths => WorkspaceFolders.Select(i => i.Uri.GetFileSystemPath()); /// /// Gets an open file in the workspace. If the file isn't open but exists on the filesystem, load and return it. diff --git a/test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs b/test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs index a25e4e8db..01f31325a 100644 --- a/test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs +++ b/test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs @@ -7,7 +7,6 @@ using System.Runtime.InteropServices; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.PowerShell.EditorServices.Services; -using Microsoft.PowerShell.EditorServices.Test.Shared; using Microsoft.PowerShell.EditorServices.Services.TextDocument; using Xunit; using Microsoft.PowerShell.EditorServices.Utility; @@ -27,6 +26,10 @@ public class WorkspaceTests internal static ScriptFile CreateScriptFile(string path) => new(path, "", VersionUtils.PSVersion); + // Remember that LSP does weird stuff to the drive letter, so we have to convert it to a URI + // and back to ensure that drive letter gets lower cased and everything matches up. + private static string s_workspacePath => + DocumentUri.FromFileSystemPath(Path.GetFullPath("Fixtures/Workspace")).GetFileSystemPath(); [Fact] public void CanResolveWorkspaceRelativePath() @@ -76,7 +79,10 @@ internal static WorkspaceService FixturesWorkspace() { return new WorkspaceService(NullLoggerFactory.Instance) { - InitialWorkingDirectory = TestUtilities.NormalizePath("Fixtures/Workspace") + WorkspaceFolders = + { + new WorkspaceFolder { Uri = DocumentUri.FromFileSystemPath(s_workspacePath) } + } }; } @@ -84,8 +90,10 @@ internal static WorkspaceService FixturesWorkspace() public void HasDefaultForWorkspacePaths() { WorkspaceService workspace = FixturesWorkspace(); - string actual = Assert.Single(workspace.WorkspacePaths); - Assert.Equal(workspace.InitialWorkingDirectory, actual); + string workspacePath = Assert.Single(workspace.WorkspacePaths); + Assert.Equal(s_workspacePath, workspacePath); + // We shouldn't assume an initial working directory since none was given. + Assert.Null(workspace.InitialWorkingDirectory); } // These are the default values for the EnumeratePSFiles() method @@ -129,10 +137,10 @@ public void CanRecurseDirectoryTree() List expected = new() { - Path.Combine(workspace.InitialWorkingDirectory, "nested", "donotfind.ps1"), - Path.Combine(workspace.InitialWorkingDirectory, "nested", "nestedmodule.psd1"), - Path.Combine(workspace.InitialWorkingDirectory, "nested", "nestedmodule.psm1"), - Path.Combine(workspace.InitialWorkingDirectory, "rootfile.ps1") + Path.Combine(s_workspacePath, "nested", "donotfind.ps1"), + Path.Combine(s_workspacePath, "nested", "nestedmodule.psd1"), + Path.Combine(s_workspacePath, "nested", "nestedmodule.psm1"), + Path.Combine(s_workspacePath, "rootfile.ps1") }; // .NET Core doesn't appear to use the same three letter pattern matching rule although the docs @@ -140,7 +148,7 @@ public void CanRecurseDirectoryTree() // ref https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.getfiles?view=netcore-2.1#System_IO_Directory_GetFiles_System_String_System_String_System_IO_EnumerationOptions_ if (RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework")) { - expected.Insert(3, Path.Combine(workspace.InitialWorkingDirectory, "other", "other.ps1xml")); + expected.Insert(3, Path.Combine(s_workspacePath, "other", "other.ps1xml")); } Assert.Equal(expected, actual); @@ -157,7 +165,7 @@ public void CanRecurseDirectoryTreeWithLimit() maxDepth: 1, ignoreReparsePoints: s_defaultIgnoreReparsePoints ); - Assert.Equal(new[] { Path.Combine(workspace.InitialWorkingDirectory, "rootfile.ps1") }, actual); + Assert.Equal(new[] { Path.Combine(s_workspacePath, "rootfile.ps1") }, actual); } [Fact] @@ -173,8 +181,8 @@ public void CanRecurseDirectoryTreeWithGlobs() ); Assert.Equal(new[] { - Path.Combine(workspace.InitialWorkingDirectory, "nested", "nestedmodule.psd1"), - Path.Combine(workspace.InitialWorkingDirectory, "rootfile.ps1") + Path.Combine(s_workspacePath, "nested", "nestedmodule.psd1"), + Path.Combine(s_workspacePath, "rootfile.ps1") }, actual); } @@ -182,7 +190,7 @@ public void CanRecurseDirectoryTreeWithGlobs() public void CanOpenAndCloseFile() { WorkspaceService workspace = FixturesWorkspace(); - string filePath = Path.GetFullPath(Path.Combine(workspace.InitialWorkingDirectory, "rootfile.ps1")); + string filePath = Path.GetFullPath(Path.Combine(s_workspacePath, "rootfile.ps1")); ScriptFile file = workspace.GetFile(filePath); Assert.Equal(workspace.GetOpenedFiles(), new[] { file });