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

panic: runtime error: invalid memory address or nil pointer dereference #567

Closed
nfyxhan opened this issue Nov 22, 2019 · 19 comments
Closed

Comments

@nfyxhan
Copy link

nfyxhan commented Nov 22, 2019

Describe the bug
panic: runtime error: invalid memory address or nil pointer dereference when I use swag init --parseDependency

To Reproduce

export GO111MODULE=on
swag init --parseDependency
2019/11/22 12:04:01 Generate swagger docs....
2019/11/22 12:04:01 Generate general API Info, search dir:./
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x843d12]

goroutine 1 [running]:
github.com/swaggo/swag.(*Parser).getAllGoFileInfoFromDeps(0xc0002bce00, 0xc0005bda30, 0x0, 0x0)
	/root/pkg/mod/github.com/swaggo/[email protected]/parser.go:1323 +0x62
github.com/swaggo/swag.(*Parser).getAllGoFileInfoFromDeps(0xc0002bce00, 0xc00098f980, 0x46, 0x0)
	/root/pkg/mod/github.com/swaggo/[email protected]/parser.go:1341 +0x13e
github.com/swaggo/swag.(*Parser).getAllGoFileInfoFromDeps(0xc0002bce00, 0xc0007529a0, 0x0, 0x0)
	/root/pkg/mod/github.com/swaggo/[email protected]/parser.go:1341 +0x13e
github.com/swaggo/swag.(*Parser).getAllGoFileInfoFromDeps(0xc0002bce00, 0xc0007bb238, 0x0, 0x0)
	/root/pkg/mod/github.com/swaggo/[email protected]/parser.go:1341 +0x13e
github.com/swaggo/swag.(*Parser).getAllGoFileInfoFromDeps(0xc0002bce00, 0xc0005a9390, 0x0, 0x0)
	/root/pkg/mod/github.com/swaggo/[email protected]/parser.go:1341 +0x13e
github.com/swaggo/swag.(*Parser).getAllGoFileInfoFromDeps(0xc0002bce00, 0xc0007cd658, 0x0, 0x0)
	/root/pkg/mod/github.com/swaggo/[email protected]/parser.go:1341 +0x13e
github.com/swaggo/swag.(*Parser).getAllGoFileInfoFromDeps(0xc0002bce00, 0xc0009594f8, 0x0, 0x0)
	/root/pkg/mod/github.com/swaggo/[email protected]/parser.go:1341 +0x13e
github.com/swaggo/swag.(*Parser).getAllGoFileInfoFromDeps(0xc0002bce00, 0xc000919130, 0x0, 0x0)
	/root/pkg/mod/github.com/swaggo/[email protected]/parser.go:1341 +0x13e
github.com/swaggo/swag.(*Parser).ParseAPI(0xc0002bce00, 0x9a2ee1, 0x2, 0x9a60f9, 0x7, 0x0, 0xc0003314d0)
	/root/pkg/mod/github.com/swaggo/[email protected]/parser.go:130 +0x44c
github.com/swaggo/swag/gen.(*Gen).Build(0xc008e595de, 0xc008e59640, 0x0, 0x0)
	/root/pkg/mod/github.com/swaggo/[email protected]/gen/gen.go:66 +0x2c4
main.main.func1(0xc00022e2c0, 0x0, 0xc00011e310)
	/root/pkg/mod/github.com/swaggo/[email protected]/cmd/swag/main.go:45 +0x2ea
github.com/urfave/cli.HandleAction(0x8fc7a0, 0x9c7fc0, 0xc00022e2c0, 0xc00022e2c0, 0x0)
	/root/pkg/mod/github.com/urfave/[email protected]/app.go:523 +0xbe
github.com/urfave/cli.Command.Run(0x9a386f, 0x4, 0x0, 0x0, 0xc0002ed5b0, 0x1, 0x1, 0x9a9ff1, 0xe, 0x0, ...)
	/root/pkg/mod/github.com/urfave/[email protected]/command.go:174 +0x521
github.com/urfave/cli.(*App).Run(0xc0000941c0, 0xc000088150, 0x3, 0x3, 0x0, 0x0)
	/root/pkg/mod/github.com/urfave/[email protected]/app.go:276 +0x729
main.main()
	/root/pkg/mod/github.com/swaggo/[email protected]/cmd/swag/main.go:93 +0x65d

The annotation are as follows

import "k8s.io/api/core/v1"
// @Success 200 {object} v1.ConfigMap

Your swag version
swag version v1.6.3

Your go version
go version go1.12.7 linux/amd64

@ubogdan
Copy link
Contributor

ubogdan commented Nov 22, 2019

Please let me know if you swag was built with go get https://github.com/swaggo/swag or you downloaded it from release.

@nfyxhan
Copy link
Author

nfyxhan commented Nov 22, 2019

Please let me know if you swag was built with go get https://github.com/swaggo/swag or you downloaded it from release.

it is build with go get https://github.com/swaggo/swag

@hyacinthus
Copy link

swag init --parseDependency -g cmd/api/main.go
2019/12/11 10:00:12 Generate swagger docs....
2019/12/11 10:00:12 Generate general API Info, search dir:./
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x149a262]

goroutine 1 [running]:
github.com/swaggo/swag.(*Parser).getAllGoFileInfoFromDeps(0xc0000dd500, 0xc0005d7340, 0x0, 0x0)
/Users/yons/go/src/github.com/swaggo/swag/parser.go:1395 +0x62
github.com/swaggo/swag.(*Parser).getAllGoFileInfoFromDeps(0xc0000dd500, 0xc000376bc8, 0x0, 0x0)
/Users/yons/go/src/github.com/swaggo/swag/parser.go:1413 +0x13e
github.com/swaggo/swag.(*Parser).getAllGoFileInfoFromDeps(0xc0000dd500, 0xc00059ac20, 0x0, 0x0)
/Users/yons/go/src/github.com/swaggo/swag/parser.go:1413 +0x13e
github.com/swaggo/swag.(*Parser).ParseAPI(0xc0000dd500, 0x15c5881, 0x2, 0x7ffeefbff907, 0xf, 0x0, 0xc000485708)
/Users/yons/go/src/github.com/swaggo/swag/parser.go:134 +0x438
github.com/swaggo/swag/gen.(*Gen).Build(0xc000368550, 0xc0008c1800, 0x0, 0x0)
/Users/yons/go/src/github.com/swaggo/swag/gen/gen.go:74 +0x1a7
main.initAction(0xc0002d2160, 0x0, 0xc00016c310)
/Users/yons/go/src/github.com/swaggo/swag/cmd/swag/main.go:68 +0x2ea
github.com/swaggo/cli.HandleAction(0x1513b60, 0x15ecb00, 0xc0002d2160, 0xc0002d2160, 0x0)
/Users/yons/go/src/github.com/swaggo/cli/app.go:523 +0xbe
github.com/swaggo/cli.Command.Run(0x15c6234, 0x4, 0x0, 0x0, 0xc000368260, 0x1, 0x1, 0x15ccac8, 0xe, 0x0, ...)
/Users/yons/go/src/github.com/swaggo/cli/command.go:174 +0x51c
github.com/swaggo/cli.(*App).Run(0xc0002ca000, 0xc0000c0000, 0x5, 0x5, 0x0, 0x0)
/Users/yons/go/src/github.com/swaggo/cli/app.go:276 +0x718
main.main()
/Users/yons/go/src/github.com/swaggo/swag/cmd/swag/main.go:92 +0x1c4
make: *** [doc] Error 2

build swag from latest source

@hyacinthus
Copy link

@ubogdan do you have any idea for this error? Thank you.

@kolaente
Copy link
Contributor

I am also facing the same issue, is there any progress to this?

@kolaente
Copy link
Contributor

If I leave out --parseDependency it can't find all the types I put in my doc comments.

@kolaente
Copy link
Contributor

I changed parser.go:1409 so it won't panic but then I hit #534

@hyacinthus
Copy link

@kolaente
I created a CI job,
go mod vendor
swag init --parseVendor

vendor mod is OK.

@kolaente
Copy link
Contributor

@hyacinthus I was able to reproduce this with my OSS-project in a docker container:

~/go/src/code.vikunja.io/api   fix/swagger ●  docker run -v $PWD:/go/src/code.vikunja.io/api -it golang:1.13.7 bash      
root@98f57e0f8c93:/go# cd src/code.vikunja.io/api/
root@98f57e0f8c93:/go/src/code.vikunja.io/api# make do-the-swag
github.com/shurcooL/sanitized_anchor_name
github.com/mailru/easyjson/buffer
golang.org/x/text/transform
github.com/PuerkitoBio/urlesc
golang.org/x/text/unicode/bidi
gopkg.in/yaml.v2
github.com/mailru/easyjson/jlexer
github.com/KyleBanks/depth
github.com/mailru/easyjson/jwriter
golang.org/x/tools/go/ast/astutil
golang.org/x/tools/go/buildutil
golang.org/x/text/unicode/norm
golang.org/x/text/width
golang.org/x/tools/go/internal/cgo
github.com/russross/blackfriday/v2
golang.org/x/text/secure/bidirule
golang.org/x/tools/go/loader
golang.org/x/net/idna
github.com/ghodss/yaml
github.com/go-openapi/swag
github.com/cpuguy83/go-md2man/v2/md2man
github.com/PuerkitoBio/purell
github.com/urfave/cli
github.com/go-openapi/jsonpointer
github.com/go-openapi/jsonreference
github.com/go-openapi/spec
github.com/swaggo/swag
github.com/swaggo/swag/gen
github.com/swaggo/swag/cmd/swag
swag init --parseVendor --parseDependency -g pkg/routes/routes.go -o ./pkg/swagger;
2020/01/29 21:07:55 Generate swagger docs....
2020/01/29 21:07:55 Generate general API Info, search dir:./
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x85eb62]

goroutine 1 [running]:
github.com/swaggo/swag.(*Parser).getAllGoFileInfoFromDeps(0xc000298c40, 0xc00c2d2230, 0x0, 0x0)
	/go/src/code.vikunja.io/api/vendor/github.com/swaggo/swag/parser.go:1413 +0x62
github.com/swaggo/swag.(*Parser).getAllGoFileInfoFromDeps(0xc000298c40, 0xc00d14caf8, 0x0, 0x0)
	/go/src/code.vikunja.io/api/vendor/github.com/swaggo/swag/parser.go:1431 +0x13e
github.com/swaggo/swag.(*Parser).getAllGoFileInfoFromDeps(0xc000298c40, 0xc00c5b06c0, 0x0, 0x0)
	/go/src/code.vikunja.io/api/vendor/github.com/swaggo/swag/parser.go:1431 +0x13e
github.com/swaggo/swag.(*Parser).getAllGoFileInfoFromDeps(0xc000298c40, 0xc00eba2e68, 0x0, 0x0)
	/go/src/code.vikunja.io/api/vendor/github.com/swaggo/swag/parser.go:1431 +0x13e
github.com/swaggo/swag.(*Parser).ParseAPI(0xc000298c40, 0x9cb641, 0x2, 0x7fff7b175e8a, 0x14, 0x0, 0xc0003596f0)
	/go/src/code.vikunja.io/api/vendor/github.com/swaggo/swag/parser.go:134 +0x438
github.com/swaggo/swag/gen.(*Gen).Build(0xc00031c470, 0xc006b537f8, 0x0, 0x0)
	/go/src/code.vikunja.io/api/vendor/github.com/swaggo/swag/gen/gen.go:77 +0x1a7
main.initAction(0xc00024c2c0, 0x0, 0xc00024e480)
	/go/src/code.vikunja.io/api/vendor/github.com/swaggo/swag/cmd/swag/main.go:73 +0x33a
github.com/urfave/cli.HandleAction(0x9198c0, 0x9f3358, 0xc00024c2c0, 0xc00024c2c0, 0x0)
	/go/src/code.vikunja.io/api/vendor/github.com/urfave/cli/app.go:523 +0xbe
github.com/urfave/cli.Command.Run(0x9cbff0, 0x4, 0x0, 0x0, 0xc00031c060, 0x1, 0x1, 0x9d2aa2, 0xe, 0x0, ...)
	/go/src/code.vikunja.io/api/vendor/github.com/urfave/cli/command.go:174 +0x51c
github.com/urfave/cli.(*App).Run(0xc0002a6000, 0xc0000be000, 0x8, 0x8, 0x0, 0x0)
	/go/src/code.vikunja.io/api/vendor/github.com/urfave/cli/app.go:276 +0x725
main.main()
	/go/src/code.vikunja.io/api/vendor/github.com/swaggo/swag/cmd/swag/main.go:98 +0x1c4
make: *** [Makefile:191: do-the-swag] Error 2
root@98f57e0f8c93:/go/src/code.vikunja.io/api# 

(The make command installs swag from vendor in the correct version by running go install)

@hyacinthus
Copy link

@kolaente After go mod vendor cmd, don't use --parseDependency .

@kolaente
Copy link
Contributor

@hyacinthus Then swag is unable to find some struct from my dependencies, which is different every time I run it.

@hyacinthus
Copy link

@kolaente

After you download all your dep pkg to vendor folder, the swag will find your struct definition from vendor folder.

I don't like use vendor mod, so I run swag in a CI job, and only gen the swagger file, then I show it in another swagger UI project.

Now swag no longer invaded my code.

@kolaente
Copy link
Contributor

kolaente commented Jan 31, 2020

@hyacinthus You mean once I have all my dependencies in vendor/ swag will automatically find them? If so, shouldn't running swag init then be enough?
When I try this, swag is still unable to find some dependency struct which is located in my vendor/ folder.

When running swag with the --parseVendor and --parseDependency options enabled, it takes a lot longer but eventually hits the same issue.

@hyacinthus
Copy link

@kolaente

Try to use --parseVendor only , --parseDependency will hit this unknown issue sometimes in go module mod.

@kolaente
Copy link
Contributor

kolaente commented Feb 3, 2020

@hyacinthus When running with --parseVendor only, swag is still unable to find some dependency located in my vendor/ folder.

@maitredede
Copy link
Contributor

Hi,
It seams related to CGO.
When it SIGSEGV at https://github.com/swaggo/swag/blob/master/parser.go#L1452 :
pkg.Raw is nil and pkg.Name is "C".

@kolaente
Copy link
Contributor

With 1.6.6 released, it won't panic anymore, but swag is still unable to find dependencies in my vendor/ folder.

@ubogdan
Copy link
Contributor

ubogdan commented Sep 28, 2021

Closing: fixed.

@ubogdan ubogdan closed this as completed Sep 28, 2021
@rcodina-tp
Copy link

rcodina-tp commented Oct 24, 2024

I have swagg 1.16, and it generated this problem again, just out of nowhere. Using --parseVendor --parseDependency

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

6 participants