Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a Go Module with a major version to a fork @ master

I have trouble mapping a fork through any pinned branch with go.mod that use a project with the /v2 / major version mapping.

I have the following go.mod:

go 1.18

require (
    github.com/versent/saml2aws/v2 v2.35.0
)

Notice the module requires the /v2, otherwise it would get v2.17.0+incompatible.

  1. I forked the project here: https://github.com/marcottedan/saml2aws
  2. I created a branch add-aad-entropy-PhoneAppNotification here: https://github.com/marcottedan/saml2aws/tree/add-aad-entropy-PhoneAppNotification
  3. I tried to modify, or not, the first line of go.mod in my fork go mod from module github.com/versent/saml2aws/v2 to module github.com/marcottedan/saml2aws/v2

I'm using the following directives and none are working:

This downloads the tag 2.35.0 of my fork even though I ask it to get master

dmarcotte@dmarcottes% go get -d github.com/marcottedan/saml2aws/v2@master
go: downloading github.com/marcottedan/saml2aws/v2 v2.35.0
go: github.com/marcottedan/saml2aws/[email protected]: parsing go.mod:
        module declares its path as: github.com/versent/saml2aws/v2
                but was required as: github.com/marcottedan/saml2aws/v2

I also tried modifying my go.mod:

replace github.com/versent/saml2aws/v2 => github.com/marcottedan/saml2aws/v2 v2.35.0

-> Can't find a way to target master with the /v2 pattern

If I drop the /v2 and just go with @master, it doesn't care and get the latest tag in v1 (which was named 2.17.0+incompatible before saml2aws migrated to go mod)

dmarcotte@dmarcottes % go get -d github.com/marcottedan/saml2aws@master   
go: github.com/marcottedan/saml2aws@master: github.com/marcottedan/[email protected]: invalid version: go.mod has post-v1 module path "github.com/marcottedan/saml2aws/v2" at revision f05a14a2b3f2

I'm quite lost here.

like image 869
Daniel Marcotte Avatar asked Jan 26 '26 06:01

Daniel Marcotte


2 Answers

After a lot of digging, here are the steps I found that seem to be working:

  1. Create a fork of any project
  2. Change the go.mod first line to your new fork name, commit & push
  3. Commit&Push any change you need, in a branch or in master
  4. In your original project, do the following command: go get -d -u github.com/marcottedan/saml2aws/v2@master where the @version is your branch name.
  5. In your go.mod, add the following replace directive: replace github.com/versent/saml2aws/v2 v2.35.0 => github.com/marcottedan/saml2aws/v2 master
  6. Every time you push a commit in your fork, repeat step 4.

At the end your go.mod should look like this:

module <yourname>

go 1.18

require (
    github.com/versent/saml2aws/v2 v2.35.0
)

replace github.com/versent/saml2aws/v2 v2.35.0 => github.com/marcottedan/saml2aws/v2 master

Note that if you're only working in solo, you can use the replace directive to map a local folder on your drive. But this tend to not work well with teammates since they must also have the same exact fork path checked out while pulling the code. Here's an example:

module <yourname>

go 1.18

require (
    github.com/versent/saml2aws/v2 v2.35.0
)

replace github.com/versent/saml2aws/v2 => /Users/dmarcotte/git/saml2aws/
like image 161
Daniel Marcotte Avatar answered Jan 27 '26 20:01

Daniel Marcotte


When working through this same issue, I found that Go has updated their replace directive. Most of the steps in Daniel's solution still do work. The small thing is that Go will not let you add branchname at the end of the replace directive. So here are the steps I took instead:

  1. Same steps for step 1-3
  2. When pushing my change on the fork, I would put an arbitary tag i.e. git tag v0.0.1 && git push origin --tags
  3. Add the replace directive replace github.com/versent/saml2aws/v2 v2.35.0 => github.com/marcottedan/saml2aws/v2 v0.0.1
  4. Whenever I push changes, I would create new tags in the fork, push them and update the replace directive in the consuming project. Go seems to understand this and will run go get for the new version.

So all in all:

module <yourname>

go 1.18

require (
    github.com/versent/saml2aws/v2 v2.35.0
)

replace github.com/versent/saml2aws/v2 v2.35.0 => github.com/marcottedan/saml2aws/v2 v0.0.1
like image 21
tanzy Avatar answered Jan 27 '26 20:01

tanzy