Skip to content

Instantly share code, notes, and snippets.

@wallyqs
Last active February 22, 2020 04:09
Show Gist options
  • Save wallyqs/7dfe08e6833b2075b5a947bb986949ab to your computer and use it in GitHub Desktop.
Save wallyqs/7dfe08e6833b2075b5a947bb986949ab to your computer and use it in GitHub Desktop.
NATS v2 & Go Modules

In case having issues during when using the recent versions of NATS such as:

go: github.com/nats-io/go-nats@v1.8.1: parsing go.mod: unexpected module path "github.com/nats-io/nats.go"
go: github.com/nats-io/go-nats-streaming@v0.5.0: parsing go.mod: unexpected module path "github.com/nats-io/stan.go"

To fix it:

  1. Update your go.mod using the latest tags, for example for both NATS and NATS Streaming clients:
module github.com/wallyqs/hello-nats-go-mod

go 1.12

require (
	github.com/nats-io/nats.go v1.8.1
	github.com/nats-io/stan.go v0.5.0
)

Or if want to import the NATS Server v2 to embed it, notice the /v2 after the nats-server module name. If that is not present, then go modules will not fetch it and would accidentally end up with 1.4.1 version of the server.

require (
	github.com/nats-io/nats-server/v2 v2.0.0
	github.com/nats-io/nats.go v1.8.1
)

If embedding both NATS Streaming and NATS Servers:

require (
	github.com/nats-io/nats-server/v2 v2.0.0 // indirect
	github.com/nats-io/nats-streaming-server v0.15.1
)	
  1. Next, update the imports within the repo:
find ./ -type f -name "*.go" -exec sed -i -e 's/github.com\/nats-io\/go-nats-streaming/github.com\/nats-io\/stan.go/g' {} \;

find ./ -type f -name "*.go" -exec sed -i -e 's/github.com\/nats-io\/go-nats/github.com\/nats-io\/nats.go/g' {} \;

find ./ -type f -name "*.go" -exec sed -i -e 's/github.com\/nats-io\/gnatsd/github.com\/nats-io\/nats-server\/v2/g' {} \;

find ./ -type f -name "*.go" -exec sed -i -e 's/github.com\/nats-io\/nats-server/github.com\/nats-io\/nats-server\/v2/g' {} \;
  1. (Recommended) Run Go fmt as that the rename will affect the proper ordering of the imports

Gotchas when using go get

If using go get for the client, then have to be careful with adding an extra slash at the end of the repo for example:

GO111MODULE=on go get github.com/nats-io/nats.go/@latest
GO111MODULE=on go get github.com/nats-io/nats.go/@v1.8.1

If trying to fetch the latest version of the server with go get, then have to add v2 at the end:

GO111MODULE=on go get github.com/nats-io/nats-server/v2@latest

Otherwise, go get will fetch the v1.4.1 version of the server, which is also named (gnatsd).

GO111MODULE=on go get github.com/nats-io/nats-server@latest
go: finding github.com/nats-io/gnatsd/server latest
go: finding golang.org/x/crypto/bcrypt latest
go: finding golang.org/x/crypto latest

In order to use an older tag, also have to use the old name otherwise it would results in go mod parsing errors.

# OK
GO111MODULE=on go get github.com/nats-io/go-nats/@v1.7.2

# Not OK
GO111MODULE=on go get github.com/nats-io/nats.go/@v1.7.2
go: finding github.com/nats-io/nats.go v1.7.2
go: downloading github.com/nats-io/nats.go v1.7.2
go: extracting github.com/nats-io/nats.go v1.7.2
go: finding github.com/nats-io/go-nats/encoders/builtin latest
go: finding github.com/nats-io/go-nats/util latest
go: finding github.com/nats-io/go-nats/encoders latest
go: finding github.com/nats-io/go-nats v1.8.1
go: downloading github.com/nats-io/go-nats v1.8.1
go: extracting github.com/nats-io/go-nats v1.8.1
go: github.com/nats-io/go-nats@v1.8.1: parsing go.mod: unexpected module path "github.com/nats-io/nats.go"
go: error loading module requirements
@danf0rth
Copy link

danf0rth commented Jun 7, 2019

Unfortunately, this not helps, i still get following errors:

go: github.com/nats-io/go-nats@v1.8.1: parsing go.mod: unexpected module path "github.com/nats-io/nats.go"
...
go get: error loading module requirements

@wallyqs
Copy link
Author

wallyqs commented Jun 7, 2019

@danf0rth do you have any imports with the old repo github.com/nats-io/go-nats? Can you share your go.mod?

@danf0rth
Copy link

danf0rth commented Jun 8, 2019

@wallyqs

I am not sure, i run your cmds to replace old imports with new one, but in go.mod i still see the old version.

module github.com/danf0rth/sender

go 1.12

require (
	cloud.google.com/go v0.40.0 // indirect
	github.com/SherClockHolmes/webpush-go v1.1.0
	github.com/danf0rth/subscription v0.0.0-20190608070909-c58f63f9dd95

	github.com/go-sql-driver/mysql v1.4.1
	github.com/go-yaml/yaml v2.1.0+incompatible
	github.com/golang/mock v1.3.1 // indirect
	github.com/google/btree v1.0.0 // indirect
	github.com/google/pprof v0.0.0-20190515194954-54271f7e092f // indirect
	github.com/google/wire v0.2.2 // indirect
	github.com/kisielk/errcheck v1.2.0 // indirect
	github.com/kr/pty v1.1.4 // indirect
	github.com/nats-io/go-nats v1.7.2 // indirect
	github.com/nats-io/nats.go v1.8.1
	github.com/pkg/errors v0.8.1
	github.com/stretchr/objx v0.2.0 // indirect
	github.com/uber-go/zap v1.10.0 // indirect
	go.opencensus.io v0.22.0 // indirect
	go.uber.org/zap v1.10.0
	golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522 // indirect
	golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff // indirect
	golang.org/x/mobile v0.0.0-20190607214518-6fa95d984e88 // indirect
	golang.org/x/mod v0.1.0 // indirect
	golang.org/x/net v0.0.0-20190607181551-461777fb6f67 // indirect
	golang.org/x/sys v0.0.0-20190608050228-5b15430b70e3 // indirect
	golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
	golang.org/x/tools v0.0.0-20190608022120-eacb66d2a7c3 // indirect
	golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522 // indirect
	honnef.co/go/tools v0.0.0-20190607181801-497c8f037f5a // indirect
)

I also did go mod tidy, doesn't helps.

@wallyqs
Copy link
Author

wallyqs commented Jun 8, 2019

@danf0rth check whether there is another dependency that it may bringing the old import (for example maybe github.com/danf0rth/subscription?)
This is because I notice the following lines:

	github.com/nats-io/go-nats v1.7.2 // indirect
	github.com/nats-io/nats.go v1.8.1

Saw from the // indirect comment, it looks like it is coming from another dependency.

Does go mod why github.com/nats-io/go-nats show any output?

@wallyqs
Copy link
Author

wallyqs commented Jun 8, 2019

@danf0rth if that is the case, then you have to first update the dependency and the require entry in the go.mod to point to the new import path

@danf0rth
Copy link

danf0rth commented Jun 8, 2019

@wallyqs
go mod why say:

# github.com/nats-io/go-nats
(main module does not need package github.com/nats-io/go-nats)

Furthermore, the github.com/danf0rth/subscription package also was updated to use latest package of NATS, and there is no such error for this package when i do go get -u ./...

module github.com/danf0rth/subscription

go 1.12

require (
	github.com/Masterminds/squirrel v1.1.0
	github.com/danf0rth/feeds v0.0.0-20190605091646-c3e0a0b899c0
	github.com/go-chi/chi v4.0.2+incompatible
	github.com/go-redis/redis v6.15.2+incompatible
	github.com/go-sql-driver/mysql v1.4.1
	github.com/go-yaml/yaml v2.1.0+incompatible
	github.com/gogo/protobuf v1.2.1
	github.com/kr/pretty v0.1.0 // indirect
	github.com/lib/pq v1.1.1 // indirect
	github.com/mattn/go-sqlite3 v1.10.0 // indirect
	github.com/nats-io/nats-server/v2 v2.0.0 // indirect
	github.com/nats-io/nats.go v1.8.1
	github.com/onsi/ginkgo v1.8.0 // indirect
	github.com/onsi/gomega v1.5.0 // indirect
	github.com/pkg/errors v0.8.1
	github.com/stretchr/testify v1.3.0 // indirect
	go.uber.org/atomic v1.4.0 // indirect
	go.uber.org/multierr v1.1.0 // indirect
	go.uber.org/zap v1.10.0
	golang.org/x/net v0.0.0-20190606173856-1492cefac77f // indirect
	golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444 // indirect
	google.golang.org/appengine v1.6.1 // indirect
	google.golang.org/genproto v0.0.0-20190605220351-eb0b1bdb6ae6 // indirect
	google.golang.org/grpc v1.21.1
	gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
	gopkg.in/yaml.v2 v2.2.2 // indirect
)

Thanks for your feedback.

@wallyqs
Copy link
Author

wallyqs commented Jun 8, 2019

Thanks @danf0rth , that last go modules file looks good to me... We're also now looking at this issue and maybe recreating some of the old repos which may help: nats-io/nats.go#483

@wallyqs
Copy link
Author

wallyqs commented Jun 8, 2019

@danf0rth there is now a pre go mod support version of the repo with the old name: https://github.com/nats-io/go-nats I think this may help in your case, could you let me know if this fixed it for you?

@danf0rth
Copy link

@wallyqs, now when i do go get -u ./... there is no errors, so it seems like the problem is solved. I am not sure what exactly solves the problem. Thanks for your help! NATS is awesome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment