stretchr / testify

A toolkit with common assertions and mocks that plays nicely with the standard library
MIT License
23.58k stars 1.6k forks source link

go generate ./... broken on Go 1.12+ #841

Closed leighmcculloch closed 4 years ago

leighmcculloch commented 5 years ago

The repository uses go generate ./... to generate the required and assertion formatter files, but when following the instructions in the README.md and following the example in .travisci.gogenerate.sh to generate the files with either Go 1.12.13 or 1.13.4, the following errors occur:

2019/11/09 22:18:23 open /home/leighmcculloch/devel/testify/src/github.com/stretchr/testify/assert/assertion_format.go: no such file or directory
exit status 1
assert/assertions.go:25: running "go": exit status 1
2019/11/09 22:18:24 open /home/leighmcculloch/devel/testify/src/github.com/stretchr/testify/assert/assertion_format.go: no such file or directory
exit status 1
require/forward_requirements.go:16: running "go": exit status 1

It looks like this code in the generator that looks up where the code lives is finding the code path incorrectly in these newer versions of Go that support Go modules. The code is located on my system outside a GOPATH.

https://github.com/stretchr/testify/blob/e442a1631a73174a4afde5b4c41ba21cde6f6889/_codegen/main.go#L174-L184

If I try moving the project to my GOPATH, I get a new set of errors:

2019/11/09 22:23:48 assertions.go:20:2: could not import github.com/davecgh/go-spew/spew (can't find import: "github.com/stretchr/testify/vendor/github.com/davecgh/go-spew/spew")
exit status 1
assert/assertions.go:25: running "go": exit status 1
2019/11/09 22:23:48 assertions.go:20:2: could not import github.com/davecgh/go-spew/spew (can't find import: "github.com/stretchr/testify/vendor/github.com/davecgh/go-spew/spew")
exit status 1
require/forward_requirements.go:16: running "go": exit status 1
boyan-soubachov commented 5 years ago

As we spoke about in #848, happy to work on a solution for this to make it a bit less manual.

An initial thought I had is to add a git commit hook for any code modifying the 'base' files to automatically (and correctly) run go generate. That way, commits should automatically have up-to-date versions of the generated files.

Thoughts? @glesica , @leighmcculloch

leighmcculloch commented 5 years ago

I saw your message on the PR but I haven't had a chance to retry the process as you described.

I think part of the issue is the generate process being dependent on a GOPATH and with Go Modules disabled. Testify is widely used, but still only runs its CI tests on Go 1.11 as the latest release which is pretty old and no longer supported. Rather than adding a githook I think it probably needs:

  1. To be updated to work with Go Modules / Go 1.13+.
  2. For a CI run to be added that executes go generate and fails if there are any diffs.
boyan-soubachov commented 5 years ago

As far as I am aware, the travis file is set to use go modules (on and off) as well as the latest golang:

(from .travis.yml)

  include:
    - go: "1.8.x"
    - go: "1.9.x"
    - go: "1.10.x"
    - go: "1.11.x"
      env: GO111MODULE=off
    - go: "1.11.x"
      env: GO111MODULE=on
    - go: tip

The CI run does do a go generate./... as part of it (.travis.gogenerate.sh). The reason that succeeds is because the repo is cloned outside of the $GOPATH in the travis run if I'm not mistaken.

I do completely agree that this is an issue though; it's very reasonable to expect the community to go get this repo and work on it from where it is by default ($GOPATH/src). It would only encourage more collaboration and contributions.

So I think it would make sense to update our tooling to allow a very easy and working 'default-case' experience.

glesica commented 5 years ago

Agreed, if someone can come up with something that works reliably I'd be excited to get it merged and released!

boyan-soubachov commented 5 years ago

@leighmcculloch , more specifically, to get go generate ./... working:

rm -rf ./vendor
unset GO111MODULE
go get ./...
glesica commented 5 years ago

Can we throw that in the README or something? If no one else gets to it I'll try to remember to add it.

boyan-soubachov commented 5 years ago

I just put out a PR (#851), that would make the README.md instructions work correctly.

leighmcculloch commented 4 years ago

@boyan-soubachov I've tried to run your instructions with Go 1.13 but I'm hitting the same error. What version of Go are you running those commands on? Could you post a complete example running inside a clean container?

I don't think this is an issue with just my environment either. I'm able to replicate the error using the default Go Dockerfile's with Go 1.11 and Go 1.13:

go generate using Go 1.11 ``` $ docker run -it golang:1.11 root@ad2ebfb0a584:/go# cd .. root@ad2ebfb0a584:/# mkdir workdir root@ad2ebfb0a584:/# git clone https://github.com/stretchr/testify root@ad2ebfb0a584:/# cd testify/ root@ad2ebfb0a584:/testify# go generate ./... go: finding github.com/pmezard/go-difflib v1.0.0 go: finding github.com/davecgh/go-spew v1.1.0 go: finding github.com/stretchr/objx v0.1.0 go: finding gopkg.in/yaml.v2 v2.2.2 go: finding gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 go: downloading github.com/pmezard/go-difflib v1.0.0 go: downloading github.com/stretchr/objx v0.1.0 go: downloading github.com/davecgh/go-spew v1.1.0 go: downloading gopkg.in/yaml.v2 v2.2.2 go: finding github.com/ernesto-jimenez/gogen/imports latest go: finding github.com/ernesto-jimenez/gogen latest go: downloading github.com/ernesto-jimenez/gogen v0.0.0-20180125220232-d7d4131e6607 2019/12/07 06:15:32 open github.com/stretchr/testify/assert/assertion_format.go: no such file or directory exit status 1 assert/assertions.go:25: running "go": exit status 1 2019/12/07 06:15:33 open github.com/stretchr/testify/assert/assertion_format.go: no such file or directory exit status 1 require/forward_requirements.go:16: running "go": exit status 1 ```
go generate using Go 1.13 ``` $ docker run -it golang:1.13 root@732beb317da5:/go# cd .. root@732beb317da5:/# mkdir workdir root@732beb317da5:/# git clone https://github.com/stretchr/testify root@732beb317da5:/# cd testify/ root@732beb317da5:/testify# go generate ./... go: downloading gopkg.in/yaml.v2 v2.2.2 go: downloading github.com/davecgh/go-spew v1.1.0 go: downloading github.com/pmezard/go-difflib v1.0.0 go: downloading github.com/stretchr/objx v0.1.0 go: extracting gopkg.in/yaml.v2 v2.2.2 go: extracting github.com/stretchr/objx v0.1.0 go: extracting github.com/davecgh/go-spew v1.1.0 go: extracting github.com/pmezard/go-difflib v1.0.0 go: finding github.com/ernesto-jimenez/gogen latest go: downloading github.com/ernesto-jimenez/gogen v0.0.0-20180125220232-d7d4131e6607 go: extracting github.com/ernesto-jimenez/gogen v0.0.0-20180125220232-d7d4131e6607 2019/12/07 06:09:55 open /testify/src/github.com/stretchr/testify/assert/assertion_format.go: no such file or directory exit status 1 assert/assertions.go:25: running "go": exit status 1 2019/12/07 06:09:55 open /testify/src/github.com/stretchr/testify/assert/assertion_format.go: no such file or directory exit status 1 require/forward_requirements.go:16: running "go": exit status 1 ```

I also don't think go generate is running on Travis CI. The ./.travis.gogenerate.sh script that is included in .travis.yml only runs on Go 1.4 and 1.5 because the script contains this guard: https://github.com/stretchr/testify/blob/7009337cb5726e58a8444528a492b85d111bc1a5/.travis.gogenerate.sh#L3-L4

boyan-soubachov commented 4 years ago

@leighmcculloch , good point. I missed that in the .go.generate.sh script. I'll look into fixing that and make sure it runs on the latest go version.

As for the usual go generate ./... errors, the cause is explained in more detail in (#851 ). Please let me know if that's not the case for you.

leighmcculloch commented 4 years ago

I think I have a fix actually that makes this works for modules, will open a PR if I can wrangle this.

leighmcculloch commented 4 years ago

Fix is open in #852. It was pretty gnarly!