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:
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.modgo: 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.2stays. MVS still runs on versions;replaceonly swaps the content of the selected version.go list -m allshows the swap with=>. - The fork's
go.modmust declaremodule 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
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
replaceonly applies in the main module. When someone else depends on your module, yourreplacelines are ignored, and their build uses the real versions. A library that only works because of its ownreplaceis broken for its users. Field Notes shows this happening.- Directory replacements don't work for anyone else.
../quote-forkexists 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 uncommittedgo.work. go install pkg@versionrefuses modules withreplacedirectives pointing at directories, and it ignores the rest, so a tool that needs a replacement can't be installed that way.- 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 andgo.sumpins 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?
- Nothing; the require line is removed
- The same
rsc.io/quote v1.5.2; replace swaps the content of the selected version rsc.io/quote ../quote-fork- 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?
- v1.99.99, because the replace is inherited
- Whatever MVS selects in their build; your replace is ignored
- A build error
- The replace only if they vendor
What is the safest way to depend on a fork long-term?
- A directory replace to a sibling checkout
- Publish a tagged version of the fork and replace with that version (or upstream the fix)
- Edit the module cache
- Delete go.sum lines