Search code examples
goprotocol-buffersprotocgo-modules

How to use a forked module, with versioned Go Modules (v1.11+, GO111MODULE=on)


I forked a go module, and want to use the fork in my project that uses versioned modules via v1.12. My code is not inside my GOPATH.

My project's go.mod:

module github.com/me/myproj

go 1.12

require (   
    go.larrymyers.com/protoc-gen-twirp_typescript v0.0.0-20190605194555-ffbfe407b60f
)

replace go.larrymyers.com/protoc-gen-twirp_typescript => github.com/rynop/protoc-gen-twirp_typescript master

protoc-gen-twirp_typescript is a tool for protoc, so here is my tools.go:

// +build tools

package tools

import (
    // protocol buffer compiler plugins
    _ "github.com/golang/protobuf/protoc-gen-go"
    _ "github.com/mwitkow/go-proto-validators/protoc-gen-govalidators"
    _ "github.com/twitchtv/twirp/protoc-gen-twirp"
    _ "github.com/rynop/protoc-gen-twirp_typescript"
)

When I run go mod tidy to download my dependencies, I get this error:

go: finding github.com/rynop/protoc-gen-twirp_typescript master
go: finding github.com/rynop/protoc-gen-twirp_typescript latest
go: github.com/rynop/[email protected]: parsing go.mod: unexpected module path "go.larrymyers.com/protoc-gen-twirp_typescript"

Why am I getting this error? I thought the replace directive in go.mod allows for the forked modules go.mod to stay untouched.


Solution

  • You have the following replace:

    replace go.larrymyers.com/protoc-gen-twirp_typescript => github.com/rynop/protoc-gen-twirp_typescript master

    which if I've followed, is effectively replace originalname => forkname

    I think the issue is that you are importing using the name of the fork, rather than the original name:

    import (
        // protocol buffer compiler plugins
        _ "github.com/golang/protobuf/protoc-gen-go"
        _ "github.com/mwitkow/go-proto-validators/protoc-gen-govalidators"
        _ "github.com/twitchtv/twirp/protoc-gen-twirp"
        _ "github.com/rynop/protoc-gen-twirp_typescript"   <<<< PROBLEM, using fork name
    )
    

    The error message you see seems to be the go command complaining about that.

    I suspect it would work if you used the original name in the import statement:

    import (
        ...
        _ "go.larrymyers.com/protoc-gen-twirp_typescript"   <<<< original name
    )
    

    You should also run go list -m all to see the final selected versions, including it shows the outcome of any replace and exclude directives.