Skip to content

Commit b22f048

Browse files
committed
internal/jsonrpc2*: remove usage of golang.org/x/xerrors
As of golang/go#50827, gopls no longer needs to build at Go 1.12. This was the only reason to continue using xerrors in the jsonrpc2 libraries. Remove this usage as a step toward eliminating the xerrors dependency from x/tools. For golang/go#52442 Change-Id: I1a0a1bbf57e6606c66b99b27439adf6f65c26a60 Reviewed-on: https://go-review.googlesource.com/c/tools/+/401155 Run-TryBot: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent bcfc38f commit b22f048

File tree

7 files changed

+37
-43
lines changed

7 files changed

+37
-43
lines changed

internal/jsonrpc2/messages.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@ package jsonrpc2
66

77
import (
88
"encoding/json"
9+
"errors"
910
"fmt"
10-
11-
errors "golang.org/x/xerrors"
1211
)
1312

1413
// Message is the interface to all jsonrpc2 message types.

internal/jsonrpc2/serve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ package jsonrpc2
66

77
import (
88
"context"
9+
"errors"
910
"io"
1011
"net"
1112
"os"
1213
"time"
1314

1415
"golang.org/x/tools/internal/event"
15-
errors "golang.org/x/xerrors"
1616
)
1717

1818
// NOTE: This file provides an experimental API for serving multiple remote

internal/jsonrpc2_v2/conn.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"golang.org/x/tools/internal/event"
1515
"golang.org/x/tools/internal/event/label"
1616
"golang.org/x/tools/internal/lsp/debug/tag"
17-
errors "golang.org/x/xerrors"
1817
)
1918

2019
// Binder builds a connection configuration.
@@ -126,7 +125,7 @@ func newConnection(ctx context.Context, rwc io.ReadWriteCloser, binder Binder) (
126125
func (c *Connection) Notify(ctx context.Context, method string, params interface{}) error {
127126
notify, err := NewNotification(method, params)
128127
if err != nil {
129-
return errors.Errorf("marshaling notify parameters: %v", err)
128+
return fmt.Errorf("marshaling notify parameters: %v", err)
130129
}
131130
ctx, done := event.Start(ctx, method,
132131
tag.Method.Of(method),
@@ -158,7 +157,7 @@ func (c *Connection) Call(ctx context.Context, method string, params interface{}
158157
call, err := NewCall(result.id, method, params)
159158
if err != nil {
160159
//set the result to failed
161-
result.resultBox <- asyncResult{err: errors.Errorf("marshaling call parameters: %w", err)}
160+
result.resultBox <- asyncResult{err: fmt.Errorf("marshaling call parameters: %w", err)}
162161
return result
163162
}
164163
ctx, endSpan := event.Start(ctx, method,
@@ -408,7 +407,7 @@ func (c *Connection) deliverMessages(ctx context.Context, handler Handler, fromQ
408407
switch {
409408
case rerr == ErrNotHandled:
410409
// message not handled, report it back to the caller as an error
411-
c.reply(entry, nil, errors.Errorf("%w: %q", ErrMethodNotFound, entry.request.Method))
410+
c.reply(entry, nil, fmt.Errorf("%w: %q", ErrMethodNotFound, entry.request.Method))
412411
case rerr == ErrAsyncResponse:
413412
// message handled but the response will come later
414413
default:
@@ -440,7 +439,7 @@ func (c *Connection) respond(entry *incoming, result interface{}, rerr error) er
440439
// send the response
441440
if result == nil && rerr == nil {
442441
// call with no response, send an error anyway
443-
rerr = errors.Errorf("%w: %q produced no response", ErrInternal, entry.request.Method)
442+
rerr = fmt.Errorf("%w: %q produced no response", ErrInternal, entry.request.Method)
444443
}
445444
var response *Response
446445
response, err = NewResponse(entry.request.ID, result, rerr)
@@ -452,11 +451,11 @@ func (c *Connection) respond(entry *incoming, result interface{}, rerr error) er
452451
switch {
453452
case rerr != nil:
454453
// notification failed
455-
err = errors.Errorf("%w: %q notification failed: %v", ErrInternal, entry.request.Method, rerr)
454+
err = fmt.Errorf("%w: %q notification failed: %v", ErrInternal, entry.request.Method, rerr)
456455
rerr = nil
457456
case result != nil:
458457
//notification produced a response, which is an error
459-
err = errors.Errorf("%w: %q produced unwanted response", ErrInternal, entry.request.Method)
458+
err = fmt.Errorf("%w: %q produced unwanted response", ErrInternal, entry.request.Method)
460459
default:
461460
// normal notification finish
462461
}

internal/jsonrpc2_v2/frame.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"io"
1313
"strconv"
1414
"strings"
15-
16-
errors "golang.org/x/xerrors"
1715
)
1816

1917
// Reader abstracts the transport mechanics from the JSON RPC protocol.
@@ -87,7 +85,7 @@ func (w *rawWriter) Write(ctx context.Context, msg Message) (int64, error) {
8785
}
8886
data, err := EncodeMessage(msg)
8987
if err != nil {
90-
return 0, errors.Errorf("marshaling message: %v", err)
88+
return 0, fmt.Errorf("marshaling message: %v", err)
9189
}
9290
n, err := w.out.Write(data)
9391
return int64(n), err
@@ -122,7 +120,7 @@ func (r *headerReader) Read(ctx context.Context) (Message, int64, error) {
122120
line, err := r.in.ReadString('\n')
123121
total += int64(len(line))
124122
if err != nil {
125-
return nil, total, errors.Errorf("failed reading header line: %w", err)
123+
return nil, total, fmt.Errorf("failed reading header line: %w", err)
126124
}
127125
line = strings.TrimSpace(line)
128126
// check we have a header line
@@ -131,23 +129,23 @@ func (r *headerReader) Read(ctx context.Context) (Message, int64, error) {
131129
}
132130
colon := strings.IndexRune(line, ':')
133131
if colon < 0 {
134-
return nil, total, errors.Errorf("invalid header line %q", line)
132+
return nil, total, fmt.Errorf("invalid header line %q", line)
135133
}
136134
name, value := line[:colon], strings.TrimSpace(line[colon+1:])
137135
switch name {
138136
case "Content-Length":
139137
if length, err = strconv.ParseInt(value, 10, 32); err != nil {
140-
return nil, total, errors.Errorf("failed parsing Content-Length: %v", value)
138+
return nil, total, fmt.Errorf("failed parsing Content-Length: %v", value)
141139
}
142140
if length <= 0 {
143-
return nil, total, errors.Errorf("invalid Content-Length: %v", length)
141+
return nil, total, fmt.Errorf("invalid Content-Length: %v", length)
144142
}
145143
default:
146144
// ignoring unknown headers
147145
}
148146
}
149147
if length == 0 {
150-
return nil, total, errors.Errorf("missing Content-Length header")
148+
return nil, total, fmt.Errorf("missing Content-Length header")
151149
}
152150
data := make([]byte, length)
153151
n, err := io.ReadFull(r.in, data)
@@ -167,7 +165,7 @@ func (w *headerWriter) Write(ctx context.Context, msg Message) (int64, error) {
167165
}
168166
data, err := EncodeMessage(msg)
169167
if err != nil {
170-
return 0, errors.Errorf("marshaling message: %v", err)
168+
return 0, fmt.Errorf("marshaling message: %v", err)
171169
}
172170
n, err := fmt.Fprintf(w.out, "Content-Length: %v\r\n\r\n", len(data))
173171
total := int64(n)

internal/jsonrpc2_v2/jsonrpc2_test.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"golang.org/x/tools/internal/event/export/eventtest"
1717
jsonrpc2 "golang.org/x/tools/internal/jsonrpc2_v2"
1818
"golang.org/x/tools/internal/stack/stacktest"
19-
errors "golang.org/x/xerrors"
2019
)
2120

2221
var callTests = []invoker{
@@ -288,19 +287,19 @@ func (h *handler) Preempt(ctx context.Context, req *jsonrpc2.Request) (interface
288287
case "unblock":
289288
var name string
290289
if err := json.Unmarshal(req.Params, &name); err != nil {
291-
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
290+
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
292291
}
293292
close(h.waiter(name))
294293
return nil, nil
295294
case "peek":
296295
if len(req.Params) > 0 {
297-
return nil, errors.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams)
296+
return nil, fmt.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams)
298297
}
299298
return h.accumulator, nil
300299
case "cancel":
301300
var params cancelParams
302301
if err := json.Unmarshal(req.Params, &params); err != nil {
303-
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
302+
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
304303
}
305304
h.conn.Cancel(jsonrpc2.Int64ID(params.ID))
306305
return nil, nil
@@ -313,71 +312,71 @@ func (h *handler) Handle(ctx context.Context, req *jsonrpc2.Request) (interface{
313312
switch req.Method {
314313
case "no_args":
315314
if len(req.Params) > 0 {
316-
return nil, errors.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams)
315+
return nil, fmt.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams)
317316
}
318317
return true, nil
319318
case "one_string":
320319
var v string
321320
if err := json.Unmarshal(req.Params, &v); err != nil {
322-
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
321+
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
323322
}
324323
return "got:" + v, nil
325324
case "one_number":
326325
var v int
327326
if err := json.Unmarshal(req.Params, &v); err != nil {
328-
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
327+
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
329328
}
330329
return fmt.Sprintf("got:%d", v), nil
331330
case "set":
332331
var v int
333332
if err := json.Unmarshal(req.Params, &v); err != nil {
334-
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
333+
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
335334
}
336335
h.accumulator = v
337336
return nil, nil
338337
case "add":
339338
var v int
340339
if err := json.Unmarshal(req.Params, &v); err != nil {
341-
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
340+
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
342341
}
343342
h.accumulator += v
344343
return nil, nil
345344
case "get":
346345
if len(req.Params) > 0 {
347-
return nil, errors.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams)
346+
return nil, fmt.Errorf("%w: expected no params", jsonrpc2.ErrInvalidParams)
348347
}
349348
return h.accumulator, nil
350349
case "join":
351350
var v []string
352351
if err := json.Unmarshal(req.Params, &v); err != nil {
353-
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
352+
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
354353
}
355354
return path.Join(v...), nil
356355
case "echo":
357356
var v []interface{}
358357
if err := json.Unmarshal(req.Params, &v); err != nil {
359-
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
358+
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
360359
}
361360
var result interface{}
362361
err := h.conn.Call(ctx, v[0].(string), v[1]).Await(ctx, &result)
363362
return result, err
364363
case "wait":
365364
var name string
366365
if err := json.Unmarshal(req.Params, &name); err != nil {
367-
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
366+
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
368367
}
369368
select {
370369
case <-h.waiter(name):
371370
return true, nil
372371
case <-ctx.Done():
373372
return nil, ctx.Err()
374373
case <-time.After(time.Second):
375-
return nil, errors.Errorf("wait for %q timed out", name)
374+
return nil, fmt.Errorf("wait for %q timed out", name)
376375
}
377376
case "fork":
378377
var name string
379378
if err := json.Unmarshal(req.Params, &name); err != nil {
380-
return nil, errors.Errorf("%w: %s", jsonrpc2.ErrParse, err)
379+
return nil, fmt.Errorf("%w: %s", jsonrpc2.ErrParse, err)
381380
}
382381
waitFor := h.waiter(name)
383382
go func() {
@@ -387,7 +386,7 @@ func (h *handler) Handle(ctx context.Context, req *jsonrpc2.Request) (interface{
387386
case <-ctx.Done():
388387
h.conn.Respond(req.ID, nil, ctx.Err())
389388
case <-time.After(time.Second):
390-
h.conn.Respond(req.ID, nil, errors.Errorf("wait for %q timed out", name))
389+
h.conn.Respond(req.ID, nil, fmt.Errorf("wait for %q timed out", name))
391390
}
392391
}()
393392
return nil, jsonrpc2.ErrAsyncResponse

internal/jsonrpc2_v2/messages.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ package jsonrpc2
66

77
import (
88
"encoding/json"
9-
10-
errors "golang.org/x/xerrors"
9+
"errors"
10+
"fmt"
1111
)
1212

1313
// ID is a Request identifier.
@@ -120,18 +120,18 @@ func EncodeMessage(msg Message) ([]byte, error) {
120120
msg.marshal(&wire)
121121
data, err := json.Marshal(&wire)
122122
if err != nil {
123-
return data, errors.Errorf("marshaling jsonrpc message: %w", err)
123+
return data, fmt.Errorf("marshaling jsonrpc message: %w", err)
124124
}
125125
return data, nil
126126
}
127127

128128
func DecodeMessage(data []byte) (Message, error) {
129129
msg := wireCombined{}
130130
if err := json.Unmarshal(data, &msg); err != nil {
131-
return nil, errors.Errorf("unmarshaling jsonrpc message: %w", err)
131+
return nil, fmt.Errorf("unmarshaling jsonrpc message: %w", err)
132132
}
133133
if msg.VersionTag != wireVersion {
134-
return nil, errors.Errorf("invalid message version tag %s expected %s", msg.VersionTag, wireVersion)
134+
return nil, fmt.Errorf("invalid message version tag %s expected %s", msg.VersionTag, wireVersion)
135135
}
136136
id := ID{}
137137
switch v := msg.ID.(type) {
@@ -144,7 +144,7 @@ func DecodeMessage(data []byte) (Message, error) {
144144
case string:
145145
id = StringID(v)
146146
default:
147-
return nil, errors.Errorf("invalid message id type <%T>%v", v, v)
147+
return nil, fmt.Errorf("invalid message id type <%T>%v", v, v)
148148
}
149149
if msg.Method != "" {
150150
// has a method, must be a call

internal/jsonrpc2_v2/serve.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ package jsonrpc2
66

77
import (
88
"context"
9+
"errors"
910
"io"
1011
"runtime"
1112
"strings"
1213
"sync"
1314
"syscall"
1415
"time"
15-
16-
errors "golang.org/x/xerrors"
1716
)
1817

1918
// Listener is implemented by protocols to accept new inbound connections.

0 commit comments

Comments
 (0)