Search code examples
gogo-modules

Go modules replace with a specific version of a local module


I have a go.mod file that looks like this:

module someName

go 1.13

require (
    .
    .
    golang.org/x/crypto v0.0.0-20190621222207-cc06ce4a13d4
    localpackage v0.0.0
)

replace localpackage => ../localpackage

This works just as expected! What I can't figure out is how can I add a certain version or commit hash to the replace directive!

For example:

replace localpackage => ../localpackage v1.0.0

or

replace localpackage => ../localpackage v0.0.0-20190731182444-35453ccff3d6

Doing this results in an error:

replacement module directory path "../localpackage" cannot have version

The error is quite clear that I shouldn't add a version to a local replace. I checked the wiki but I couldn't find an answer!

The question:

Is it possible to add this kind of replacement and how? What am I missing here?


Solution

  • As the error says: you cannot specify a version when the replace directive is pointed at a local folder. There is no guarantee and it is not a requirement that the replacement folder contains files of a versioning system, it is perfectly valid to just have "snaphots" of the Go sources. So in many cases it would have no meaning to specify a version.

    However, if your local folder is a clone of a git repository for example, you may simply switch that to your intended version. E.g. execute a git checkout v1.0.0 in that local folder to switch to that version, and that version will be used.