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

Don't rely on Uri.ToString() in Serializer #195

Closed
TylerLeonhardt opened this issue Dec 18, 2019 · 1 comment · Fixed by #196
Closed

Don't rely on Uri.ToString() in Serializer #195

TylerLeonhardt opened this issue Dec 18, 2019 · 1 comment · Fixed by #196

Comments

@TylerLeonhardt
Copy link
Collaborator

TylerLeonhardt commented Dec 18, 2019

In the AbsoluteUriConverter class, we use Uri.ToString().

Unfortunately, the Uri.ToString() has bizarre behavior with Uris with escaped non-ASCII characters.

> new Uri("/Users/tyleonha/Code/PowerShell/Misc/测试.ps1").ToString()
file:///Users/tyleonha/Code/PowerShell/Misc/%E6%B5%8B%E8%AF%95.ps1/Users/tyleonha/Code/PowerShell/Misc/测试.ps1

Notice the path is written twice?

This only seems to happen on non-Windows as adding a drive letter fixes it... but then it url decodes the characters:

> new Uri("file:///C:/Users/tyleonha/Code/PowerShell/Misc/%E6%B5%8B%E8%AF%95.ps1").ToString()
file:///C:/Users/tyleonha/Code/PowerShell/Misc/测试.ps1

But then again... since VS Code expects the colon to be escaped in the drive letter... we're still left in a bad state:

new Uri("file:///C%3A/Users/tyleonha/Code/PowerShell/Misc/%E6%B5%8B%E8%AF%95.ps1").ToString()
file://%2FC%3A%2FUsers%2Ftyleonha%2FCode%2FPowerShell%2FMisc%2F测试.ps1/C%3A/Users/tyleonha/Code/PowerShell/Misc/测试.ps1
@TylerLeonhardt TylerLeonhardt changed the title Don Don't rely on Uri.ToString() in Serializer Dec 18, 2019
@TylerLeonhardt
Copy link
Collaborator Author

TylerLeonhardt commented Dec 19, 2019

So I think something like this... but in C# should do the trick:

# Test cases
$uri = [uri]::new("C:\Users\tyleonha\Code\PowerShell\Misc\foo.ps1")
# or
$uri = [uri]::new("/Users/tyleonha/Code/PowerShell/Misc/测试.ps1")
# or
$uri = [uri]::new("file:///C%3A/Users/tyleonha/Code/PowerShell/Misc/%E6%B5%8B%E8%AF%95.ps1")
# or
$uri = [uri]::new("file:///C%3A/Users/tyleonha/Code/PowerShell/Misc/foo.ps1")
# or
$uri = [uri]::new("file:///C:/Users/tyleonha/Code/PowerShell/Misc/%E6%B5%8B%E8%AF%95.ps1")

$uri.Scheme + "://" + ($uri.Segments -join '')

These all work as expected - the characters are escaped if they should be.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant