Importing local module for development

Hello, I am working on modifying the caddy-l4 module and would like to test my changes locally. I’ve built Caddy from source, and see where I can import custom modules in main.go.

package main

import (
	caddycmd "github.com/caddyserver/caddy/v2/cmd"

	// plug in Caddy modules here
	_ "../../modules/caddy-l4"

	_ "github.com/caddyserver/caddy/v2/modules/standard"
)

func main() {
	caddycmd.Main()
}

However, I’m not sure now to import a local module. Is there a way to do this? Or should I be pointing the import to a github fork of the module?

You can import a local Go module using replace in your go.mod:

replace github.com/you/project => ../project

Or something like that. (Your local module will still need a go.mod with an import path as if it was published, even if it’s not.)

However, if you’re using xcaddy, it does this for you. All you have to do is run xcaddy inside your module folder instead of caddy. It will compile the current folder as a module into Caddy and run it. (So, for example, you’ll often do xcaddy run instead of caddy run.)

2 Likes