Skip to content

Commit f7e8e24

Browse files
committed
internal/lsp: support Check For Upgrades in vendor mode
Essentially the same bug, and fix, as golang/go#38711. Fixes golang/go#44756. Change-Id: Ib4aaa73c2036e23f7afd7e48b685096039759ef9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/311909 Trust: Heschi Kreinick <[email protected]> Run-TryBot: Heschi Kreinick <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Rebecca Stambler <[email protected]>
1 parent a8e7c0c commit f7e8e24

File tree

3 files changed

+38
-28
lines changed

3 files changed

+38
-28
lines changed

gopls/internal/regtest/codelens/codelens_test.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package codelens
66

77
import (
8+
"fmt"
89
"runtime"
910
"strings"
1011
"testing"
@@ -103,7 +104,7 @@ var Goodbye error
103104
-- go.mod --
104105
module mod.com
105106
106-
go 1.12
107+
go 1.14
107108
108109
require golang.org/x/hello v1.2.3
109110
-- go.sum --
@@ -121,7 +122,7 @@ func main() {
121122

122123
const wantGoMod = `module mod.com
123124
124-
go 1.12
125+
go 1.14
125126
126127
require golang.org/x/hello v1.3.3
127128
`
@@ -159,20 +160,25 @@ require golang.org/x/hello v1.3.3
159160
})
160161
})
161162
}
162-
t.Run("Upgrade individual dependency", func(t *testing.T) {
163-
WithOptions(ProxyFiles(proxyWithLatest)).Run(t, shouldUpdateDep, func(t *testing.T, env *Env) {
164-
env.OpenFile("go.mod")
165-
env.ExecuteCodeLensCommand("go.mod", command.CheckUpgrades)
166-
d := &protocol.PublishDiagnosticsParams{}
167-
env.Await(OnceMet(env.DiagnosticAtRegexpWithMessage("go.mod", `require`, "can be upgraded"),
168-
ReadDiagnostics("go.mod", d)))
169-
env.ApplyQuickFixes("go.mod", d.Diagnostics)
170-
env.Await(env.DoneWithChangeWatchedFiles())
171-
if got := env.Editor.BufferText("go.mod"); got != wantGoMod {
172-
t.Fatalf("go.mod upgrade failed:\n%s", tests.Diff(t, wantGoMod, got))
173-
}
163+
for _, vendoring := range []bool{false, true} {
164+
t.Run(fmt.Sprintf("Upgrade individual dependency vendoring=%v", vendoring), func(t *testing.T) {
165+
WithOptions(ProxyFiles(proxyWithLatest)).Run(t, shouldUpdateDep, func(t *testing.T, env *Env) {
166+
if vendoring {
167+
env.RunGoCommand("mod", "vendor")
168+
}
169+
env.OpenFile("go.mod")
170+
env.ExecuteCodeLensCommand("go.mod", command.CheckUpgrades)
171+
d := &protocol.PublishDiagnosticsParams{}
172+
env.Await(OnceMet(env.DiagnosticAtRegexpWithMessage("go.mod", `require`, "can be upgraded"),
173+
ReadDiagnostics("go.mod", d)))
174+
env.ApplyQuickFixes("go.mod", d.Diagnostics)
175+
env.Await(env.DoneWithChangeWatchedFiles())
176+
if got := env.Editor.BufferText("go.mod"); got != wantGoMod {
177+
t.Fatalf("go.mod upgrade failed:\n%s", tests.Diff(t, wantGoMod, got))
178+
}
179+
})
174180
})
175-
})
181+
}
176182
}
177183

178184
func TestUnusedDependenciesCodelens(t *testing.T) {

internal/lsp/cache/snapshot.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -351,22 +351,25 @@ func (s *snapshot) goCommandInvocation(ctx context.Context, flags source.Invocat
351351
return "", nil, cleanup, err
352352
}
353353

354-
mutableModFlag := ""
355-
if s.view.goversion >= 16 {
356-
mutableModFlag = "mod"
357-
}
354+
// If the mod flag isn't set, populate it based on the mode and workspace.
355+
if inv.ModFlag == "" {
356+
mutableModFlag := ""
357+
if s.view.goversion >= 16 {
358+
mutableModFlag = "mod"
359+
}
358360

359-
switch mode {
360-
case source.LoadWorkspace, source.Normal:
361-
if vendorEnabled {
362-
inv.ModFlag = "vendor"
363-
} else if !allowModfileModificationOption {
364-
inv.ModFlag = "readonly"
365-
} else {
361+
switch mode {
362+
case source.LoadWorkspace, source.Normal:
363+
if vendorEnabled {
364+
inv.ModFlag = "vendor"
365+
} else if !allowModfileModificationOption {
366+
inv.ModFlag = "readonly"
367+
} else {
368+
inv.ModFlag = mutableModFlag
369+
}
370+
case source.UpdateUserModFile, source.WriteTemporaryModFile:
366371
inv.ModFlag = mutableModFlag
367372
}
368-
case source.UpdateUserModFile, source.WriteTemporaryModFile:
369-
inv.ModFlag = mutableModFlag
370373
}
371374

372375
wantTempMod := mode != source.UpdateUserModFile

internal/lsp/command.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ func (s *Server) getUpgrades(ctx context.Context, snapshot source.Snapshot, uri
584584
Verb: "list",
585585
Args: append([]string{"-m", "-u", "-json"}, modules...),
586586
WorkingDir: filepath.Dir(uri.Filename()),
587+
ModFlag: "readonly",
587588
})
588589
if err != nil {
589590
return nil, err

0 commit comments

Comments
 (0)