I'm trying to import go module from subdirectory. My repository looks like
+$ tree .
.
└── bar
├── bar.go
└── go.mod
1 directory, 2 files
+$ cat bar/go.mod
module github.com/graywolf/foo/bar
go 1.13
+$ cat bar/bar.go
package bar
func Bar() string {
return "bar"
}
when pushed to github this source code runs fine
+$ cat github.go
package main
import (
"fmt"
"github.com/graywolf/foo/bar"
)
func main() {
fmt.Println(bar.Bar())
}
+$ go run github.go
go: finding github.com/graywolf/foo latest
go: finding github.com/graywolf/foo/bar latest
go: downloading github.com/graywolf/foo v0.0.0-20191019144834-ffb419608ae6
go: extracting github.com/graywolf/foo v0.0.0-20191019144834-ffb419608ae6
bar
However it stops working when I push it to different git hosting. I tweak the url
diff --git a/bar/go.mod b/bar/go.mod
index 7407848..6134a24 100644
--- a/bar/go.mod
+++ b/bar/go.mod
@@ -1,3 +1,3 @@
-module github.com/graywolf/foo/bar
+module git.sr.ht/~graywolf/foo/bar
go 1.13
commit and push it and try to run it
+$ cat sr.go
package main
import (
"fmt"
"git.sr.ht/~graywolf/foo/bar"
)
func main() {
fmt.Println(bar.Bar())
}
+$ go run sr.go
go: finding git.sr.ht/~graywolf/foo latest
go: downloading git.sr.ht/~graywolf/foo v0.0.0-20191019153505-33a4721605aa
go: extracting git.sr.ht/~graywolf/foo v0.0.0-20191019153505-33a4721605aa
build command-line-arguments: cannot load git.sr.ht/~graywolf/foo/bar: module git.sr.ht/~graywolf/foo@latest (v0.0.0-20191019153505-33a4721605aa) found, but does not contain package git.sr.ht/~graywolf/foo/bar
It looks like github and source hut provide different go-import meta tag, notice the missing .git for source hut.
+$ curl -sSf https://github.com/graywolf/foo?go-get=1 | grep -A1 go-import
<meta name="go-import" content="github.com/graywolf/foo git https://github.com/graywolf/foo.git">
+$ curl -sSf https://git.sr.ht/~graywolf/foo?go-get=1 | grep -A1 go-import
<meta name="go-import"
content="git.sr.ht/~graywolf/foo git https://git.sr.ht/~graywolf/foo">
When I edit the source code to use foo.git it does find the module, but provide different error message:
+$ go run sr.go
go: finding git.sr.ht/~graywolf/foo.git latest
go: finding git.sr.ht/~graywolf/foo.git/bar latest
go: downloading git.sr.ht/~graywolf/foo.git v0.0.0-20191019153505-33a4721605aa
go: downloading git.sr.ht/~graywolf/foo.git/bar v0.0.0-20191019153505-33a4721605aa
go: extracting git.sr.ht/~graywolf/foo.git v0.0.0-20191019153505-33a4721605aa
go: extracting git.sr.ht/~graywolf/foo.git/bar v0.0.0-20191019153505-33a4721605aa
go: git.sr.ht/~graywolf/foo.git/bar: git.sr.ht/~graywolf/foo.git/bar@v0.0.0-20191019153505-33a4721605aa: parsing go.mod:
module declares its path as: git.sr.ht/~graywolf/foo/bar
but was required as: git.sr.ht/~graywolf/foo.git/bar
Btw both (github and source hut) works fine when the go.mod is in the root of the repository, issue happens only when it is in a subdirectory.
I guess my question is what can I do about it. Did anyone run into something like this? Why doesn't go just clone the ~graywolf/foo and not check the bar subdirectory?