From 64a767472a741f7b917ed1f5a3f8f06687ecdb03 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Mon, 16 Sep 2019 10:49:42 -0400 Subject: [PATCH] internal/span: handle escaping file URIs I wasn't sure if we should just manually construct the URI here or use the URL unescaping function. Let me know which you think is best. Updates golang/go#34270 Change-Id: Idb48fc2650d39f3e54cac141a70f356c31e303ad Reviewed-on: https://go-review.googlesource.com/c/tools/+/195618 Run-TryBot: Rebecca Stambler Reviewed-by: Ian Cottrell --- internal/lsp/source/util.go | 4 ++-- internal/span/uri.go | 6 +++++- internal/span/uri_test.go | 1 + 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/internal/lsp/source/util.go b/internal/lsp/source/util.go index 7feba2f36b3..194159874da 100644 --- a/internal/lsp/source/util.go +++ b/internal/lsp/source/util.go @@ -86,7 +86,7 @@ func fileToMapper(ctx context.Context, view View, uri span.URI) (*ast.File, []Pa if err != nil { return nil, nil, nil, err } - file, m, err := pkgToMapper(ctx, view, pkg, uri) + file, m, err := pkgToMapper(ctx, view, pkg, f.URI()) if err != nil { return nil, nil, nil, err } @@ -109,7 +109,7 @@ func cachedFileToMapper(ctx context.Context, view View, uri span.URI) (*ast.File if err != nil { return nil, nil, err } - file, m, err := pkgToMapper(ctx, view, pkg, uri) + file, m, err := pkgToMapper(ctx, view, pkg, f.URI()) if err != nil { return nil, nil, err } diff --git a/internal/span/uri.go b/internal/span/uri.go index 9dfbdaad9f4..e05a9e6ef5d 100644 --- a/internal/span/uri.go +++ b/internal/span/uri.go @@ -124,7 +124,11 @@ func FileURI(path string) URI { Scheme: fileScheme, Path: path, } - return URI(u.String()) + uri := u.String() + if unescaped, err := url.PathUnescape(uri); err == nil { + uri = unescaped + } + return URI(uri) } // isWindowsDrivePath returns true if the file path is of the form used by diff --git a/internal/span/uri_test.go b/internal/span/uri_test.go index 36dd1997e25..907b47bf48c 100644 --- a/internal/span/uri_test.go +++ b/internal/span/uri_test.go @@ -23,6 +23,7 @@ func TestURI(t *testing.T) { `c:/Go/src/bob.go`, `/path/to/dir`, `/a/b/c/src/bob.go`, + `c:/Go/src/bob george/george/george.go`, } { testPath := filepath.FromSlash(test) expectPath := testPath