Replace modules from command line

155 views
Skip to first unread message

Peter Feichtinger

unread,
Aug 5, 2019, 12:46:01 PM8/5/19
to golang-nuts
Hi Go Nuts,

I have a rather unusual use case and I'm hoping for some input.

For testing purposes, I want to build a Go binary with different versions of one of its dependencies. The only way I could think of was to modify the go.mod file to add a replace directive with the version I want to test.
Instead I'd like to avoid having to modify the go.mod file during the build and specify the replacement on the command line instead, but I don't think that's possible.

Do you guys have any ideas on how else I might accomplish overriding a module version during build?

-- Peter

Paul Jolly

unread,
Aug 5, 2019, 1:16:08 PM8/5/19
to Peter Feichtinger, golang-nuts

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/efc91729-47a6-4dcb-a08d-6c907b24bd56%40googlegroups.com.

Peter Feichtinger

unread,
Aug 6, 2019, 1:26:45 AM8/6/19
to golang-nuts
Thanks for that, this will come in handy for editing the go.mod file.
But I'd like to avoid editing the file at all.


On Monday, August 5, 2019 at 7:16:08 PM UTC+2, Paul Jolly wrote:
On Mon, 5 Aug 2019, 18:45 Peter Feichtinger, <shi...@gmx.at> wrote:
Hi Go Nuts,

I have a rather unusual use case and I'm hoping for some input.

For testing purposes, I want to build a Go binary with different versions of one of its dependencies. The only way I could think of was to modify the go.mod file to add a replace directive with the version I want to test.
Instead I'd like to avoid having to modify the go.mod file during the build and specify the replacement on the command line instead, but I don't think that's possible.

Do you guys have any ideas on how else I might accomplish overriding a module version during build?

-- Peter

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.

anderson...@onefootball.com

unread,
Aug 6, 2019, 11:12:14 AM8/6/19
to golang-nuts
go mod edit does the trick.

Try

go mod edit -replace=github.com/foo/barr=github.com/bar/foo


go mod edit
-replace=github.com/foo/barr=github.com/bar/foo@12345567789


Best

Anderson

anderson...@onefootball.com

unread,
Aug 6, 2019, 11:12:14 AM8/6/19
to golang-nuts
Ops, now really understood what you want...

Well, a not really elegant way could be to have it pointing to a local folder and change the content on the local folder. But I believe the go.sum would spot the difference and break the build.
But if you need to change the version, why cannot you change the go.mod?

best,

Anderson

Paul Jolly

unread,
Aug 6, 2019, 1:11:04 PM8/6/19
to Peter Feichtinger, golang-nuts
You won't be able to avoid editing the go.mod - it's the starting point for all module resolution.

How about taking a copy of the original go.{mod,sum} and restoring them, or using git checkout . to trash unstaged changes?

To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/6ced2780-eca5-4692-a7f6-c0c73ad5b966%40googlegroups.com.

Peter Feichtinger

unread,
Aug 8, 2019, 3:12:35 AM8/8/19
to golang-nuts
Yeah I was expecting that would be necessary. Thanks for your input anyway.

Elias Naur

unread,
Aug 8, 2019, 5:20:26 AM8/8/19
to golang-nuts
I don't think it's possible today. There is an issue for it: https://github.com/golang/go/issues/26640

 - elias

Peter Feichtinger

unread,
Aug 8, 2019, 6:36:53 AM8/8/19
to golang-nuts
This would do pretty much exactly what I want. I'll be watching that, thanks.
Reply all
Reply to author
Forward
0 new messages