Skip to content

Commit 3f95539

Browse files
findleyrgopherbot
authored andcommitted
[gopls-release-branch.0.15] gopls/internal/server: set -mod=readonly when checking for upgrades
When a vendor directory is present, we must explicitly use -mod=readonly to query upgrades with `go list`. This was broken by the fixes for workspace vendoring, which removed the `-mod` flag in most usage of the gocommand package. Fixes golang/go#66055 Change-Id: I29efb617a8fe56e9752dc088dc5ea884f1cefb86 Reviewed-on: https://go-review.googlesource.com/c/tools/+/569877 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]> (cherry picked from commit c178933) Reviewed-on: https://go-review.googlesource.com/c/tools/+/569878 Reviewed-by: Nooras Saba‎ <[email protected]> Auto-Submit: Robert Findley <[email protected]>
1 parent 68630b3 commit 3f95539

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

gopls/internal/server/command.go

+1
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,7 @@ func (s *server) getUpgrades(ctx context.Context, snapshot *cache.Snapshot, uri
745745
stdout, err := snapshot.RunGoCommandDirect(ctx, cache.Normal|cache.AllowNetwork, &gocommand.Invocation{
746746
Verb: "list",
747747
Args: append([]string{"-m", "-u", "-json"}, modules...),
748+
ModFlag: "readonly", // necessary when vendor is present (golang/go#66055)
748749
WorkingDir: filepath.Dir(uri.Path()),
749750
})
750751
if err != nil {

gopls/internal/test/integration/codelens/codelens_test.go

+64-7
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,7 @@ const (
7373
}
7474
}
7575

76-
// This test confirms the full functionality of the code lenses for updating
77-
// dependencies in a go.mod file. It checks for the code lens that suggests
78-
// an update and then executes the command associated with that code lens. A
79-
// regression test for golang/go#39446. It also checks that these code lenses
80-
// only affect the diagnostics and contents of the containing go.mod file.
81-
func TestUpgradeCodelens(t *testing.T) {
82-
const proxyWithLatest = `
76+
const proxyWithLatest = `
8377
-- golang.org/x/[email protected]/go.mod --
8478
module golang.org/x/hello
8579
@@ -98,6 +92,13 @@ package hi
9892
var Goodbye error
9993
`
10094

95+
// This test confirms the full functionality of the code lenses for updating
96+
// dependencies in a go.mod file, when using a go.work file. It checks for the
97+
// code lens that suggests an update and then executes the command associated
98+
// with that code lens. A regression test for golang/go#39446. It also checks
99+
// that these code lenses only affect the diagnostics and contents of the
100+
// containing go.mod file.
101+
func TestUpgradeCodelens_Workspace(t *testing.T) {
101102
const shouldUpdateDep = `
102103
-- go.work --
103104
go 1.18
@@ -246,6 +247,62 @@ require golang.org/x/hello v1.2.3
246247
}
247248
}
248249

250+
func TestUpgradeCodelens_ModVendor(t *testing.T) {
251+
// This test checks the regression of golang/go#66055. The upgrade codelens
252+
// should work in a mod vendor context (the test above using a go.work file
253+
// was not broken).
254+
testenv.NeedsGo1Point(t, 22)
255+
const shouldUpdateDep = `
256+
-- go.mod --
257+
module mod.com/a
258+
259+
go 1.22
260+
261+
require golang.org/x/hello v1.2.3
262+
-- go.sum --
263+
golang.org/x/hello v1.2.3 h1:7Wesfkx/uBd+eFgPrq0irYj/1XfmbvLV8jZ/W7C2Dwg=
264+
golang.org/x/hello v1.2.3/go.mod h1:OgtlzsxVMUUdsdQCIDYgaauCTH47B8T8vofouNJfzgY=
265+
-- main.go --
266+
package main
267+
268+
import "golang.org/x/hello/hi"
269+
270+
func main() {
271+
_ = hi.Goodbye
272+
}
273+
`
274+
275+
const wantGoModA = `module mod.com/a
276+
277+
go 1.22
278+
279+
require golang.org/x/hello v1.3.3
280+
`
281+
282+
WithOptions(
283+
ProxyFiles(proxyWithLatest),
284+
).Run(t, shouldUpdateDep, func(t *testing.T, env *Env) {
285+
env.RunGoCommand("mod", "vendor")
286+
env.AfterChange()
287+
env.OpenFile("go.mod")
288+
289+
env.ExecuteCodeLensCommand("go.mod", command.CheckUpgrades, nil)
290+
d := &protocol.PublishDiagnosticsParams{}
291+
env.OnceMet(
292+
CompletedWork(server.DiagnosticWorkTitle(server.FromCheckUpgrades), 1, true),
293+
Diagnostics(env.AtRegexp("go.mod", `require`), WithMessage("can be upgraded")),
294+
ReadDiagnostics("go.mod", d),
295+
)
296+
297+
// Apply the diagnostics to a/go.mod.
298+
env.ApplyQuickFixes("go.mod", d.Diagnostics)
299+
env.AfterChange()
300+
if got := env.BufferText("go.mod"); got != wantGoModA {
301+
t.Fatalf("go.mod upgrade failed:\n%s", compare.Text(wantGoModA, got))
302+
}
303+
})
304+
}
305+
249306
func TestUnusedDependenciesCodelens(t *testing.T) {
250307
const proxy = `
251308
-- golang.org/x/[email protected]/go.mod --

internal/gocommand/invoke.go

+3
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,15 @@ type Invocation struct {
158158
BuildFlags []string
159159

160160
// If ModFlag is set, the go command is invoked with -mod=ModFlag.
161+
// TODO(rfindley): remove, in favor of Args.
161162
ModFlag string
162163

163164
// If ModFile is set, the go command is invoked with -modfile=ModFile.
165+
// TODO(rfindley): remove, in favor of Args.
164166
ModFile string
165167

166168
// If Overlay is set, the go command is invoked with -overlay=Overlay.
169+
// TODO(rfindley): remove, in favor of Args.
167170
Overlay string
168171

169172
// If CleanEnv is set, the invocation will run only with the environment

0 commit comments

Comments
 (0)