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 issue where # in path causes the path to resolve incorrectly #750

Merged
merged 4 commits into from
Sep 26, 2018
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
44 changes: 41 additions & 3 deletions src/PowerShellEditorServices/Workspace/Workspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
using System.Security;
using System.Text;

#if CoreCLR
using System.Runtime.InteropServices;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used anywhere else? If not, maybe get rid of this ifdef and use the full name space below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only used in the one line, but twice. We would need the full namespace on both RuntimeInformation.IsOSPlatform() and OSPlatform.Windows.

I just tried it, and I think the import is less cumbersome.

#endif

namespace Microsoft.PowerShell.EditorServices
{
/// <summary>
Expand Down Expand Up @@ -355,15 +359,15 @@ private void RecursivelyFindReferences(
}
}

private string ResolveFilePath(string filePath)
internal string ResolveFilePath(string filePath)
{
if (!IsPathInMemory(filePath))
{
if (filePath.StartsWith(@"file://"))
{
filePath = Workspace.UnescapeDriveColon(filePath);
// Client sent the path in URI format, extract the local path
Uri fileUri = new Uri(Uri.UnescapeDataString(filePath));
filePath = fileUri.LocalPath;
filePath = new Uri(filePath).LocalPath;
}

// Clients could specify paths with escaped space, [ and ] characters which .NET APIs
Expand Down Expand Up @@ -486,6 +490,40 @@ private string ResolveRelativeScriptPath(string baseFilePath, string relativePat
return combinedPath;
}

/// <summary>
/// Takes a file-scheme URI with an escaped colon after the drive letter and unescapes only the colon.
/// VSCode sends escaped colons after drive letters, but System.Uri expects unescaped.
/// </summary>
/// <param name="fileUri">The fully-escaped file-scheme URI string.</param>
/// <returns>A file-scheme URI string with the drive colon unescaped.</returns>
private static string UnescapeDriveColon(string fileUri)
{
#if CoreCLR
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return fileUri;
}
#endif
// Check here that we have something like "file:///C%3A/" as a prefix (caller must check the file:// part)
if (!(fileUri[7] == '/' &&
char.IsLetter(fileUri[8]) &&
fileUri[9] == '%' &&
fileUri[10] == '3' &&
fileUri[11] == 'A' &&
fileUri[12] == '/'))
{
return fileUri;
}

var sb = new StringBuilder(fileUri.Length - 2); // We lost "%3A" and gained ":", so length - 2
sb.Append("file:///");
sb.Append(fileUri[8]); // The drive letter
sb.Append(':');
sb.Append(fileUri.Substring(12)); // The rest of the URI after the colon

return sb.ToString();
}

#endregion
}
}
19 changes: 19 additions & 0 deletions test/PowerShellEditorServices.Test/Session/WorkspaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,24 @@ public void CanDetermineIsPathInMemory()
$"Testing path {testCase.Path}");
}
}

[Theory()]
[InlineData("file:///C%3A/banana/", @"C:\banana\")]
[InlineData("file:///C%3A/banana/ex.ps1", @"C:\banana\ex.ps1")]
[InlineData("file:///E%3A/Path/to/awful%23path", @"E:\Path\to\awful#path")]
[InlineData("file:///path/with/no/drive", @"C:\path\with\no\drive")]
[InlineData("file:///path/wi[th]/squ[are/brackets/", @"C:\path\wi[th]\squ[are\brackets\")]
[InlineData("file:///Carrots/A%5Ere/Good/", @"C:\Carrots\A^re\Good\")]
[InlineData("file:///Users/barnaby/%E8%84%9A%E6%9C%AC/Reduce-Directory", @"C:\Users\barnaby\脚本\Reduce-Directory")]
[InlineData("file:///C%3A/Program%20Files%20%28x86%29/PowerShell/6/pwsh.exe", @"C:\Program Files (x86)\PowerShell\6\pwsh.exe")]
[InlineData("file:///home/maxim/test%20folder/%D0%9F%D0%B0%D0%BF%D0%BA%D0%B0/helloworld.ps1", @"C:\home\maxim\test folder\Папка\helloworld.ps1")]
public void CorrectlyResolvesPaths(string givenPath, string expectedPath)
{
Workspace workspace = new Workspace(PowerShellVersion, Logging.NullLogger);

string resolvedPath = workspace.ResolveFilePath(givenPath);

Assert.Equal(expectedPath, resolvedPath);
}
}
}