How to upgrade a go module behind a replace directive?

78 views Asked by At

Here is my go.mod

require(
    git.myorg.com/foo v0.0.0-20220124220940-064ec2185f3a
)

replace git.myorg.com/foo => git.myorg.com/myteam/foo vv0.0.0-20220202233523-3fe23663418f

module my-project-demo

go 1.16

Now I want to upgrade the package foo, I tried:

go get -u git.myorg.com/foo, it upgrades some other module's version but the module foo doesn't get upgraded;

I also tried go get -u git.myorg.com/myteam/foo, but got error message

parsing go.mod:
        module declares its path as: git.myorg.com/foo
                but was required as: git.myorg.com/myteam/foo

How to upgrade this foo module to latest version? Thanks!

1

There are 1 answers

2
sk shahriar ahmed raka On

To upgrade a Go module behind a replace directive, follow these steps:

1. Check the latest version of the module:

go list -m -versions git.myorg.com/myteam/foo

2. Update the version in the replace directive:

Open your go.mod file and modify the replace directive:

replace git.myorg.com/foo => git.myorg.com/myteam/foo v1.2.3

you have to Replace v1.2.3 with the latest version obtained in step 1.

3. Run go get to upgrade the module:

go get -u git.myorg.com/foo

This command updates the module to the latest version specified in the replace directive.

4. Tidy up dependencies:

go mod tidy

This ensures that the go.mod file is clean and reflects the correct dependencies.

Additional Considerations:

  • Module Path Consistency: Ensure the module path in the require and replace directives matches the actual module path declared in git.myorg.com/foo's go.mod file.

  • Local Development: If using a local version or fork, consider using a branch-based replace directive (e.g., replace git.myorg.com/foo => ../myteam/foo) for local development.

  • Vendoring: For strict control over the version, use vendoring to lock down the specific version of git.myorg.com/foo in your project.