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 <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
Robert Findley 2022-03-10 12:45:22 -05:00
parent 613589de97
commit c773560c36
2 changed files with 58 additions and 0 deletions

View File

@ -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)

View File

@ -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")
}
}