Skip to content

Commit 0eded08

Browse files
committed
internal/span, internal/lsp: fix URI escaping
We had previously worked around a VS Code URI bug by unescaping URIs. This is incorrect, so stop doing it and then add a specific workaround just for that one bug. Fixes golang/go#36999 Change-Id: I92f1a5f71749af7a6b1020eee1272586515f7084 Reviewed-on: https://go-review.googlesource.com/c/tools/+/217599 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> (cherry picked from commit 35ac94b) Reviewed-on: https://go-review.googlesource.com/c/tools/+/217639
1 parent e700b84 commit 0eded08

File tree

5 files changed

+46
-20
lines changed

5 files changed

+46
-20
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package percent
2+
3+
import (
4+
)
5+
6+
func _() {
7+
var x int //@diag("x", "compiler", "x declared but not used")
8+
}

internal/lsp/testdata/summary.txt.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ DeepCompletionsCount = 5
66
FuzzyCompletionsCount = 8
77
RankedCompletionsCount = 66
88
CaseSensitiveCompletionsCount = 4
9-
DiagnosticsCount = 37
9+
DiagnosticsCount = 38
1010
FoldingRangesCount = 2
1111
FormatCount = 6
1212
ImportCount = 7

internal/span/uri.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,26 @@ func filename(uri URI) (string, error) {
4646
if isWindowsDriveURIPath(u.Path) {
4747
u.Path = strings.ToUpper(string(u.Path[1])) + u.Path[2:]
4848
}
49-
5049
return u.Path, nil
5150
}
5251

5352
// NewURI returns a span URI for the string.
5453
// It will attempt to detect if the string is a file path or uri.
5554
func NewURI(s string) URI {
56-
if u, err := url.PathUnescape(s); err == nil {
57-
s = u
58-
}
5955
// If a path has a scheme, it is already a URI.
6056
// We only handle the file:// scheme.
61-
if strings.HasPrefix(s, fileScheme+"://") {
57+
if i := len(fileScheme + "://"); strings.HasPrefix(s, "file:///") {
58+
// Handle microsoft/vscode#75027 by making it a special case.
59+
// On Windows, VS Code sends file URIs that look like file:///C%3A/x/y/z.
60+
// Replace the %3A so that the URI looks like: file:///C:/x/y/z.
61+
if strings.ToLower(s[i+2:i+5]) == "%3a" {
62+
s = s[:i+2] + ":" + s[i+5:]
63+
}
6264
// File URIs from Windows may have lowercase drive letters.
6365
// Since drive letters are guaranteed to be case insensitive,
6466
// we change them to uppercase to remain consistent.
6567
// For example, file:///c:/x/y/z becomes file:///C:/x/y/z.
66-
if i := len(fileScheme + "://"); isWindowsDriveURIPath(s[i:]) {
68+
if isWindowsDriveURIPath(s[i:]) {
6769
s = s[:i+1] + strings.ToUpper(string(s[i+1])) + s[i+2:]
6870
}
6971
return URI(s)
@@ -136,11 +138,7 @@ func FileURI(path string) URI {
136138
Scheme: fileScheme,
137139
Path: path,
138140
}
139-
uri := u.String()
140-
if unescaped, err := url.PathUnescape(uri); err == nil {
141-
uri = unescaped
142-
}
143-
return URI(uri)
141+
return URI(u.String())
144142
}
145143

146144
// isWindowsDrivePath returns true if the file path is of the form used by

internal/span/uri_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,31 @@ func TestURI(t *testing.T) {
5454
{
5555
path: `c:/Go/src/bob george/george/george.go`,
5656
wantFile: `C:/Go/src/bob george/george/george.go`,
57-
wantURI: span.URI("file:///C:/Go/src/bob george/george/george.go"),
57+
wantURI: span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
5858
},
5959
{
60-
path: `file:///c:/Go/src/bob george/george/george.go`,
60+
path: `file:///c:/Go/src/bob%20george/george/george.go`,
6161
wantFile: `C:/Go/src/bob george/george/george.go`,
62-
wantURI: span.URI("file:///C:/Go/src/bob george/george/george.go"),
62+
wantURI: span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
63+
},
64+
{
65+
path: `file:///C%3A/Go/src/bob%20george/george/george.go`,
66+
wantFile: `C:/Go/src/bob george/george/george.go`,
67+
wantURI: span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
68+
},
69+
{
70+
path: `file:///path/to/%25p%25ercent%25/per%25cent.go`,
71+
wantFile: `/path/to/%p%ercent%/per%cent.go`,
72+
wantURI: span.URI(`file:///path/to/%25p%25ercent%25/per%25cent.go`),
6373
},
6474
} {
6575
got := span.NewURI(test.path)
6676
if got != test.wantURI {
67-
t.Errorf("ToURI: got %s, expected %s", got, test.wantURI)
77+
t.Errorf("NewURI(%q): got %q, expected %q", test.path, got, test.wantURI)
6878
}
6979
gotFilename := got.Filename()
7080
if gotFilename != test.wantFile {
71-
t.Errorf("Filename: got %s, expected %s", gotFilename, test.wantFile)
81+
t.Errorf("Filename(%q): got %q, expected %q", got, gotFilename, test.wantFile)
7282
}
7383
}
7484
}

internal/span/uri_windows_test.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,22 @@ func TestURI(t *testing.T) {
5454
{
5555
path: `c:\Go\src\bob george\george\george.go`,
5656
wantFile: `C:\Go\src\bob george\george\george.go`,
57-
wantURI: span.URI("file:///C:/Go/src/bob george/george/george.go"),
57+
wantURI: span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
5858
},
5959
{
60-
path: `file:///c:/Go/src/bob george/george/george.go`,
60+
path: `file:///c:/Go/src/bob%20george/george/george.go`,
6161
wantFile: `C:\Go\src\bob george\george\george.go`,
62-
wantURI: span.URI("file:///C:/Go/src/bob george/george/george.go"),
62+
wantURI: span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
63+
},
64+
{
65+
path: `file:///C%3A/Go/src/bob%20george/george/george.go`,
66+
wantFile: `C:\Go\src\bob george\george\george.go`,
67+
wantURI: span.URI("file:///C:/Go/src/bob%20george/george/george.go"),
68+
},
69+
{
70+
path: `file:///c:/path/to/%25p%25ercent%25/per%25cent.go`,
71+
wantFile: `C:\path\to\%p%ercent%\per%cent.go`,
72+
wantURI: span.URI(`file:///C:/path/to/%25p%25ercent%25/per%25cent.go`),
6373
},
6474
} {
6575
got := span.NewURI(test.path)

0 commit comments

Comments
 (0)