How to manage replace directives in go.mod files when code versioning ?

407 views
Skip to first unread message

christoph...@gmail.com

unread,
Apr 27, 2021, 1:23:52 AM4/27/21
to golang-nuts
When debugging or testing we may need to add a replace directive in the go.mod file.

This change intended to be local only may interfere with code versioning. The replace directive may be accidentally committed and published. What is the proper way to manage replace directives when it comes to code versioning ? 

Apparently we are supposed to remove replace directives before commits, and put it back to continue testing. That is boring and error prone. Is there a better way ?

Uli Kunitz

unread,
Apr 27, 2021, 1:27:58 AM4/27/21
to golang-nuts
Are you aware of "replace directives only apply in the main module's go.mod file and are ignored in other modules. See Minimal version selection for details."?

christoph...@gmail.com

unread,
Apr 27, 2021, 2:59:47 AM4/27/21
to golang-nuts
Thank you for pointing this out. I wasn’t aware of it. But the question still holds for published main programs.

Reto

unread,
Apr 27, 2021, 10:24:51 AM4/27/21
to christoph...@gmail.com, golang-nuts
On Mon, Apr 26, 2021 at 11:59:47PM -0700, christoph...@gmail.com wrote:
> Thank you for pointing this out. I wasn’t aware of it. But the question
> still holds for published main programs.

If you have such a tight dependency that you need to develop them in parallel
(and hence need replace directives to a local fork) maybe they ought to life
in the same module?
Then you don't need to have a replace directive in the first place.

Other than that, git is your friend... *Do* commit the change and then rebase
the thing away when you don't need it anymore.

Ilia Choly

unread,
Apr 27, 2021, 10:30:23 AM4/27/21
to golang-nuts
I usually just use patch commits:

git add -p

Reply all
Reply to author
Forward
0 new messages