Skip to content

Commit 88ba5d0

Browse files
committed
internal/imports: handle un-downloaded modules
Now that gopls is passing GOPROXY=off, running go list -m gives an error if any modules aren't downloaded. We need to pass -e to get results for the modules that we do have. Also add the missing error handling that resulted in silent failure. That, in turn, reveals that we need to explicitly ignore an expected error. Fixes golang/go#43333. Change-Id: I77604650b67a3c480d8231c65f0486f22e4a6722 Reviewed-on: https://go-review.googlesource.com/c/tools/+/283172 Trust: Heschi Kreinick <heschi@google.com> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
1 parent d33bae4 commit 88ba5d0

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

gopls/internal/regtest/completion_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,40 @@ func _() {
286286
)
287287
})
288288
}
289+
290+
// Test that completions still work with an undownloaded module, golang/go#43333.
291+
func TestUndownloadedModule(t *testing.T) {
292+
// mod.com depends on example.com, but only in a file that's hidden by a
293+
// build tag, so the IWL won't download example.com. That will cause errors
294+
// in the go list -m call performed by the imports package.
295+
const files = `
296+
-- go.mod --
297+
module mod.com
298+
299+
go 1.14
300+
301+
require example.com v1.2.3
302+
-- go.sum --
303+
example.com v1.2.3 h1:ihBTGWGjTU3V4ZJ9OmHITkU9WQ4lGdQkMjgyLFk0FaY=
304+
example.com v1.2.3/go.mod h1:Y2Rc5rVWjWur0h3pd9aEvK5Pof8YKDANh9gHA2Maujo=
305+
-- useblah.go --
306+
// +build hidden
307+
308+
package pkg
309+
import "example.com/blah"
310+
var _ = blah.Name
311+
-- mainmod/mainmod.go --
312+
package mainmod
313+
314+
const Name = "mainmod"
315+
`
316+
withOptions(ProxyFiles(proxy)).run(t, files, func(t *testing.T, env *Env) {
317+
env.CreateBuffer("import.go", "package pkg\nvar _ = mainmod.Name\n")
318+
env.SaveBuffer("import.go")
319+
content := env.ReadWorkspaceFile("import.go")
320+
if !strings.Contains(content, `import "mod.com/mainmod`) {
321+
t.Errorf("expected import of mod.com/mainmod in %q", content)
322+
}
323+
})
324+
325+
}

internal/imports/mod.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ func (r *ModuleResolver) init() error {
8686
r.modsByDir = []*gocommand.ModuleJSON{mainMod, r.dummyVendorMod}
8787
} else {
8888
// Vendor mode is off, so run go list -m ... to find everything.
89-
r.initAllMods()
89+
err := r.initAllMods()
90+
// We expect an error when running outside of a module with
91+
// GO111MODULE=on. Other errors are fatal.
92+
if err != nil && !strings.Contains(err.Error(), "working directory is not part of a module") {
93+
return err
94+
}
9095
}
9196

9297
if gmc := r.env.Env["GOMODCACHE"]; gmc != "" {
@@ -161,7 +166,7 @@ func (r *ModuleResolver) init() error {
161166
}
162167

163168
func (r *ModuleResolver) initAllMods() error {
164-
stdout, err := r.env.invokeGo(context.TODO(), "list", "-m", "-json", "...")
169+
stdout, err := r.env.invokeGo(context.TODO(), "list", "-m", "-e", "-json", "...")
165170
if err != nil {
166171
return err
167172
}

0 commit comments

Comments
 (0)