Skip to content

Commit 2b26a47

Browse files
committed
cmd/gomobile: handle modules replaced by other versioned modules
Previously, gomobile bind's go.mod generation logic assumed replacing module was always located in the disk, but is not always true. It's valid to replace a module(version) with another module&version. For example, replace golang.org/x/tools => ../ causes: { "Path": "golang.org/x/tools", "Version": "v0.0.0-20191017151554-a3bc800455d5", "Replace": { "Path": "../", "Dir": "/usr/local/google/home/hakim/go/src/golang.org/x/tools", "GoMod": "/usr/local/google/home/hakim/go/src/golang.org/x/tools/go.mod", "GoVersion": "1.11" }, "Dir": "/usr/local/google/home/hakim/go/src/golang.org/x/tools", "GoMod": "/usr/local/google/home/hakim/go/src/golang.org/x/tools/go.mod", "GoVersion": "1.11" } replace github.com/anacrolix/torrent v1.13.0 => gitlab.com/axet/torrent v0.0.0-20200205141541-92b4b9e7387e causes: { "Path": "github.com/anacrolix/torrent", "Version": "v1.13.0", "Replace": { "Path": "gitlab.com/axet/torrent", "Version": "v0.0.0-20200205141541-92b4b9e7387e", "Time": "2020-02-05T14:15:41Z", "Dir": "/usr/local/google/home/hakim/go/pkg/mod/gitlab.com/axet/[email protected]", "GoMod": "/usr/local/google/home/hakim/go/pkg/mod/cache/download/gitlab.com/axet/torrent/@v/v0.0.0-20200205141541-92b4b9e7387e.mod" }, "Dir": "/usr/local/google/home/hakim/go/pkg/mod/gitlab.com/axet/[email protected]", "GoMod": "/usr/local/google/home/hakim/go/pkg/mod/cache/download/gitlab.com/axet/torrent/@v/v0.0.0-20200205141541-92b4b9e7387e.mod" } Also, while we are here, trim down the entries added to the generated go.mod. We need the main module, and the replaced module info. We may want to pin golang.org/x/mobile version if possible, but I don't know a reliable way to achieve that yet. Fixes golang/go#37048 Change-Id: Ibd7332338c0a3c4165a642c3e86852061f6ab13b Reviewed-on: https://go-review.googlesource.com/c/mobile/+/218057 Run-TryBot: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Hajime Hoshi <[email protected]>
1 parent 0df4eb2 commit 2b26a47

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

cmd/gomobile/bind.go

+16-5
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ func getModuleVersions(targetOS string, targetArch string, src string) (*modfile
235235
if targetOS == "darwin" {
236236
tags = append(tags, "ios")
237237
}
238+
// TODO(hyangah): probably we don't need to add all the dependencies.
238239
cmd.Args = append(cmd.Args, "-m", "-json", "-tags="+strings.Join(tags, ","), "all")
239240
cmd.Dir = src
240241

@@ -245,6 +246,7 @@ func getModuleVersions(targetOS string, targetArch string, src string) (*modfile
245246
}
246247

247248
type Module struct {
249+
Main bool
248250
Path string
249251
Version string
250252
Dir string
@@ -263,12 +265,21 @@ func getModuleVersions(targetOS string, targetArch string, src string) (*modfile
263265
if mod != nil {
264266
switch {
265267
case mod.Replace != nil:
266-
f.AddReplace(mod.Path, mod.Version, mod.Replace.Dir, mod.Replace.Version)
267-
case mod.Version == "":
268+
p, v := mod.Replace.Path, mod.Replace.Version
269+
if modfile.IsDirectoryPath(p) {
270+
// replaced by a local directory
271+
p = mod.Replace.Dir
272+
}
273+
f.AddReplace(mod.Path, mod.Version, p, v)
274+
case mod.Main, mod.Path == "golang.org/x/mobile":
275+
// We are binding this module or it has
276+
// explicit dependency on golang.org/x/mobile.
268277
// When the version part is empty, the module is local and mod.Dir represents the location.
269-
f.AddReplace(mod.Path, "", mod.Dir, "")
270-
default:
271-
f.AddRequire(mod.Path, mod.Version)
278+
if v := mod.Version; v == "" {
279+
f.AddReplace(mod.Path, mod.Version, mod.Dir, "")
280+
} else {
281+
f.AddRequire(mod.Path, v)
282+
}
272283
}
273284
}
274285
if err == io.EOF {

0 commit comments

Comments
 (0)