Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix file close in workspace service for Linux #1902

Merged
merged 1 commit into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ public ScriptFile GetFile(DocumentUri documentUri)
{
Validate.IsNotNull(nameof(documentUri), documentUri);

string keyName = VersionUtils.IsLinux
? documentUri.ToString()
: documentUri.ToString().ToLower();
string keyName = GetFileKey(documentUri);

// Make sure the file isn't already loaded into the workspace
if (!workspaceFiles.TryGetValue(keyName, out ScriptFile scriptFile))
Expand Down Expand Up @@ -258,9 +256,7 @@ public ScriptFile GetFileBuffer(DocumentUri documentUri, string initialBuffer)
{
Validate.IsNotNull(nameof(documentUri), documentUri);

string keyName = VersionUtils.IsLinux
? documentUri.ToString()
: documentUri.ToString().ToLower();
string keyName = GetFileKey(documentUri);

// Make sure the file isn't already loaded into the workspace
if (!workspaceFiles.TryGetValue(keyName, out ScriptFile scriptFile) && initialBuffer != null)
Expand Down Expand Up @@ -293,7 +289,8 @@ public void CloseFile(ScriptFile scriptFile)
{
Validate.IsNotNull(nameof(scriptFile), scriptFile);

workspaceFiles.TryRemove(scriptFile.Id, out ScriptFile _);
string keyName = GetFileKey(scriptFile.DocumentUri);
workspaceFiles.TryRemove(keyName, out ScriptFile _);
}

/// <summary>
Expand Down Expand Up @@ -540,6 +537,14 @@ internal string ResolveRelativeScriptPath(string baseFilePath, string relativePa
return combinedPath;
}

/// <summary>
/// Returns a normalized string for a given documentUri to be used as key name.
/// Case-sensitive uri on Linux and lowercase for other platforms.
/// </summary>
/// <param name="documentUri">A DocumentUri object to get a normalized key name from</param>
private static string GetFileKey(DocumentUri documentUri)
=> VersionUtils.IsLinux ? documentUri.ToString() : documentUri.ToString().ToLower();

#endregion
}
}
14 changes: 14 additions & 0 deletions test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.PowerShell.EditorServices.Services;
using Microsoft.PowerShell.EditorServices.Test.Shared;
using Microsoft.PowerShell.EditorServices.Services.TextDocument;
using Xunit;

namespace PowerShellEditorServices.Test.Session
Expand Down Expand Up @@ -175,5 +176,18 @@ public void CanDetermineIsPathInMemory()

Assert.All(notInMemoryPaths, (p) => Assert.False(WorkspaceService.IsPathInMemory(p)));
}

[Fact]
public void CanOpenAndCloseFile()
{
WorkspaceService workspace = FixturesWorkspace();
string filePath = Path.GetFullPath(Path.Combine(workspace.WorkspacePath, "rootfile.ps1"));

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

workspace.CloseFile(file);
Assert.Equal(workspace.GetOpenedFiles(), Array.Empty<ScriptFile>());
}
}
}