Skip to content

Commit 6505601

Browse files
gabrittoCopilot
andauthored
Allow invalid UTF-8 (#2704)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent cfc9b72 commit 6505601

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1271
-689
lines changed

.golangci.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ linters:
7070
main:
7171
deny:
7272
- pkg: 'encoding/json$'
73-
desc: 'Use "github.com/go-json-experiment/json" instead.'
73+
desc: 'Use "github.com/microsoft/typescript-go/internal/json" instead.'
74+
- pkg: 'github.com/go-json-experiment/json'
75+
desc: 'Use "github.com/microsoft/typescript-go/internal/json" instead.'
7476

7577
forbidigo:
7678
analyze-types: true

internal/api/callbackfs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"time"
77

8-
"github.com/go-json-experiment/json"
8+
"github.com/microsoft/typescript-go/internal/json"
99
"github.com/microsoft/typescript-go/internal/vfs"
1010
)
1111

internal/api/conn.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ import (
44
"context"
55
"errors"
66

7-
"github.com/go-json-experiment/json"
8-
"github.com/go-json-experiment/json/jsontext"
7+
"github.com/microsoft/typescript-go/internal/json"
98
)
109

1110
var (
@@ -16,9 +15,9 @@ var (
1615
// Handler processes incoming API requests and notifications.
1716
type Handler interface {
1817
// HandleRequest handles an incoming request and returns a result or error.
19-
HandleRequest(ctx context.Context, method string, params jsontext.Value) (any, error)
18+
HandleRequest(ctx context.Context, method string, params json.Value) (any, error)
2019
// HandleNotification handles an incoming notification.
21-
HandleNotification(ctx context.Context, method string, params jsontext.Value) error
20+
HandleNotification(ctx context.Context, method string, params json.Value) error
2221
}
2322

2423
// Conn represents a bidirectional connection for API communication.
@@ -28,14 +27,14 @@ type Conn interface {
2827
Run(ctx context.Context) error
2928

3029
// Call sends a request to the client and waits for a response.
31-
Call(ctx context.Context, method string, params any) (jsontext.Value, error)
30+
Call(ctx context.Context, method string, params any) (json.Value, error)
3231

3332
// Notify sends a notification to the client (no response expected).
3433
Notify(ctx context.Context, method string, params any) error
3534
}
3635

3736
// UnmarshalParams is a helper to unmarshal params into a typed struct.
38-
func UnmarshalParams[T any](params jsontext.Value) (*T, error) {
37+
func UnmarshalParams[T any](params json.Value) (*T, error) {
3938
if len(params) == 0 {
4039
return nil, nil
4140
}

internal/api/conn_async.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"sync"
1010
"sync/atomic"
1111

12-
"github.com/go-json-experiment/json/jsontext"
12+
"github.com/microsoft/typescript-go/internal/json"
1313
"github.com/microsoft/typescript-go/internal/jsonrpc"
1414
)
1515

@@ -135,7 +135,7 @@ func (c *AsyncConn) handleNotification(ctx context.Context, msg *Message) {
135135
}
136136

137137
// Call sends a request to the client and waits for a response.
138-
func (c *AsyncConn) Call(ctx context.Context, method string, params any) (jsontext.Value, error) {
138+
func (c *AsyncConn) Call(ctx context.Context, method string, params any) (json.Value, error) {
139139
// Create unique request ID
140140
id := jsonrpc.NewIDString(fmt.Sprintf("api%d", c.seq.Add(1)))
141141

internal/api/conn_sync.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"runtime/debug"
99
"sync"
1010

11-
"github.com/go-json-experiment/json/jsontext"
11+
"github.com/microsoft/typescript-go/internal/json"
1212
"github.com/microsoft/typescript-go/internal/jsonrpc"
1313
)
1414

@@ -115,7 +115,7 @@ func (c *SyncConn) handleNotification(ctx context.Context, msg *Message) {
115115

116116
// Call sends a request to the client and waits for a response.
117117
// This method is safe to call from multiple goroutines - calls are serialized.
118-
func (c *SyncConn) Call(ctx context.Context, method string, params any) (jsontext.Value, error) {
118+
func (c *SyncConn) Call(ctx context.Context, method string, params any) (json.Value, error) {
119119
// Serialize all Call operations. This is critical because:
120120
// 1. The msgpack protocol uses method names as response IDs
121121
// 2. The handler code (project internals) may spawn goroutines that call

internal/api/proto.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ import (
66
"strconv"
77
"strings"
88

9-
"github.com/go-json-experiment/json"
10-
"github.com/go-json-experiment/json/jsontext"
119
"github.com/microsoft/typescript-go/internal/ast"
1210
"github.com/microsoft/typescript-go/internal/checker"
1311
"github.com/microsoft/typescript-go/internal/core"
12+
"github.com/microsoft/typescript-go/internal/json"
1413
"github.com/microsoft/typescript-go/internal/project"
1514
"github.com/microsoft/typescript-go/internal/tspath"
1615
)
@@ -214,7 +213,7 @@ type SourceFileResponse struct {
214213
Data string `json:"data"`
215214
}
216215

217-
func unmarshalPayload(method string, payload jsontext.Value) (any, error) {
216+
func unmarshalPayload(method string, payload json.Value) (any, error) {
218217
unmarshaler, ok := unmarshalers[Method(method)]
219218
if !ok {
220219
return nil, fmt.Errorf("unknown API method %q", method)

internal/api/protocol_jsonrpc.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ package api
33
import (
44
"io"
55

6-
"github.com/go-json-experiment/json"
7-
"github.com/go-json-experiment/json/jsontext"
6+
"github.com/microsoft/typescript-go/internal/json"
87
"github.com/microsoft/typescript-go/internal/jsonrpc"
98
)
109

@@ -47,7 +46,7 @@ func (p *JSONRPCProtocol) WriteRequest(id *jsonrpc.ID, method string, params any
4746
Method: method,
4847
Params: params,
4948
}
50-
data, err := json.Marshal(msg, jsontext.AllowInvalidUTF8(true))
49+
data, err := json.Marshal(msg)
5150
if err != nil {
5251
return err
5352
}
@@ -73,7 +72,7 @@ func (p *JSONRPCProtocol) WriteResponse(id *jsonrpc.ID, result any) error {
7372
ID: id,
7473
Result: result,
7574
}
76-
data, err := json.Marshal(msg, jsontext.AllowInvalidUTF8(true))
75+
data, err := json.Marshal(msg)
7776
if err != nil {
7877
return err
7978
}

internal/api/protocol_msgpack.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import (
66
"fmt"
77
"io"
88

9-
"github.com/go-json-experiment/json"
10-
"github.com/go-json-experiment/json/jsontext"
9+
"github.com/microsoft/typescript-go/internal/json"
1110
"github.com/microsoft/typescript-go/internal/jsonrpc"
1211
)
1312

@@ -183,7 +182,7 @@ func (p *MessagePackProtocol) readBin() ([]byte, error) {
183182
// WriteRequest implements Protocol.
184183
func (p *MessagePackProtocol) WriteRequest(id *jsonrpc.ID, method string, params any) error {
185184
// For msgpack protocol, requests from server are "Call" type
186-
payload, err := json.Marshal(params, jsontext.AllowInvalidUTF8(true))
185+
payload, err := json.Marshal(params)
187186
if err != nil {
188187
return err
189188
}
@@ -210,7 +209,7 @@ func (p *MessagePackProtocol) WriteResponse(id *jsonrpc.ID, result any) error {
210209
if raw, ok := result.(RawBinary); ok {
211210
payload = []byte(raw)
212211
} else {
213-
payload, err = json.Marshal(result, jsontext.AllowInvalidUTF8(true))
212+
payload, err = json.Marshal(result)
214213
if err != nil {
215214
return err
216215
}

internal/api/session.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
"sync"
88
"sync/atomic"
99

10-
"github.com/go-json-experiment/json/jsontext"
1110
"github.com/microsoft/typescript-go/internal/api/encoder"
1211
"github.com/microsoft/typescript-go/internal/ast"
1312
"github.com/microsoft/typescript-go/internal/astnav"
1413
"github.com/microsoft/typescript-go/internal/checker"
1514
"github.com/microsoft/typescript-go/internal/compiler"
15+
"github.com/microsoft/typescript-go/internal/json"
1616
"github.com/microsoft/typescript-go/internal/ls/lsconv"
1717
"github.com/microsoft/typescript-go/internal/project"
1818
"github.com/microsoft/typescript-go/internal/tsoptions"
@@ -93,7 +93,7 @@ func (s *Session) ensureSnapshot() {
9393
}
9494

9595
// HandleRequest implements Handler.
96-
func (s *Session) HandleRequest(ctx context.Context, method string, params jsontext.Value) (any, error) {
96+
func (s *Session) HandleRequest(ctx context.Context, method string, params json.Value) (any, error) {
9797
// Handle simple methods that don't need param parsing
9898
switch method {
9999
case "echo":
@@ -145,7 +145,7 @@ func (s *Session) HandleRequest(ctx context.Context, method string, params jsont
145145
}
146146

147147
// HandleNotification implements Handler.
148-
func (s *Session) HandleNotification(ctx context.Context, method string, params jsontext.Value) error {
148+
func (s *Session) HandleNotification(ctx context.Context, method string, params json.Value) error {
149149
// TODO: Implement notification handling
150150
return nil
151151
}

internal/collections/ordered_map.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import (
99
"slices"
1010
"strconv"
1111

12-
"github.com/go-json-experiment/json"
13-
"github.com/go-json-experiment/json/jsontext"
12+
"github.com/microsoft/typescript-go/internal/json"
1413
)
1514

1615
// OrderedMap is an insertion ordered map.
@@ -215,8 +214,8 @@ func (m *OrderedMap[K, V]) clone() OrderedMap[K, V] {
215214

216215
var _ json.MarshalerTo = (*OrderedMap[string, string])(nil)
217216

218-
func (m *OrderedMap[K, V]) MarshalJSONTo(enc *jsontext.Encoder) error {
219-
if err := enc.WriteToken(jsontext.BeginObject); err != nil {
217+
func (m *OrderedMap[K, V]) MarshalJSONTo(enc *json.Encoder) error {
218+
if err := enc.WriteToken(json.BeginObject); err != nil {
220219
return err
221220
}
222221

@@ -236,7 +235,7 @@ func (m *OrderedMap[K, V]) MarshalJSONTo(enc *jsontext.Encoder) error {
236235
}
237236
}
238237

239-
return enc.WriteToken(jsontext.EndObject)
238+
return enc.WriteToken(json.EndObject)
240239
}
241240

242241
func resolveKeyName(k reflect.Value) (string, error) {
@@ -261,22 +260,22 @@ func resolveKeyName(k reflect.Value) (string, error) {
261260

262261
var _ json.UnmarshalerFrom = (*OrderedMap[string, string])(nil)
263262

264-
func (m *OrderedMap[K, V]) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
263+
func (m *OrderedMap[K, V]) UnmarshalJSONFrom(dec *json.Decoder) error {
265264
token, err := dec.ReadToken()
266265
if err != nil {
267266
return err
268267
}
269-
if token.Kind() == 'n' { // jsontext.Null.Kind()
268+
if token.Kind() == 'n' { // json.Null.Kind()
270269
// By convention, to approximate the behavior of Unmarshal itself,
271270
// Unmarshalers implement UnmarshalJSON([]byte("null")) as a no-op.
272271
// https://pkg.go.dev/encoding/json#Unmarshaler
273272
// TODO: reconsider
274273
return nil
275274
}
276-
if token.Kind() != '{' { // jsontext.ObjectStart.Kind()
275+
if token.Kind() != '{' { // json.ObjectStart.Kind()
277276
return errors.New("cannot unmarshal non-object JSON value into Map")
278277
}
279-
for dec.PeekKind() != '}' { // jsontext.ObjectEnd.Kind()
278+
for dec.PeekKind() != '}' { // json.ObjectEnd.Kind()
280279
var key K
281280
var value V
282281
if err := json.UnmarshalDecode(dec, &key); err != nil {

0 commit comments

Comments
 (0)