Skip to content

Commit 6dfb4f6

Browse files
committed
Allow WorkspacePaths to be empty if we're not in a workspace
We were previously adding the initial working directory as a fallback if there was no workspace. However, this led to new VS Code windows (not in a workspace) enumerating all files in the home directory (if that's what the client had to default to, without a workspace folder). And this could even spam permission requests on more secure systems such as macOS. So we just need to let be an empty enumerable.
1 parent a691981 commit 6dfb4f6

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

src/PowerShellEditorServices/Services/Extension/EditorOperationsService.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ public async Task SaveFileAsync(string currentPath, string newSavePath)
192192
}
193193

194194
// NOTE: This name is now outdated since we don't have a way to distinguish one workspace
195-
// from another for the extension API.
195+
// from another for the extension API. TODO: Should this be an empty string if we have no
196+
// workspaces?
196197
public string GetWorkspacePath() => _workspaceService.InitialWorkingDirectory;
197198

198199
public string[] GetWorkspacePaths() => _workspaceService.WorkspacePaths.ToArray();

src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,7 @@ public WorkspaceService(ILoggerFactory factory)
104104

105105
#region Public Methods
106106

107-
public IEnumerable<string> WorkspacePaths => WorkspaceFolders.Count == 0
108-
? new List<string> { InitialWorkingDirectory }
109-
: WorkspaceFolders.Select(i => i.Uri.GetFileSystemPath());
107+
public IEnumerable<string> WorkspacePaths => WorkspaceFolders.Select(i => i.Uri.GetFileSystemPath());
110108

111109
/// <summary>
112110
/// Gets an open file in the workspace. If the file isn't open but exists on the filesystem, load and return it.

test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs

+19-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Runtime.InteropServices;
88
using Microsoft.Extensions.Logging.Abstractions;
99
using Microsoft.PowerShell.EditorServices.Services;
10-
using Microsoft.PowerShell.EditorServices.Test.Shared;
1110
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
1211
using Xunit;
1312
using Microsoft.PowerShell.EditorServices.Utility;
@@ -27,6 +26,8 @@ public class WorkspaceTests
2726

2827
internal static ScriptFile CreateScriptFile(string path) => new(path, "", VersionUtils.PSVersion);
2928

29+
// Remember that LSP does weird stuff to the drive letter, so it has to be lower case.
30+
private static readonly string s_workspacePath = Path.GetFullPath("Fixtures/Workspace").Replace("C:\\", "c:\\");
3031

3132
[Fact]
3233
public void CanResolveWorkspaceRelativePath()
@@ -76,16 +77,21 @@ internal static WorkspaceService FixturesWorkspace()
7677
{
7778
return new WorkspaceService(NullLoggerFactory.Instance)
7879
{
79-
InitialWorkingDirectory = TestUtilities.NormalizePath("Fixtures/Workspace")
80+
WorkspaceFolders =
81+
{
82+
new WorkspaceFolder { Uri = DocumentUri.FromFileSystemPath(s_workspacePath) }
83+
}
8084
};
8185
}
8286

8387
[Fact]
8488
public void HasDefaultForWorkspacePaths()
8589
{
8690
WorkspaceService workspace = FixturesWorkspace();
87-
string actual = Assert.Single(workspace.WorkspacePaths);
88-
Assert.Equal(workspace.InitialWorkingDirectory, actual);
91+
string workspacePath = Assert.Single(workspace.WorkspacePaths);
92+
Assert.Equal(s_workspacePath, workspacePath);
93+
// We shouldn't assume an initial working directory since none was given.
94+
Assert.Null(workspace.InitialWorkingDirectory);
8995
}
9096

9197
// These are the default values for the EnumeratePSFiles() method
@@ -129,18 +135,18 @@ public void CanRecurseDirectoryTree()
129135

130136
List<string> expected = new()
131137
{
132-
Path.Combine(workspace.InitialWorkingDirectory, "nested", "donotfind.ps1"),
133-
Path.Combine(workspace.InitialWorkingDirectory, "nested", "nestedmodule.psd1"),
134-
Path.Combine(workspace.InitialWorkingDirectory, "nested", "nestedmodule.psm1"),
135-
Path.Combine(workspace.InitialWorkingDirectory, "rootfile.ps1")
138+
Path.Combine(s_workspacePath, "nested", "donotfind.ps1"),
139+
Path.Combine(s_workspacePath, "nested", "nestedmodule.psd1"),
140+
Path.Combine(s_workspacePath, "nested", "nestedmodule.psm1"),
141+
Path.Combine(s_workspacePath, "rootfile.ps1")
136142
};
137143

138144
// .NET Core doesn't appear to use the same three letter pattern matching rule although the docs
139145
// suggest it should be find the '.ps1xml' files because we search for the pattern '*.ps1'
140146
// 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_
141147
if (RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework"))
142148
{
143-
expected.Insert(3, Path.Combine(workspace.InitialWorkingDirectory, "other", "other.ps1xml"));
149+
expected.Insert(3, Path.Combine(s_workspacePath, "other", "other.ps1xml"));
144150
}
145151

146152
Assert.Equal(expected, actual);
@@ -157,7 +163,7 @@ public void CanRecurseDirectoryTreeWithLimit()
157163
maxDepth: 1,
158164
ignoreReparsePoints: s_defaultIgnoreReparsePoints
159165
);
160-
Assert.Equal(new[] { Path.Combine(workspace.InitialWorkingDirectory, "rootfile.ps1") }, actual);
166+
Assert.Equal(new[] { Path.Combine(s_workspacePath, "rootfile.ps1") }, actual);
161167
}
162168

163169
[Fact]
@@ -173,16 +179,16 @@ public void CanRecurseDirectoryTreeWithGlobs()
173179
);
174180

175181
Assert.Equal(new[] {
176-
Path.Combine(workspace.InitialWorkingDirectory, "nested", "nestedmodule.psd1"),
177-
Path.Combine(workspace.InitialWorkingDirectory, "rootfile.ps1")
182+
Path.Combine(s_workspacePath, "nested", "nestedmodule.psd1"),
183+
Path.Combine(s_workspacePath, "rootfile.ps1")
178184
}, actual);
179185
}
180186

181187
[Fact]
182188
public void CanOpenAndCloseFile()
183189
{
184190
WorkspaceService workspace = FixturesWorkspace();
185-
string filePath = Path.GetFullPath(Path.Combine(workspace.InitialWorkingDirectory, "rootfile.ps1"));
191+
string filePath = Path.GetFullPath(Path.Combine(s_workspacePath, "rootfile.ps1"));
186192

187193
ScriptFile file = workspace.GetFile(filePath);
188194
Assert.Equal(workspace.GetOpenedFiles(), new[] { file });

0 commit comments

Comments
 (0)