From f7e8e24497d97453038e968e34eeba5f1164a085 Mon Sep 17 00:00:00 2001 From: Heschi Kreinick Date: Tue, 20 Apr 2021 14:27:24 -0400 Subject: [PATCH] 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 Run-TryBot: Heschi Kreinick gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Rebecca Stambler --- .../regtest/codelens/codelens_test.go | 36 +++++++++++-------- internal/lsp/cache/snapshot.go | 29 ++++++++------- internal/lsp/command.go | 1 + 3 files changed, 38 insertions(+), 28 deletions(-) diff --git a/gopls/internal/regtest/codelens/codelens_test.go b/gopls/internal/regtest/codelens/codelens_test.go index d745ced678..fe351ea529 100644 --- a/gopls/internal/regtest/codelens/codelens_test.go +++ b/gopls/internal/regtest/codelens/codelens_test.go @@ -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) { diff --git a/internal/lsp/cache/snapshot.go b/internal/lsp/cache/snapshot.go index 28d04491f5..b7f0bf60ae 100644 --- a/internal/lsp/cache/snapshot.go +++ b/internal/lsp/cache/snapshot.go @@ -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 diff --git a/internal/lsp/command.go b/internal/lsp/command.go index 24fd719b82..b1fbd77ba8 100644 --- a/internal/lsp/command.go +++ b/internal/lsp/command.go @@ -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