Skip to content

Commit 7b4c4ad

Browse files
committed
internal/lsp: correctly marshal arguments for upgrade code lens
I'm not sure how the regtest didn't catch this - is it possible that it could unmarshal a single string a slice of string? Either way, I'd like to get the fix in quicker - I'll try to add more regtests for this later. Also, validate the upgrade results more thoroughly. Change-Id: I812a3fecd9f0642a1408c0a9c0376bb98d50b397 Reviewed-on: https://go-review.googlesource.com/c/tools/+/245065 Run-TryBot: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com>
1 parent 59c6fc0 commit 7b4c4ad

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

internal/lsp/cache/mod.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,16 @@ func (s *snapshot) ModUpgradeHandle(ctx context.Context) (source.ModUpgradeHandl
350350
for _, upgrade := range upgradesList[1:] {
351351
// Example: "github.com/x/tools v1.1.0 [v1.2.0]"
352352
info := strings.Split(upgrade, " ")
353-
if len(info) < 3 {
353+
if len(info) != 3 {
354354
continue
355355
}
356356
dep, version := info[0], info[2]
357-
latest := version[1:] // remove the "["
358-
latest = strings.TrimSuffix(latest, "]") // remove the "]"
357+
358+
// Make sure that the format matches our expectation.
359+
if version[0] != '[' || version[len(version)-1] != ']' {
360+
continue
361+
}
362+
latest := version[1 : len(version)-1] // remove the "[" and "]"
359363
upgrades[dep] = latest
360364
}
361365
return &modUpgradeData{

internal/lsp/command.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,11 @@ func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCom
137137
return nil, err
138138
case source.CommandUpgradeDependency:
139139
var uri protocol.DocumentURI
140-
var deps []string
141-
if err := source.UnmarshalArgs(params.Arguments, &uri, &deps); err != nil {
140+
var goCmdArgs []string
141+
if err := source.UnmarshalArgs(params.Arguments, &uri, &goCmdArgs); err != nil {
142142
return nil, err
143143
}
144-
err := s.directGoModCommand(ctx, uri, "get", deps...)
144+
err := s.directGoModCommand(ctx, uri, "get", goCmdArgs...)
145145
return nil, err
146146
default:
147147
return nil, fmt.Errorf("unknown command: %s", params.Command)

internal/lsp/mod/code_lens.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func CodeLens(ctx context.Context, snapshot source.Snapshot, uri span.URI) ([]pr
5959
if err != nil {
6060
return nil, err
6161
}
62-
jsonArgs, err := source.MarshalArgs(uri, dep)
62+
jsonArgs, err := source.MarshalArgs(uri, []string{dep})
6363
if err != nil {
6464
return nil, err
6565
}

internal/lsp/regtest/codelens_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"golang.org/x/tools/internal/lsp/fake"
1111
"golang.org/x/tools/internal/lsp/protocol"
1212
"golang.org/x/tools/internal/lsp/source"
13+
"golang.org/x/tools/internal/lsp/tests"
1314
"golang.org/x/tools/internal/testenv"
1415
)
1516

@@ -100,16 +101,16 @@ func main() {
100101
`
101102
runner.Run(t, shouldUpdateDep, func(t *testing.T, env *Env) {
102103
env.OpenFile("go.mod")
103-
before := env.ReadWorkspaceFile("go.mod")
104104
lenses := env.CodeLens("go.mod")
105105
want := "Upgrade dependency to v1.3.3"
106-
var found *protocol.CodeLens
106+
var found protocol.CodeLens
107107
for _, lens := range lenses {
108108
if lens.Command.Title == want {
109-
found = &lens
109+
found = lens
110+
break
110111
}
111112
}
112-
if found == nil {
113+
if found.Command.Command == "" {
113114
t.Fatalf("did not find lens %q, got %v", want, lenses)
114115
}
115116
if _, err := env.Editor.Server.ExecuteCommand(env.Ctx, &protocol.ExecuteCommandParams{
@@ -118,9 +119,15 @@ func main() {
118119
}); err != nil {
119120
t.Fatal(err)
120121
}
121-
after := env.ReadWorkspaceFile("go.mod")
122-
if before == after {
123-
t.Fatalf("go.mod file was unchanged by upgrade command")
122+
got := env.ReadWorkspaceFile("go.mod")
123+
const wantGoMod = `module mod.com
124+
125+
go 1.14
126+
127+
require golang.org/x/hello v1.3.3
128+
`
129+
if got != wantGoMod {
130+
t.Fatalf("go.mod upgrade failed:\n%s", tests.Diff(wantGoMod, got))
124131
}
125132
}, WithProxyFiles(proxyWithLatest))
126133
}

0 commit comments

Comments
 (0)