Skip to content

Commit

Permalink
internal/uri: account for VSCode's over-escaping of colon
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Feb 21, 2022
1 parent 14511b4 commit 8a8f24c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/uri/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,17 @@ func parseUri(rawUri string) (*url.URL, error) {
// as it is relevant in LSP (which uses the file scheme).
uri.Path = strings.TrimSuffix(uri.Path, "/")

// Upstream net/url parser (correctly) escapes only
// non-ASCII characters as per § 2.1 of RFC 3986.
// https://datatracker.ietf.org/doc/html/rfc3986#section-2.1
// Unfortunately VSCode effectively violates that section
// by escaping ASCII characters such as colon.
// See https://github.com/microsoft/vscode/issues/75027
//
// To account for this we reset RawPath which would
// otherwise be used by String() to effectively enforce
// clean re-escaping of the (unescaped) Path.
uri.RawPath = ""

return uri, nil
}
10 changes: 10 additions & 0 deletions internal/uri/uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func TestPathFromURI(t *testing.T) {
URI: "file:///C:/Users/Test/tf-test/",
ExpectedPath: `C:\Users\Test\tf-test`,
},
// Ensure over-escaped colon (which may come from VS Code) is normalized
{
URI: "file:///C%3A/Users/With%20Space/tf-test",
ExpectedPath: `C:\Users\With Space\tf-test`,
},
}
} else {
// unix
Expand Down Expand Up @@ -142,6 +147,11 @@ func TestMustParseURI(t *testing.T) {
RawURI: "file:///C:/Users/With%20Space/tf-test/file.tf",
ExpectedURI: "file:///C:/Users/With%20Space/tf-test/file.tf",
},
// Ensure over-escaped colon (which may come from VS Code) is normalized
{
RawURI: "file:///C%3A/Users/With%20Space/tf-test/file.tf",
ExpectedURI: "file:///C:/Users/With%20Space/tf-test/file.tf",
},
}

for i, tc := range testCases {
Expand Down

0 comments on commit 8a8f24c

Please sign in to comment.