From 0fb899f7f0554837f228c396d4c1b909ab5716a7 Mon Sep 17 00:00:00 2001 From: Josue Barros Date: Mon, 16 Nov 2026 14:16:24 -0300 Subject: [PATCH] Compare document highlight URIs by file path ProvideDocumentHighlights filtered results with a raw URI string compare. The server percent-encodes characters such as @ when it converts a file name back to a URI, while clients such as Neovim leave @ unescaped, so every highlight was dropped for paths containing it. Compare path keys instead, which normalizes escaping and respects the host case sensitivity. Add an LSP test that opens a file under a directory containing @ with an unescaped URI and expects the three highlights. Fixes #64758 --- tsc/internal/ls/documenthighlights.go | 5 +- .../lsp/server_documenthighlight_test.go | 80 +++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 tsc/internal/lsp/server_documenthighlight_test.go diff --git a/tsc/internal/ls/documenthighlights.go b/tsc/internal/ls/documenthighlights.go index 47fe0092c7e8b..1c9881ad287e3 100644 --- a/tsc/internal/ls/documenthighlights.go +++ b/tsc/internal/ls/documenthighlights.go @@ -26,8 +26,11 @@ func (l *LanguageService) ProvideDocumentHighlights(ctx context.Context, documen // Extract highlights for the current file only. var documentHighlights []*lsproto.DocumentHighlight if result.MultiDocumentHighlights != nil { + // Compare file paths, not raw URI strings: clients are not required to + // percent-encode the same characters we do. + documentPathKey := documentUri.PathKey(l.CaseSensitivity()) for _, mh := range *result.MultiDocumentHighlights { - if mh.Uri == documentUri { + if mh.Uri.PathKey(l.CaseSensitivity()) == documentPathKey { documentHighlights = append(documentHighlights, mh.Highlights...) } } diff --git a/tsc/internal/lsp/server_documenthighlight_test.go b/tsc/internal/lsp/server_documenthighlight_test.go new file mode 100644 index 0000000000000..29408242dd73a --- /dev/null +++ b/tsc/internal/lsp/server_documenthighlight_test.go @@ -0,0 +1,80 @@ +package lsp_test + +import ( + "context" + "io" + "testing" + + "github.com/microsoft/TypeScript/tsc/internal/bundled" + "github.com/microsoft/TypeScript/tsc/internal/lsp" + "github.com/microsoft/TypeScript/tsc/internal/lsp/lsproto" + "github.com/microsoft/TypeScript/tsc/internal/testutil/lsptestutil" + "github.com/microsoft/TypeScript/tsc/internal/tspath" + "github.com/microsoft/TypeScript/tsc/internal/vfs/vfstest" + "gotest.tools/v3/assert" +) + +// TestDocumentHighlightUnescapedAtSign reproduces #64758: clients are not +// required to percent-encode every character the server encodes, and Neovim +// sends an unescaped @. The server used to filter highlights with a raw URI +// string comparison, which dropped every result for such paths. +func TestDocumentHighlightUnescapedAtSign(t *testing.T) { + t.Parallel() + if !bundled.Embedded { + t.Skip("bundled files are not embedded") + } + + const file = "/home/projects/a@b/x.ts" + files := map[string]string{ + "/home/projects/tsconfig.json": `{}`, + file: "const foo = 1;\nconsole.log(foo);\nconsole.log(foo);\n", + } + fs := bundled.WrapFS(vfstest.FromMap(files, tspath.CaseInsensitive)) + + onServerRequest := func(_ context.Context, req *lsproto.RequestMessage) *lsproto.ResponseMessage { + switch req.Method { + case lsproto.MethodClientRegisterCapability, lsproto.MethodClientUnregisterCapability, lsproto.MethodWindowWorkDoneProgressCreate: + return &lsproto.ResponseMessage{ + ID: req.ID, + JSONRPC: req.JSONRPC, + Result: lsproto.Null{}, + } + default: + return nil + } + } + + client, closeClient := lsptestutil.NewLSPClient(t, lsp.ServerOptions{ + Err: io.Discard, + Cwd: "/home/projects", + FS: fs, + DefaultLibraryPath: bundled.LibPath(), + }, onServerRequest) + t.Cleanup(func() { _ = closeClient() }) + + initMsg, _, ok := client.SendRequest(t, lsproto.InitializeInfo, &lsproto.InitializeParams{ + Capabilities: &lsproto.ClientCapabilities{}, + }) + assert.Assert(t, ok && initMsg.AsResponse().Error == nil, "Initialize failed") + client.SendNotification(t, lsproto.InitializedInfo, &lsproto.InitializedParams{}) + <-client.Server.InitComplete() + + // Deliberately leave @ unescaped. + uri := lsproto.DocumentUri("file:///home/projects/a@b/x.ts") + client.SendNotification(t, lsproto.TextDocumentDidOpenInfo, &lsproto.DidOpenTextDocumentParams{ + TextDocument: &lsproto.TextDocumentItem{ + Uri: uri, + LanguageId: "typescript", + Text: files[file], + }, + }) + + msg, highlights, ok := client.SendRequest(t, lsproto.TextDocumentDocumentHighlightInfo, &lsproto.DocumentHighlightParams{ + TextDocument: lsproto.TextDocumentIdentifier{Uri: uri}, + Position: lsproto.Position{Line: 0, Character: 7}, + }) + assert.Assert(t, ok) + assert.Assert(t, msg.AsResponse().Error == nil) + assert.Assert(t, highlights.DocumentHighlights != nil) + assert.Equal(t, len(*highlights.DocumentHighlights), 3) +}