Skip to content

Commit

Permalink
internal/lsp/cache: set GOWORK=off when mutating modfiles
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
findleyr committed Mar 10, 2022
1 parent 613589d commit c773560
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
56 changes: 56 additions & 0 deletions gopls/internal/regtest/modfile/modfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions internal/lsp/cache/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down

0 comments on commit c773560

Please sign in to comment.