dsg56465
2019-07-22 10:14
浏览 220
已采纳

go.mod在修订版中具有v0后的模块路径“ git.example.com/owner/repo/v3”吗?

My coworker pushed a tag v3.0.1 before updating go.mod to have /v3 suffix (https://github.com/golang/go/wiki/Modules#releasing-modules-v2-or-higher). I have updated module path (go.mod) and all import paths (*.go) to fix it, tagged as v3.0.2.

Now the problem is:

go get -v git.example.com/owner/repo@v3.0.2
go: finding git.example.com/owner/repo v3.0.2
go: git.example.com/owner/repo@v0.0.0-20190722053407-d85c4f69ad17: go.mod has post-v0 module path "git.example.com/owner/repo/v3" at revision 
d85c4f69ad17

Found this: go build keeps complaining that: go.mod has post-v0 module path

So, I deleted both v3.0.0 and v3.0.1 tags, pointed it to the latest commit, re-pushed but the problem still stand.

I noticed that go.mod still refered to the old version as an indirect dependency:

require (
    git.example.com/owner.repo v0.1.2 // indirect

Even if I changed it to /v3 v3.0.2 it will be restored to v0.1.12 automatically.

Why?

Did I miss something?


Tue Jul 23 05:54:56 +07 2019

rm go.*
go mod init git.example.com/dependent/project
go mod tidy

and go.mod is updated correctly now:

require (
-       git.example.com/owner/repo v0.1.2
+       git.example.com/owner/repo/v3 v3.0.2

but go get -v git.example.com/owner/repo@v3.0.2 still returned the error:

go: finding git.example.com/owner/repo v3.0.2
go: git.example.com/owner/repo@v0.0.0-20190722053407-d85c4f69ad17: go.mod has post-v0 module path "git.example.com/owner/repo/v3" at revision 
d85c4f69ad17

(d85c4f69ad17 is the latest commit in master)

I noticed that there are both v0.1.2 and v3.0.2 in go.sum:

git.example.com/owner/repo v0.1.2 h1:mCGJEmyrFDTCGkRfUIORpqdrNkSONQ6K+AcTNgxqveY=
git.example.com/owner/repo v0.1.2/go.mod h1:FfUKnyPrARCtAXQZ3BQVJI7h2eJ0UpQBMLg4bNs4Kdc=
git.example.com/owner/repo/v3 v3.0.2 h1:mJtDKLeiP8vMRSZo08i/k/KDbIoZTlKW2aWu7DUBvMM=
git.example.com/owner/repo/v3 v3.0.2/go.mod h1:64LE0ts0Lk9InIQyhPYGmnxs6LZIl6H4Iorl1EXfqxo=
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanhongyi2964 2019-07-23 08:07
    已采纳

    Please pay attention to my go get command:

    go get -v git.example.com/owner/repo@v3.0.2
    

    It should be:

    go get -v git.example.com/owner/repo/v3@v3.0.2
    
    点赞 打赏 评论
  • douchongbc72264 2019-07-22 11:17

    for example you can replace repository with this hack: https://github.com/golang/go/wiki/Modules

    require {
    ...
    }
    
    replace git.example.com/owner.repo v0.1.2 => git.example.com/owner.repo v3.0.2
    

    or you can use go get at the commit hash you want:

    go get git.example.com/owner.repo@af044c0995fe

    go get will correctly update the dependency files (go.mod, go.sum).

    For more information: https://github.com/golang/go/wiki/Modules#how-to-upgrade-and-downgrade-dependencies

    or for last example you should clean cache

    1. remove go.mod and go.sum
    2. go cache clean
    3. go mod vendor
    点赞 打赏 评论
  • douju9847 2019-08-03 23:00

    Expanding on the answer from @quanta...

    You are doing:

    go get -v git.example.com/owner/repo@v3.0.2

    Because it is a v3 module, the go get command should include a /v3 before the @:

    go get -v git.example.com/owner/repo/v3@v3.0.2

    Once a v3.x.y package is a module with its own go.mod, then whenenver you are operating with modules enabled, you pretty much always include the /v3 whenever you reference the v3.x.y module, including in:

    • arguments to go get on the command line
    • import statements in .go code for the consumer
    • require statements in a consumer's go.mod
    • replace or exclude statements in a consumer's go.mod
    • the module line of the v3 module's go.mod file
    • internal import statements in .go code inside the v3 module importing other packages in the v3 module
    • etc.

    One way to think about it is that the module's name is now effectively git.example.com/owner/repo/v3, where its name includes the trailing /v3.

    If you are a consumer of a vN module and need to update your import paths in your .go files to include the vN, then github.com/marwan-at-work/mod is a commonly used tool from the community that automates adding the /vN in all the required spots. Separately, it also automates placing the /vN in all the required spots if you are a module author for a v2+ module.

    From the "Semantic Import Versioning" section of the Go modules wiki:

    If the module is version v2 or higher, the major version of the module must be included as a /vN at the end of the module paths used in go.mod files (e.g., module github.com/my/mod/v2, require github.com/my/mod/v2 v2.0.0) and in the package import path (e.g., import "github.com/my/mod/v2/mypkg").

    点赞 打赏 评论

相关推荐 更多相似问题