diff --git a/gopls/internal/regtest/modfile/modfile_test.go b/gopls/internal/regtest/modfile/modfile_test.go index 05b7ade294..868aa70aa5 100644 --- a/gopls/internal/regtest/modfile/modfile_test.go +++ b/gopls/internal/regtest/modfile/modfile_test.go @@ -239,6 +239,62 @@ require random.org v1.2.3 }) } +// Tests that multiple missing dependencies gives good single fixes. +func TestMissingDependencyFixesWithGoWork(t *testing.T) { + testenv.NeedsGo1Point(t, 18) + const mod = ` +-- go.work -- +go 1.18 + +use ( + ./a +) +-- a/go.mod -- +module mod.com + +go 1.12 + +-- a/main.go -- +package main + +import "example.com/blah" +import "random.org/blah" + +var _, _ = blah.Name, hello.Name +` + + const want = `module mod.com + +go 1.12 + +require random.org v1.2.3 +` + + RunMultiple{ + {"default", WithOptions(ProxyFiles(proxy), WorkspaceFolders("a"))}, + {"nested", WithOptions(ProxyFiles(proxy))}, + }.Run(t, mod, func(t *testing.T, env *Env) { + env.OpenFile("a/main.go") + var d protocol.PublishDiagnosticsParams + env.Await( + OnceMet( + env.DiagnosticAtRegexp("a/main.go", `"random.org/blah"`), + ReadDiagnostics("a/main.go", &d), + ), + ) + var randomDiag protocol.Diagnostic + for _, diag := range d.Diagnostics { + if strings.Contains(diag.Message, "random.org") { + randomDiag = diag + } + } + env.ApplyQuickFixes("a/main.go", []protocol.Diagnostic{randomDiag}) + if got := env.ReadWorkspaceFile("a/go.mod"); got != want { + t.Fatalf("unexpected go.mod content:\n%s", tests.Diff(t, want, got)) + } + }) +} + func TestIndirectDependencyFix(t *testing.T) { testenv.NeedsGo1Point(t, 14) diff --git a/internal/lsp/cache/snapshot.go b/internal/lsp/cache/snapshot.go index 1554fbea61..11ef0f2b1c 100644 --- a/internal/lsp/cache/snapshot.go +++ b/internal/lsp/cache/snapshot.go @@ -413,6 +413,8 @@ func (s *snapshot) goCommandInvocation(ctx context.Context, flags source.Invocat } case source.WriteTemporaryModFile: inv.ModFlag = mutableModFlag + // -mod must be readonly when using go.work files - see issue #48941 + inv.Env = append(inv.Env, "GOWORK=off") } }