Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 639
rynop Avatar asked Jun 19 '19 15:06

rynop


People also ask

Is GO111MODULE enabled by default?

With the release of Go 1.16, the GO111MODULE variable now defaults to on which means that module-aware mode is enabled by default regardless of whether a go. mod file is present in the current directory. If you want to revert to the previous behavior, set GO111MODULE to auto .

How do I run a go module?

Open a command prompt and cd to your home directory. Create a greetings directory for your Go module source code. Start your module using the go mod init command. Run the go mod init command, giving it your module path -- here, use example.com/greetings .

What is go GO111MODULE?

GO111MODULE is an environment variable that can be set when using go for changing how Go imports packages. One of the first pain-points is that depending on the Go version, its semantics change.


1 Answers

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.

like image 52
thepudds Avatar answered Oct 17 '22 10:10

thepudds