cmd/mobile: add an arbitrary package to go-list at gomobile-init
go-list without a given module/package tries to analyze the module
at the current directory. If the current directory's module doesn't
have any Go files and is empty, go-list fails. This is the cause
of the problem that gomobile-init fails if the current directory
is such module.
This CL fixes this issue by giving an arbitrary standard library
when executing go-list.
Fixes golang/go#36668
Change-Id: If72d88081c94c2b9ef19d2870b41956a07102122
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/217700
Run-TryBot: Hajime Hoshi <hajimehoshi@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/cmd/gomobile/env.go b/cmd/gomobile/env.go
index 41c1543..868c114 100644
--- a/cmd/gomobile/env.go
+++ b/cmd/gomobile/env.go
@@ -76,7 +76,10 @@
func envInit() (err error) {
// Check the current Go version by go-list.
- cmd := exec.Command("go", "list", "-e", "-f", `{{range context.ReleaseTags}}{{if eq . "go1.14"}}{{.}}{{end}}{{end}}`)
+ // An arbitrary standard package ('runtime' here) is given to go-list.
+ // This is because go-list tries to analyze the module at the current directory if no packages are given,
+ // and if the module doesn't have any Go file, go-list fails. See golang/go#36668.
+ cmd := exec.Command("go", "list", "-e", "-f", `{{range context.ReleaseTags}}{{if eq . "go1.14"}}{{.}}{{end}}{{end}}`, "runtime")
cmd.Stderr = os.Stderr
out, err := cmd.Output()
if err != nil {
diff --git a/cmd/gomobile/init_test.go b/cmd/gomobile/init_test.go
index 2330f19..1849252 100644
--- a/cmd/gomobile/init_test.go
+++ b/cmd/gomobile/init_test.go
@@ -50,17 +50,67 @@
os.Setenv("HOMEDRIVE", "C:")
}
- if err := runInit(cmdInit); err != nil {
- t.Log(buf.String())
+ emptymod, err := ioutil.TempDir("", "gomobile-test")
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer os.RemoveAll(emptymod)
+
+ // Create go.mod, but without Go files.
+ f, err := os.Create(filepath.Join(emptymod, "go.mod"))
+ if err != nil {
+ t.Fatal(err)
+ }
+ defer f.Close()
+ if _, err := f.WriteString("module example.com/m\n"); err != nil {
+ t.Fatal(err)
+ }
+ if err := f.Sync(); err != nil {
t.Fatal(err)
}
- diff, err := diffOutput(buf.String(), initTmpl)
- if err != nil {
- t.Fatalf("computing diff failed: %v", err)
+ dirs := []struct {
+ dir string
+ name string
+ }{
+ {
+ dir: ".",
+ name: "current",
+ },
+ {
+ dir: emptymod,
+ name: "emptymod",
+ },
}
- if diff != "" {
- t.Errorf("unexpected output:\n%s", diff)
+ for _, dir := range dirs {
+ dir := dir
+ t.Run(dir.name, func(t *testing.T) {
+ wd, err := os.Getwd()
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := os.Chdir(dir.dir); err != nil {
+ t.Fatal(err)
+ }
+ defer os.Chdir(wd)
+
+ if err := runInit(cmdInit); err != nil {
+ t.Log(buf.String())
+ t.Fatal(err)
+ }
+
+ if dir.name == "emptymod" {
+ return
+ }
+
+ diff, err := diffOutput(buf.String(), initTmpl)
+ if err != nil {
+ t.Fatalf("computing diff failed: %v", err)
+ }
+ if diff != "" {
+ t.Errorf("unexpected output:\n%s", diff)
+ }
+ })
}
}