Skip to content

Commit 9eb3535

Browse files
committed
internal/lsp: 'go get' packages instead of modules
Previously, we were running `go get` only on modules, which led to us not downloading dependencies of packages. This resulted in further go.mod diagnostics that users could not resolve with quick fixes. Now, download packages directly so that dependencies are downloaded. Fixes golang/go#44307 Change-Id: Id764ea5a2f7028e238eadaaba0ca3cfc765b85b4 Reviewed-on: https://go-review.googlesource.com/c/tools/+/293729 Trust: Rebecca Stambler <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 67e49ef commit 9eb3535

File tree

2 files changed

+82
-6
lines changed

2 files changed

+82
-6
lines changed

gopls/internal/regtest/modfile/modfile_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,3 +1045,68 @@ example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo=
10451045
}
10461046
})
10471047
}
1048+
1049+
func TestDownloadDeps(t *testing.T) {
1050+
testenv.NeedsGo1Point(t, 14)
1051+
1052+
const proxy = `
1053+
-- [email protected]/go.mod --
1054+
module example.com
1055+
1056+
go 1.12
1057+
1058+
require random.org v1.2.3
1059+
-- [email protected]/blah/blah.go --
1060+
package blah
1061+
1062+
import "random.org/bye"
1063+
1064+
func SaySomething() {
1065+
bye.Goodbye()
1066+
}
1067+
-- [email protected]/go.mod --
1068+
module random.org
1069+
1070+
go 1.12
1071+
-- [email protected]/bye/bye.go --
1072+
package bye
1073+
1074+
func Goodbye() {
1075+
println("Bye")
1076+
}
1077+
`
1078+
1079+
const mod = `
1080+
-- go.mod --
1081+
module mod.com
1082+
1083+
go 1.12
1084+
-- go.sum --
1085+
-- main.go --
1086+
package main
1087+
1088+
import (
1089+
"example.com/blah"
1090+
)
1091+
1092+
func main() {
1093+
blah.SaySomething()
1094+
}
1095+
`
1096+
WithOptions(
1097+
ProxyFiles(proxy),
1098+
Modes(Singleton),
1099+
).Run(t, mod, func(t *testing.T, env *Env) {
1100+
env.OpenFile("main.go")
1101+
d := &protocol.PublishDiagnosticsParams{}
1102+
env.Await(
1103+
env.DiagnosticAtRegexpWithMessage("main.go", `"example.com/blah"`, `could not import example.com/blah (no required module provides package "example.com/blah")`),
1104+
ReadDiagnostics("main.go", d),
1105+
)
1106+
env.ApplyQuickFixes("main.go", d.Diagnostics)
1107+
env.Await(
1108+
EmptyDiagnostics("main.go"),
1109+
NoDiagnostics("go.mod"),
1110+
)
1111+
})
1112+
}

internal/lsp/command.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,13 @@ func (c *commandHandler) GoGetPackage(ctx context.Context, args command.GoGetPac
468468
}
469469
ver := strings.TrimSpace(stdout.String())
470470
return c.s.runGoModUpdateCommands(ctx, deps.snapshot, args.URI.SpanURI(), func(invoke func(...string) (*bytes.Buffer, error)) error {
471-
return runGoGetModule(invoke, args.AddRequire, []string{ver})
471+
if args.AddRequire {
472+
if err := addModuleRequire(invoke, []string{ver}); err != nil {
473+
return err
474+
}
475+
}
476+
_, err := invoke(append([]string{"get", "-d"}, args.Pkg)...)
477+
return err
472478
})
473479
})
474480
}
@@ -556,18 +562,23 @@ func applyFileEdits(ctx context.Context, snapshot source.Snapshot, uri span.URI,
556562

557563
func runGoGetModule(invoke func(...string) (*bytes.Buffer, error), addRequire bool, args []string) error {
558564
if addRequire {
559-
// Using go get to create a new dependency results in an
560-
// `// indirect` comment we may not want. The only way to avoid it
561-
// is to add the require as direct first. Then we can use go get to
562-
// update go.sum and tidy up.
563-
if _, err := invoke(append([]string{"mod", "edit", "-require"}, args...)...); err != nil {
565+
if err := addModuleRequire(invoke, args); err != nil {
564566
return err
565567
}
566568
}
567569
_, err := invoke(append([]string{"get", "-d"}, args...)...)
568570
return err
569571
}
570572

573+
func addModuleRequire(invoke func(...string) (*bytes.Buffer, error), args []string) error {
574+
// Using go get to create a new dependency results in an
575+
// `// indirect` comment we may not want. The only way to avoid it
576+
// is to add the require as direct first. Then we can use go get to
577+
// update go.sum and tidy up.
578+
_, err := invoke(append([]string{"mod", "edit", "-require"}, args...)...)
579+
return err
580+
}
581+
571582
func (s *Server) getUpgrades(ctx context.Context, snapshot source.Snapshot, uri span.URI, modules []string) (map[string]string, error) {
572583
stdout, err := snapshot.RunGoCommandDirect(ctx, source.Normal|source.AllowNetwork, &gocommand.Invocation{
573584
Verb: "list",

0 commit comments

Comments
 (0)