go-swagger: Generate spec does not work with Go Modules

Problem statement

the swagger CLI tool does not work correctly when the target repository is a Go 1.11 Module. The tool should not use GOPATH/src to resolve dependencies, and instead use the GOPATH/pkg/mod The possible failure cases I have discovered are as follows:

  1. required dependency is not in GOPATH
    1. results in the CLI tool saying it cannot find dependency
  2. dependency in GOPATH is not the same as declared in MODULE
    1. individual types/functions/packages fail, rather than the dependency as a whole

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 28
  • Comments: 19 (4 by maintainers)

Most upvoted comments

I had success in my module-based project by doing what @bserdar did and adding the following twist:

# start inside the module-based project's repo (which is outside GOPATH)
cd /path/to/my/go/module/project-repo

# stores all the go.mod dependencies in `./vendor`
go mod vendor 

# get inside my module-based project's repo (which is now also inside GOPATH)
cd $GOPATH/src/path/to/my/symbolic/link/project-repo

# The `vendor` folder will now satisfy our unfilled deps... no more errors! \o/
swagger generate spec -o spec.yaml

Guys, is there any progress with modules? If no, how could I get around this issue?

Ugly workaround: I have my source tree outside GOPATH, and a symlink to it under GOPATH. I use the version under GOPATH to build swagger specs, and the original for real work.

I don’t think so. It will work if the project you are running on is in the GOPATH and you have GO111MODULE=off. However, if you have GO111MODULE=on or your project is not in the GOPATH it will fail. It will continue to fail until they use a different library for parsing go code.

On Wed, Jan 23, 2019 at 8:41 AM Amireza Fatemi notifications@github.com wrote:

just got the latest version yesterday and seems it works fine generating module base Go project. maybe the issue can be closed ?

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/go-swagger/go-swagger/issues/1681#issuecomment-456874297, or mute the thread https://github.com/notifications/unsubscribe-auth/AonV2l6iIaVHJxejkV9lo7h9fkLMmCN4ks5vGJDGgaJpZM4WWv7t .