Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined: "github.com/swaggo/swag".Spec #1126

Closed
bashdrew opened this issue Feb 3, 2022 · 29 comments
Closed

undefined: "github.com/swaggo/swag".Spec #1126

bashdrew opened this issue Feb 3, 2022 · 29 comments

Comments

@bashdrew
Copy link

bashdrew commented Feb 3, 2022

hi, i have been using this repo for a while now. just a few hours ago, it suddenly threw out the error below:

docs/docs.go:11152:28: undefined: "github.com/swaggo/swag".Spec

may i know how to get rid of this error? thank you.

@momarques
Copy link

same for me here, it's been a while since I last used it. maybe it's something with v1.7.9?

@bashdrew
Copy link
Author

bashdrew commented Feb 4, 2022

Yes, I switched back 1.7.8 and it worked.

@ubogdan
Copy link
Contributor

ubogdan commented Feb 4, 2022

Please fill in the report on which swagger handler are u using and which version.
example
echo-swagger 1.0.0

We will investigate and will come back with a solution ASAP.

@mehulgohil
Copy link

Yes getting the same error while initializing swagger in docker

I am using golang:1.16

image

@ubogdan
Copy link
Contributor

ubogdan commented Feb 4, 2022

@mehulgohil Thanks for letting us know, but you are not helping with so less details.

~ go get -u github.com/swaggo/swag/cmd/swag
go: downloading golang.org/x/tools v0.1.9
go: downloading github.com/urfave/cli v1.22.5
go: downloading github.com/go-openapi/swag v0.21.1
go: downloading github.com/cpuguy83/go-md2man/v2 v2.0.1
go: downloading github.com/cpuguy83/go-md2man v1.0.10
go: downloading github.com/russross/blackfriday/v2 v2.1.0
go: downloading github.com/mailru/easyjson v0.7.7
go: downloading golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd
go: downloading github.com/russross/blackfriday v1.6.0
go: downloading golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27
go get: installing executables with 'go get' in module mode is deprecated.
	Use 'go install pkg@version' instead.
	For more information, see https://golang.org/doc/go-get-install-deprecation
	or run 'go help get' or 'go help install'.

~ swag -v
swag version v1.7.9

~ go version
go version go1.17.5 darwin/amd64

@AxelDum
Copy link

AxelDum commented Feb 4, 2022

I had the same issue I was installing swag and generating docs in docker with a go.mod not necessarily matching latest

In docker file i have:

FROM golang:1.17-alpine
RUN go mod download
RUN go install github.com/swaggo/swag/cmd/swag@latest
RUN swag init
RUN go build

Upgrading lib in go.mod seems to resolve the issue :

  • from swag v1.7.6 to swag v1.7.9
  • from echo-swagger v1.1.4 to echo-swagger v1.2.0

Error in CI was :

 [build-env 11/11] RUN GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/app
# git.example.com/repo/app/docs
#  docs/docs.go:3343:28: undefined: "github.com/swaggo/swag".Spec
#ERROR: executor failed running [/bin/sh -c GOOS=linux GOARCH=amd64 go build -ldflags="-w -s" -o /go/bin/app]: exit code: 2

@daniel-vera-g
Copy link

daniel-vera-g commented Feb 4, 2022

Having the same issue as described above using swagger-http handler:

  • swagger-http: v1.2.0
  • swag: v1.7.9
  • go: go1.17.6 darwin/amd64

Had a similar usage like AxelDum described, but upgrading the go.mod did not help.

swag init with v1.7.9 causes undefined: docs.SwaggerInfo. The v1.7.8 version works.

@bashdrew
Copy link
Author

bashdrew commented Feb 4, 2022

  • github.com/swaggo/http-swagger v1.2.0
  • github.com/swaggo/files v0.0.0-20210815190702-a29dd2bc99b2

@ubogdan
Copy link
Contributor

ubogdan commented Feb 4, 2022

swag init with v1.7.9 causes undefined: docs.SwaggerInfo. The v1.7.8 version works.

Does make any difference if you remove the docs.go from the generated documentation before running swag init?

The expected behavior should be

~ go build
docs/docs.go:61:28: undefined: "github.com/swaggo/swag".Spec

@ubogdan
Copy link
Contributor

ubogdan commented Feb 4, 2022

@AxelDum I can reproduce the issue. We will release an update for the http-swagger package asap.

https://github.com/ubogdan/http-swagger-example

@daniel-vera-g
Copy link

daniel-vera-g commented Feb 4, 2022

swag init with v1.7.9 causes undefined: docs.SwaggerInfo. The v1.7.8 version works.

Does make any difference if you remove the docs.go from the generated documentation before running swag init?

The expected behavior should be

~ go build
docs/docs.go:61:28: undefined: "github.com/swaggo/swag".Spec

Using cmd/swag@latest:

  • I get undefined: docs.SwaggerInfo when I use the newest versions of http-swagger and swag in my go.mod. I do this, by following the descriptions in the README
  • I get undefined: "github.com/swaggo/swag".Spec when my go.mod has an older version of the packages(http-swagger v1.1.2)

I see, that in your example you're not using the latest swag version in your go mod: https://github.com/ubogdan/http-swagger-example/blob/master/go.mod#L18

In this case, I also get your expected behavior.

@ubogdan
Copy link
Contributor

ubogdan commented Feb 4, 2022

Please see the fixed branch.

What version of go are u using ? For go > 1.16 the correct install command is
go install github.com/swaggo/swag/cmd/swag@v1.7.9 for 1.7.9 version and go install github.com/swaggo/swag/cmd/swag@latest for the latest version (today is 1.7.9).

@daniel-vera-g
Copy link

Please see the fixed branch.

What version of go are u using ? For go > 1.16 the correct install command is go install github.com/swaggo/swag/cmd/swag@v1.7.9 for 1.7.9 version and go install github.com/swaggo/swag/cmd/swag@latest for the latest version (today is 1.7.9).

Ah well sorry, was probably my fault.

I oversaw, that v1.7.9 introduced the instance Name (#1116 ) . I was using the config as described in the README (docs.SwaggerInfo.Version = ... ) and didn't notice that the default value for the instance was swagger.

You mentioned in his Issue :

that's a great idea but we can't break compatibility.
This may be a feature to add maybe in v2.x.x.

Did I overlook something or is this a breaking change by mistake?

@ubogdan
Copy link
Contributor

ubogdan commented Feb 5, 2022

@daniel-vera-g Yes, it was by mistake.

The issue was caused by the need of having 2 generated documents in the same docs package. Keeping original SwaggerInfo is not possible because when it's used with the new feature the code will not compile at all complaining SwaggerInfo has been declared.

Please open a separate issue regarding SwaggerInfo. I've started to work on a fix an I will like to have a clear reference.

@mahbubzulkarnain
Copy link

swag init with v1.7.9 causes undefined: docs.SwaggerInfo. The v1.7.8 version works.

@jihoon-seo
Copy link

@ubogdan Thank you for your devotion to this project. 😊

I think there exist 2 problems for now.

Problem 1: go install github.com/swaggo/swag/cmd/swag@latest installs v1.7.9, not v1.7.9-p1.

❯ go install github.com/swaggo/swag/cmd/swag@latest
go: downloading github.com/swaggo/swag v1.7.9
go: downloading github.com/go-openapi/spec v0.20.4
go: downloading golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d

❯ swag -v
swag version v1.7.9

❯ make swag
❯ make
go build -mod=mod -o cb-tumblebug
# github.com/cloud-barista/cb-tumblebug/src/api/rest/docs
api/rest/docs/docs.go:6912:28: undefined: "github.com/swaggo/swag".Spec
Makefile:2: recipe for target 'default' failed
make: *** [default] Error 2

This is because:
(From go help get)

...
For each named package or package pattern, get must decide which version of
the corresponding module to use. By default, get looks up the latest tagged
release version, such as v0.4.5 or v1.2.3. If there are no tagged release
versions, get looks up the latest tagged pre-release version, such as
v0.0.1-pre1. If there are no tagged versions at all, get looks up the latest
known commit. If the module is not already required at a later version
(for example, a pre-release newer than the latest release), get will use
the version it looked up. Otherwise, get will use the currently
required version.

That is, go commands like go install judge that v1.7.9-p1 is an pre-release,
and v1.7.9 is the latest tagged release version,
so these will pick v1.7.9 as latest instead of v1.7.9-p1.


Problem 2: Even if I manually install v1.7.9-p1, the build still fails.
(Note that swag says its version is v1.7.9 not v1.7.9-p1,
but I think that this is just because the Version string in version.go is not changed in #1127.)

❯ go install github.com/swaggo/swag/cmd/swag@v1.7.9-p1
go: downloading github.com/swaggo/swag v1.7.9-p1

❯ swag -v
swag version v1.7.9

❯ make swag
❯ make
go build -mod=mod -o cb-tumblebug
# github.com/cloud-barista/cb-tumblebug/src/api/rest/docs
api/rest/docs/docs.go:6912:20: undefined: "github.com/swaggo/swag".Spec
Makefile:2: recipe for target 'default' failed
make: *** [default] Error 2

So I recommend these:

  1. If #1127 did not completely fix this issue (#1126), then add more fixes.
  2. Retract v1.7.9 and v1.7.9-p1 by adding these lines to go.mod:
retract (
	v1.7.9
	v1.7.9-p1
)
  1. Publish a new release like v1.7.10

I hope this recommendation actually works! 😊

@ubogdan
Copy link
Contributor

ubogdan commented Feb 9, 2022

@jihoon-seo The appreciation I get from guys like you motivates me to go forward. ❤️

The change made in 1.7.9-p1 only affects swag, not packages depending on it. The swag -version output is a string saved into version.go and wasn't updated during the p1 release, but this doesn't affect how GO modules are working.

I will look at the provided examples and figure out a way to solve this.

regarding
❯ make swag
❯ make

go build -mod=mod -o cb-tumblebug
# github.com/cloud-barista/cb-tumblebug/src/api/rest/docs
api/rest/docs/docs.go:6912:28: undefined: "github.com/swaggo/swag".Spec
Makefile:2: recipe for target 'default' failed

I guess make swag runs swag init --some params
Before running make (go build) you need to run go get github.com/swaggo/echo-swagger so the version of echo-swagger is used and this has a dependecy of swag 1.7.9. or more simple update this manually in go.mod and run go mod tidy.

Please let me know if this helps.

@bmpotter
Copy link

bmpotter commented Feb 9, 2022

@ubogdan Thanks for the excellent swaggo project! I just want to report that I had the same problem as @jihoon-seo , but following your instructions solved it:

go get github.com/swaggo/echo-swagger
go install github.com/swaggo/swag/cmd/swag@v1.7.9-p1

@Abdurashidjon
Copy link

I have ParseComment error in file problem I have go-go1.17.1 version above did not help me

@ubogdan
Copy link
Contributor

ubogdan commented Apr 14, 2022

@Abdurashidjon Please open a separate issue and describe the issue you are experiencing in detail.

@Abdurashidjon
Copy link

@ubogdan I have a ParseComment problem when I run a swag

@ubogdan
Copy link
Contributor

ubogdan commented Apr 14, 2022

This issue is related to undefined: "github.com/swaggo/swag".Spec.

@Abdurashidjon
Copy link

@ubogdan
yes so how can i solve it

@ubogdan
Copy link
Contributor

ubogdan commented Apr 15, 2022

You need to update your swag HTTP handler to the latest version. If you use echo-swagger then the following command should be enough.

go get github.com/swaggo/echo-swagger

@Abdurashidjon
Copy link

@ubogdan
it didn’t help me go get github.com/swaggo/echo-swagger

@ubogdan
Copy link
Contributor

ubogdan commented Apr 16, 2022

@Abdurashidjon I'm sorry but I can't help you with 0 details. Please open an issue using the tempalte.

@Abdurashidjon
Copy link

2022/04/16 13:44:36 ParseComment error in file /home/abdurashid/go/src/....../action.go :cannot find type definition: modul.CreateResponse
make: *** [Makefile:61: swag_init] Error 1

@ubogdan is giving such an error and I did make swag_init. But it repeats such a mistake

@Abdurashidjon
Copy link

@ubogdan

@ubogdan
Copy link
Contributor

ubogdan commented Apr 18, 2022

I told you to open a separate issue. Stop mentioning me.

@ubogdan ubogdan closed this as completed Jun 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants