Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/vgo: can't use submodules along with "parent" modules #26250

Closed
mwf opened this issue Jul 6, 2018 · 5 comments
Closed

x/vgo: can't use submodules along with "parent" modules #26250

mwf opened this issue Jul 6, 2018 · 5 comments
Milestone

Comments

@mwf
Copy link

mwf commented Jul 6, 2018

What version of Go are you using (go version)?

go version go1.10.3 darwin/amd64 vgo:2018-02-20.1

Latest vgo:

commit 6cd5a417451d8ee907692eded07ef1b6b53825b1 (HEAD -> master, origin/master, origin/HEAD)
Author: Bryan C. Mills <bcmills@google.com>
Date:   Mon Jul 2 16:43:34 2018 -0400

    cmd/go/internal/modcmd: use the correct path in verifyMod

What did you do?

Please refer to https://github.com/mwf/vgo-modules

I'm very sorry, but this case doesn't have standalone example, because tagging revisions and active repo usage is involved.

Let's assume we are in active development phase with versions v0.x.

At some point we decide to move some package to a standalone module, to split the dependencies and to have better semantic structure.

Thus cmd package is introduced to a separate module github.com/mwf/vgo-modules/cmd.

But it turns out you can't use both github.com/mwf/vgo-modules/cmd and github.com/mwf/vgo-modules at the same time. Have a look at example project.

What did you expect to see?

vgo get github.com/mwf/vgo-modules/cmd should run without error and get the latest github.com/mwf/vgo-modules/cmd v0.0.1

What did you see instead?

$ cd ./example && vgo get github.com/mwf/vgo-modules/cmd
vgo: import "github.com/mwf/vgo-modules/cmd": found in both github.com/mwf/vgo-modules v0.0.2 and github.com/mwf/vgo-modules/cmd v0.0.1

The actual error is absolutely strange to me. Because formally github.com/mwf/vgo-modules v0.0.2 doesn't contain cmd package:

vgo mod -packages
github.com/mwf/vgo-modules
github.com/mwf/vgo-modules/a

This is a case not only for a refactoring (like introducing new modules for existing packages), but also if you make a brand-new submodule and try to use both "parent" and the new one. The error is the same.

@gopherbot gopherbot added this to the vgo milestone Jul 6, 2018
@mwf
Copy link
Author

mwf commented Jul 6, 2018

And the second connected issue - vgo install can't work with "submodules"

vgo install github.com/mwf/vgo-modules/cmd
vgo: import "github.com/mwf/vgo-modules/cmd" [/Users/ikorolev/.gvm/pkgsets/go1.10.3/global/src/mod/github.com/mwf/vgo-modules@v0.0.2/cmd]: open /Users/ikorolev/.gvm/pkgsets/go1.10.3/global/src/mod/github.com/mwf/vgo-modules@v0.0.2/cmd: no such file or directory

It assumes cmd is in github.com/mwf/vgo-modules instead of standalone module.

@mwf
Copy link
Author

mwf commented Jul 9, 2018

cc @myitcv
Have you tried working with nested modules? Because as I see in the #25855 discussion, there must be a way to use them.
Maybe I'm missing something essential.

cd `mktemp -d`
cat <<EOD > go.mod
module example.com/test

require (
	github.com/mwf/vgo-modules v0.0.3
	github.com/mwf/vgo-modules/another_module v0.0.1
)
EOD
cat << EOD > main.go
package main

import (
	"fmt"

	"github.com/mwf/vgo-modules"
	"github.com/mwf/vgo-modules/another_module"
)

func main() {
	fmt.Printf("vgo-modules version: %s\n", vgo_modules.Version)
	fmt.Printf("another_module.Foo: %s\n", another_module.Foo)
}
EOD
vgo run main.go
vgo: import "github.com/mwf/vgo-modules/another_module": found in both github.com/mwf/vgo-modules v0.0.3 and github.com/mwf/vgo-modules/another_module v0.0.1

@gopherbot
Copy link

Change https://golang.org/cl/123095 mentions this issue: cmd/go/internal/modload: finish Import implementation

@myitcv
Copy link
Member

myitcv commented Jul 13, 2018

@mwf sorry, I missed you pinging me in this.

Yes, I have successfully worked with submodules. Here's a simple example

Given vgo has "become" go (as well as a whole load of other fixes landing), please can you try with Go tip?


Initialise a directory as a git repo, and add an appropriate remote:

$ mkdir go-modules-by-example-submodules
$ cd go-modules-by-example-submodules
$ git init
Initialized empty Git repository in /root/go-modules-by-example-submodules/.git/
$ git remote add origin https://github.com/$GITHUB_USERNAME/go-modules-by-example-submodules

Now define our root module, at the root of the repo, commit and push:

$ cat <<EOD >go.mod
module github.com/$GITHUB_USERNAME/go-modules-by-example-submodules
EOD
$ git add go.mod
$ git commit -am 'Initial commit'
[master (root-commit) b143fe5] Initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 go.mod
$ git push
To https://github.com/myitcv/go-modules-by-example-submodules
 * [new branch]      master -> master

Now create a package b and test that it builds:

$ mkdir b
$ cd b
$ cat <<EOD >b.go
package b // import "github.com/$GITHUB_USERNAME/go-modules-by-example-submodules/b"

const Name = "Gopher"
EOD
$ echo >go.mod
$ go test
?   	github.com/myitcv/go-modules-by-example-submodules/b	[no test files]

Now commit, tag and push our new package:

$ cd ..
$ git add b
$ git commit -am 'Add package b'
[master 4179a82] Add package b
 2 files changed, 4 insertions(+)
 create mode 100644 b/b.go
 create mode 100644 b/go.mod
$ git push
To https://github.com/myitcv/go-modules-by-example-submodules
   b143fe5..4179a82  master -> master
$ git tag b/v0.1.1
$ git push origin b/v0.1.1
To https://github.com/myitcv/go-modules-by-example-submodules
 * [new tag]         b/v0.1.1 -> b/v0.1.1

Now create a main package that will use b:

$ mkdir a
$ cd a
$ cat <<EOD >.gitignore
/a
EOD
$ cat <<EOD >a.go
package main // import "github.com/$GITHUB_USERNAME/go-modules-by-example-submodules/a"

import (
	"github.com/$GITHUB_USERNAME/go-modules-by-example-submodules/b"
	"fmt"
)

const Name = b.Name

func main() {
	fmt.Println(Name)
}
EOD
$ echo >go.mod

Now let's build and run our package:

$ go build
go: finding github.com/myitcv/go-modules-by-example-submodules/b v0.1.1
go: downloading github.com/myitcv/go-modules-by-example-submodules/b v0.1.1
$ ./a
Gopher
$ cat go.mod
module github.com/myitcv/go-modules-by-example-submodules/a

require github.com/myitcv/go-modules-by-example-submodules/b v0.1.1

See how we resolve to the tagged version of package b.

Finally we commit, tag and push our main package:

$ cd ..
$ git add a
$ git commit -am 'Add package a'
[master 6f5eb7d] Add package a
 4 files changed, 18 insertions(+)
 create mode 100644 a/.gitignore
 create mode 100644 a/a.go
 create mode 100644 a/go.mod
 create mode 100644 a/go.sum
$ git push
To https://github.com/myitcv/go-modules-by-example-submodules
   4179a82..6f5eb7d  master -> master
$ git tag a/v1.0.0
$ git push origin a/v1.0.0
To https://github.com/myitcv/go-modules-by-example-submodules
 * [new tag]         a/v1.0.0 -> a/v1.0.0

Version details

go version devel +8a330454dc Fri Jul 13 03:53:00 2018 +0000 linux/amd64

@mwf
Copy link
Author

mwf commented Jul 17, 2018

Thanks for the answer, @myitcv !

https://golang.org/cl/123095 fixed all this stuff, so it works with latest vgo and I believe it works in go tip :)

@golang golang locked and limited conversation to collaborators Jul 17, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants