3

I'm using "replace" statement while do my local development. So my go.mod looks like this:

require (
 gorm.io/gorm v1.21.11
 github.com/mypackages/session v1.1.0
)

replace (
 github.com/mypackages/session => ./../session
)

But I have no need in "replace" when I git commit my changes and deploy code to production, so I need to comment this line of replace code on each git commit and uncomment it then. Is there a way to ignore the "replace" statement on a production environment?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
sn0rk
  • 83
  • 1
  • 5
  • For what it's worth, https://golang.org/issue/45713 proposes an alternative to `replace` for local development, in a form that would be easier to `.gitignore` (and/or easier to override for production use by passing `-workfile=off`). – bcmills Aug 02 '21 at 14:34
  • 1
    if the replace is there only because you need it in your local development, [you could use an alternate go.mod file instead](https://stackoverflow.com/questions/68764637/how-to-use-an-alternate-go-mod-file-for-local-development) – blackgreen Jun 01 '22 at 21:23

5 Answers5

5

replace can't be ignored in an environment, because it's used at dependency resolution time, which comes before build, which is long before it ever gets executed in production. But to answer the root question, no, you can't "ignore" the directive. If it's there, it's there.

Adrian
  • 42,911
  • 6
  • 107
  • 99
  • Thanks for your answer. Are you ever faced with that problem? Maybe you know a workaround solution? – sn0rk Jul 25 '21 at 12:34
5

While @Adrian is correct in that there is no way to accomplish this in Go, I think this question is less about Go and more about Git. You can ignore a specific part of your file using content filters. See this SO answer for more information.

Clark McCauley
  • 1,342
  • 5
  • 16
1

Have a local version of the mod file (e.g. go.local.mod) and then you can tell the go command to use it:

go build -modfile=go.local.mod main.go
lazyhacker
  • 11
  • 2
  • 1
    When linking to your own site or content (or content that you are affiliated with), you [must disclose your affiliation _in the answer_](/help/promotion) in order for it not to be considered spam. Having the same text in your username as the URL or mentioning it in your profile is not considered sufficient disclosure under Stack Exchange policy. – cigien Jan 22 '22 at 14:32
0

You can code a pre commit hook, which will detect whether your go.mod file contains replace or not.

Pre commit hook code -> https://gist.github.com/MohitVachhani/e331321bb6f8c3e5e26b0cb4ab792c55

Please go through this and let me know if any improvements you have!

Mohit Vachhani
  • 108
  • 1
  • 2
  • 5
0

The recipe:

Add this to .git/config of your project

[filter "goreplace"]
  smudge = sed 's~//replace~replace~'
  clean = sed 's~replace~//replace~'

alternative add global:

git config --global filter.goreplace.smudge "sed 's~//replace~replace~'"
git config --global filter.goreplace.clean "sed 's~replace~//replace~'"

and then add (to) the .gitattributes

*.mod filter=goreplace

Notice it just works for you not for all contributors. Also it only comment out the 'replace' statement you could also remove it but this would require more sed magic ;).

Kani
  • 810
  • 1
  • 19
  • 38