Skip to Content
Course content

170: Replace Directives for Local Development

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I've seen this happen more times than I can count: a developer is working on a shared library and a main application simultaneously. They hit a bug in the library, fix it locally, and then add a replace directive to the application's go.mod file to point to their local folder. Everything works perfectly on their machine. Then, they commit that go.mod file and push it to the main branch. Five minutes later, the CI pipeline explodes, and every other engineer on the team is staring at a "directory not found" error.

The "Just Commit the Relative Path" Trap

The misconception is thinking that a replace directive is a way to "link" two projects permanently. It isn't. When you write something like replace github.com/myorg/logger => ../logger, you aren't telling Go how the project is structured in the cloud; you are telling the Go toolchain to ignore the remote version and look at a specific folder on your hard drive.

Let's look at a concrete example. Suppose you have a project structure like this:

/home/dev/projects/
  ├── auth-service/ (The app)
  └── common-lib/   (The shared library)

In auth-service/go.mod, you add:

module github.com/myorg/auth-service

go 1.21

require github.com/myorg/common-lib v1.0.0

replace github.com/myorg/common-lib => ../common-lib

This works great while you're coding. You change a function in common-lib, and auth-service sees it immediately. But the moment you push this to GitHub, the Go compiler on the build server looks for ../common-lib relative to the build directory. Since the build server only cloned the auth-service repo, that folder doesn't exist. The build fails. I've had to spend a few too many Friday afternoons scrubbing these directives out of version control.

Bridging Local Modules Without Breaking CI

The replace directive is a powerful tool for iteration, not for distribution. The correct workflow is to use it as a temporary bridge. You use it to verify that your library changes actually fix the bug in the application before you ever publish the library.

Here is the professional loop for handling this:

  • Local Iteration: Add the replace directive to go.mod. This allows you to skip the "push-tag-update" cycle every time you fix a typo.
  • Verification: Run your tests in the application to ensure the local library changes work.
  • Release: Commit and push the changes to the common-lib repository. Create a new semantic version tag (e.g., v1.0.1).
  • Integration: Remove the replace directive from auth-service/go.mod and run go get github.com/myorg/common-lib@v1.0.1.

If you find yourself doing this constantly across five or six different modules, you might want to look into Go Workspaces (go.work), but for a quick fix between a library and an app, the replace directive is the fastest path—provided you treat it like a local configuration, not a project setting. Pro tip: if you're using Git, you can actually add go.mod to your .git/info/exclude file (not the tracked .gitignore) if you want to make sure you don't accidentally stage those local changes, though usually, a quick git diff before committing is enough to catch it.




📋 Practical Task

Decoupling the Payment Gateway Library

You have been handed a project called payment-processor that is currently broken in the CI pipeline. The previous developer left a local replace directive in the go.mod file that points to a directory that doesn't exist on any other machine.

Your Goal: Clean up the module dependencies so the project can be built from the remote source again.

  1. Open the payment-processor/go.mod file.
  2. Locate the line: replace github.com/myorg/payment-sdk => ../payment-sdk.
  3. Remove this directive entirely.
  4. Run go mod tidy to ensure the go.sum file is cleaned up and the project is pointing to the actual remote version of payment-sdk.
  5. Verify the project still compiles by running go build ./....
Rating
0 0

There are no comments for now.

to be the first to leave a comment.