TExcelTAD
Home › Go Modules in Depth › replace for Local Development

replace for Local Development

You found a bug in a dependency and want to try a fix before the maintainer releases it. Or you are changing a library and the app that uses it at the same time. Never edit files in the module cache (go.sum showed why). Use a replace directive instead: it tells the go command to take a module's code from somewhere else.

The three forms

Form Example Meaning
module → local directory replace rsc.io/quote => ../quote-fork use the code in that directory (it must contain a go.mod); no version, no download, no go.sum line
module → other version replace rsc.io/sampler => rsc.io/sampler v1.3.1 always use v1.3.1, whatever MVS would select
module → other module replace github.com/up/lib => github.com/me/lib v1.2.4-fix a published fork under another path

Add @version on the left (replace rsc.io/sampler v1.99.99 => …) to replace only that version. The right side of a directory replacement starts with ./ or ../ (or is absolute).

Patching a dependency locally

This session copies rsc.io/quote out of the module cache into ../quote-fork, patches it, and points the app at it:

bash · try it yourself
mkdir app && cd app
go mod init example.com/app 2>/dev/null
cat > main.go <<'SRC'
package main

import (
	"fmt"

	"rsc.io/quote"
)

func main() { fmt.Println(quote.Hello()) }
SRC
go get rsc.io/quote@v1.5.2
go mod tidy
go run .
cp -r "$(go list -m -f '{{.Dir}}' rsc.io/quote)" ../quote-fork
chmod -R u+w ../quote-fork
sed -i 's/return sampler.Hello()/return sampler.Hello() + " (patched in my fork)"/' ../quote-fork/quote.go
go mod edit -replace rsc.io/quote=../quote-fork
go run .
go list -m all
grep quote go.mod
go: downloading rsc.io/quote v1.5.2
go: downloading rsc.io/sampler v1.3.0
go: downloading golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
go: added golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
go: added rsc.io/quote v1.5.2
go: added rsc.io/sampler v1.3.0
Ahoy, world!
Ahoy, world! (patched in my fork)
example.com/app
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c
rsc.io/quote v1.5.2 => ../quote-fork
rsc.io/sampler v1.3.0
require rsc.io/quote v1.5.2
replace rsc.io/quote => ../quote-fork

Notice:

  • The requirement rsc.io/quote v1.5.2 stays. MVS still runs on versions; replace only swaps the content of the selected version. go list -m all shows the swap with =>.
  • The fork's go.mod must declare module rsc.io/quote (or be compatible with it); its requirements join the module graph like any dependency's.
  • The local directory has no hash in go.sum: it's your code, not a download.

Replacing a version, and undoing it

bash · try it yourself
mkdir app && cd app
go mod init example.com/app 2>/dev/null
printf 'package main\n\nimport (\n\t"fmt"\n\n\t"rsc.io/quote"\n)\n\nfunc main() { fmt.Println(quote.Hello()) }\n' > main.go
go get rsc.io/quote@v1.5.2 2>/dev/null
go mod edit -replace rsc.io/sampler=rsc.io/sampler@v1.99.99
go mod tidy
go list -m rsc.io/sampler
go run .
go mod edit -dropreplace rsc.io/sampler
go mod tidy
go list -m rsc.io/sampler
go run .
go: downloading rsc.io/sampler v1.99.99
rsc.io/sampler v1.3.0 => rsc.io/sampler v1.99.99
99 bottles of beer on the wall, 99 bottles of beer, ...
rsc.io/sampler v1.3.0
Ahoy, world!

A version replacement overrides MVS for that module entirely, so it's a sharp tool: use it for a short-term fix and leave a comment saying why and when it can go.

The rules that bite

  1. replace only applies in the main module. When someone else depends on your module, your replace lines are ignored, and their build uses the real versions. A library that only works because of its own replace is broken for its users. Field Notes shows this happening.
  2. Directory replacements don't work for anyone else. ../quote-fork exists on your disk, not in CI or on a colleague's machine. Don't commit them; for local multi-module work use a workspace instead, which lives in an uncommitted go.work.
  3. go install pkg@version refuses modules with replace directives pointing at directories, and it ignores the rest, so a tool that needs a replacement can't be installed that way.
  4. For a lasting fork, publish it (a tag in your fork's repository) and replace with a version of the fork: replace github.com/up/lib => github.com/me/lib v1.2.4-fix.1. Then CI can download it and go.sum pins it. Better still, upstream the fix and drop the replacement.

Test yourself

After replace rsc.io/quote => ../quote-fork, what does go.mod still require?

  1. Nothing; the require line is removed
  2. The same rsc.io/quote v1.5.2; replace swaps the content of the selected version
  3. rsc.io/quote ../quote-fork
  4. A pseudo-version of the fork

Your library's go.mod has replace rsc.io/sampler => rsc.io/sampler v1.99.99. What do applications that depend on your library get?

  1. v1.99.99, because the replace is inherited
  2. Whatever MVS selects in their build; your replace is ignored
  3. A build error
  4. The replace only if they vendor

What is the safest way to depend on a fork long-term?

  1. A directory replace to a sibling checkout
  2. Publish a tagged version of the fork and replace with that version (or upstream the fix)
  3. Edit the module cache
  4. Delete go.sum lines