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 <heschi@google.com>
Run-TryBot: Heschi Kreinick <heschi@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Heschi Kreinick 2021-04-20 14:27:24 -04:00
parent a8e7c0c9c2
commit f7e8e24497
3 changed files with 38 additions and 28 deletions

View File

@ -5,6 +5,7 @@
package codelens
import (
"fmt"
"runtime"
"strings"
"testing"
@ -103,7 +104,7 @@ var Goodbye error
-- go.mod --
module mod.com
go 1.12
go 1.14
require golang.org/x/hello v1.2.3
-- go.sum --
@ -121,7 +122,7 @@ func main() {
const wantGoMod = `module mod.com
go 1.12
go 1.14
require golang.org/x/hello v1.3.3
`
@ -159,20 +160,25 @@ require golang.org/x/hello v1.3.3
})
})
}
t.Run("Upgrade individual dependency", func(t *testing.T) {
WithOptions(ProxyFiles(proxyWithLatest)).Run(t, shouldUpdateDep, func(t *testing.T, env *Env) {
env.OpenFile("go.mod")
env.ExecuteCodeLensCommand("go.mod", command.CheckUpgrades)
d := &protocol.PublishDiagnosticsParams{}
env.Await(OnceMet(env.DiagnosticAtRegexpWithMessage("go.mod", `require`, "can be upgraded"),
ReadDiagnostics("go.mod", d)))
env.ApplyQuickFixes("go.mod", d.Diagnostics)
env.Await(env.DoneWithChangeWatchedFiles())
if got := env.Editor.BufferText("go.mod"); got != wantGoMod {
t.Fatalf("go.mod upgrade failed:\n%s", tests.Diff(t, wantGoMod, got))
}
for _, vendoring := range []bool{false, true} {
t.Run(fmt.Sprintf("Upgrade individual dependency vendoring=%v", vendoring), func(t *testing.T) {
WithOptions(ProxyFiles(proxyWithLatest)).Run(t, shouldUpdateDep, func(t *testing.T, env *Env) {
if vendoring {
env.RunGoCommand("mod", "vendor")
}
env.OpenFile("go.mod")
env.ExecuteCodeLensCommand("go.mod", command.CheckUpgrades)
d := &protocol.PublishDiagnosticsParams{}
env.Await(OnceMet(env.DiagnosticAtRegexpWithMessage("go.mod", `require`, "can be upgraded"),
ReadDiagnostics("go.mod", d)))
env.ApplyQuickFixes("go.mod", d.Diagnostics)
env.Await(env.DoneWithChangeWatchedFiles())
if got := env.Editor.BufferText("go.mod"); got != wantGoMod {
t.Fatalf("go.mod upgrade failed:\n%s", tests.Diff(t, wantGoMod, got))
}
})
})
})
}
}
func TestUnusedDependenciesCodelens(t *testing.T) {

View File

@ -351,22 +351,25 @@ func (s *snapshot) goCommandInvocation(ctx context.Context, flags source.Invocat
return "", nil, cleanup, err
}
mutableModFlag := ""
if s.view.goversion >= 16 {
mutableModFlag = "mod"
}
// If the mod flag isn't set, populate it based on the mode and workspace.
if inv.ModFlag == "" {
mutableModFlag := ""
if s.view.goversion >= 16 {
mutableModFlag = "mod"
}
switch mode {
case source.LoadWorkspace, source.Normal:
if vendorEnabled {
inv.ModFlag = "vendor"
} else if !allowModfileModificationOption {
inv.ModFlag = "readonly"
} else {
switch mode {
case source.LoadWorkspace, source.Normal:
if vendorEnabled {
inv.ModFlag = "vendor"
} else if !allowModfileModificationOption {
inv.ModFlag = "readonly"
} else {
inv.ModFlag = mutableModFlag
}
case source.UpdateUserModFile, source.WriteTemporaryModFile:
inv.ModFlag = mutableModFlag
}
case source.UpdateUserModFile, source.WriteTemporaryModFile:
inv.ModFlag = mutableModFlag
}
wantTempMod := mode != source.UpdateUserModFile

View File

@ -584,6 +584,7 @@ func (s *Server) getUpgrades(ctx context.Context, snapshot source.Snapshot, uri
Verb: "list",
Args: append([]string{"-m", "-u", "-json"}, modules...),
WorkingDir: filepath.Dir(uri.Filename()),
ModFlag: "readonly",
})
if err != nil {
return nil, err