Skip to content

Commit 78473fc

Browse files
committed
gopls/internal/golang/completion: return error if file is not new
The previous logic returned (nil, nil) in that case, without the necessary documentation, and the caller failed to handle that case, leading to a panic. Fixes golang/go#74057 Change-Id: I9f0c6e98457354189845954bab53a2b848401b01 Reviewed-on: https://go-review.googlesource.com/c/tools/+/680259 Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 499bea9 commit 78473fc

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

gopls/internal/golang/completion/newfile.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ import (
1616
"golang.org/x/tools/gopls/internal/protocol"
1717
)
1818

19-
// NewFile returns a document change to complete an empty go file.
19+
// NewFile returns a document change to complete an empty Go source file.
2020
func NewFile(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) (*protocol.DocumentChange, error) {
21-
if bs, err := fh.Content(); err != nil || len(bs) != 0 {
21+
content, err := fh.Content()
22+
if err != nil {
2223
return nil, err
2324
}
25+
if len(content) != 0 {
26+
return nil, fmt.Errorf("file is not empty")
27+
}
2428
meta, err := snapshot.NarrowestMetadataForFile(ctx, fh.URI())
2529
if err != nil {
2630
return nil, err

gopls/internal/server/workspace.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ func (s *server) DidCreateFiles(ctx context.Context, params *protocol.CreateFile
160160
case file.Go:
161161
change, err := completion.NewFile(ctx, snapshot, fh)
162162
if err != nil {
163+
// any error, including "it's not a new file"
163164
continue
164165
}
165166
allChanges = append(allChanges, *change)

0 commit comments

Comments
 (0)