Skip to content

Commit c773560

Browse files
committed
internal/lsp/cache: set GOWORK=off when mutating modfiles
Commands will fail with -mod=mod when GOWORK is set. Explicitly set it to off. Fixes golang/go#48941 Change-Id: I39f9d95526ca6f3b0414ff273cecd443d69afa61 Reviewed-on: https://go-review.googlesource.com/c/tools/+/391518 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
1 parent 613589d commit c773560

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

gopls/internal/regtest/modfile/modfile_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,62 @@ require random.org v1.2.3
239239
})
240240
}
241241

242+
// Tests that multiple missing dependencies gives good single fixes.
243+
func TestMissingDependencyFixesWithGoWork(t *testing.T) {
244+
testenv.NeedsGo1Point(t, 18)
245+
const mod = `
246+
-- go.work --
247+
go 1.18
248+
249+
use (
250+
./a
251+
)
252+
-- a/go.mod --
253+
module mod.com
254+
255+
go 1.12
256+
257+
-- a/main.go --
258+
package main
259+
260+
import "example.com/blah"
261+
import "random.org/blah"
262+
263+
var _, _ = blah.Name, hello.Name
264+
`
265+
266+
const want = `module mod.com
267+
268+
go 1.12
269+
270+
require random.org v1.2.3
271+
`
272+
273+
RunMultiple{
274+
{"default", WithOptions(ProxyFiles(proxy), WorkspaceFolders("a"))},
275+
{"nested", WithOptions(ProxyFiles(proxy))},
276+
}.Run(t, mod, func(t *testing.T, env *Env) {
277+
env.OpenFile("a/main.go")
278+
var d protocol.PublishDiagnosticsParams
279+
env.Await(
280+
OnceMet(
281+
env.DiagnosticAtRegexp("a/main.go", `"random.org/blah"`),
282+
ReadDiagnostics("a/main.go", &d),
283+
),
284+
)
285+
var randomDiag protocol.Diagnostic
286+
for _, diag := range d.Diagnostics {
287+
if strings.Contains(diag.Message, "random.org") {
288+
randomDiag = diag
289+
}
290+
}
291+
env.ApplyQuickFixes("a/main.go", []protocol.Diagnostic{randomDiag})
292+
if got := env.ReadWorkspaceFile("a/go.mod"); got != want {
293+
t.Fatalf("unexpected go.mod content:\n%s", tests.Diff(t, want, got))
294+
}
295+
})
296+
}
297+
242298
func TestIndirectDependencyFix(t *testing.T) {
243299
testenv.NeedsGo1Point(t, 14)
244300

internal/lsp/cache/snapshot.go

+2
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,8 @@ func (s *snapshot) goCommandInvocation(ctx context.Context, flags source.Invocat
413413
}
414414
case source.WriteTemporaryModFile:
415415
inv.ModFlag = mutableModFlag
416+
// -mod must be readonly when using go.work files - see issue #48941
417+
inv.Env = append(inv.Env, "GOWORK=off")
416418
}
417419
}
418420

0 commit comments

Comments
 (0)