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

crash on version 1.8.5 when running 'swag init --parseDependency' #1319

Open
sunrui opened this issue Sep 6, 2022 · 19 comments
Open

crash on version 1.8.5 when running 'swag init --parseDependency' #1319

sunrui opened this issue Sep 6, 2022 · 19 comments

Comments

@sunrui
Copy link

sunrui commented Sep 6, 2022

hi, i found a bug content as follows

swag init --parseDependency
2022/09/06 21:11:42 Generate swagger docs....
2022/09/06 21:11:42 Generate general API Info, search dir:./
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x8 pc=0x102814d20]

goroutine 1 [running]:
github.com/swaggo/swag.(*PackagesDefinitions).parseFunctionScopedTypesFromFile(0x1400000e858, 0x14001732880, {0x1400037d8f0, 0x15}, 0x140026b3ad0)
go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:168 +0x90
github.com/swaggo/swag.(*PackagesDefinitions).ParseTypes(0x1400000e858)
go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:110 +0xa0
github.com/swaggo/swag.(*Parser).ParseAPIMultiSearchDir(0x140001b42a0, {0x140001da0b0?, 0x1?, 0x0?}, {0x10283f25a?, 0x7?}, 0x64)
go/pkg/mod/github.com/swaggo/swag@v1.8.5/parser.go:362 +0x29c
github.com/swaggo/swag/gen.(*Gen).Build(0x140001ad980, 0x140001aeea0)
go/pkg/mod/github.com/swaggo/swag@v1.8.5/gen/gen.go:177 +0x4a0
main.initAction(0x140001418c0?)
go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:151 +0x654
github.com/urfave/cli/v2.(*Command).Run(0x14000000fc0, 0x140001c6340)
go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/command.go:163 +0x4fc
github.com/urfave/cli/v2.(*App).RunContext(0x1400011d6c0, {0x1029f5630?, 0x140000300b8}, {0x140000201b0, 0x3, 0x3})
go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:313 +0x860
github.com/urfave/cli/v2.(*App).Run(...)
go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:224
main.main()
go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:221 +0x65c

@sunrui sunrui changed the title hi, i found a bug content as follows. crash on version 1.8.5 when running 'swag init --parseDependency' Sep 6, 2022
@li1234yun
Copy link

Me too

1 similar comment
@heafox
Copy link

heafox commented Sep 7, 2022

Me too

@ShinJustinHolly3317
Copy link

use 1.8.4 instead, that's current stable version.
and some people already working on it

@mstrYoda
Copy link
Contributor

mstrYoda commented Sep 8, 2022

[Duplicate] Same on here: #1309

Fixed with the merged pr: #1310

You can use latest stable version v1.8.4.

@movpushmov
Copy link

[Duplicate] Same on here: #1309

Fixed with the merged pr: #1310

You can use latest stable version v1.8.4.

@mstrYoda

v1.8.4 doesn't work for me.

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x8 pc=0x10065cfc8]

goroutine 1 [running]:
github.com/swaggo/swag.(*PackagesDefinitions).parseFunctionScopedTypesFromFile(0x140001a8828, 0x140031c2a80, {0x140003bf8f0, 0x29}, 0x140057223c0)
/Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:168 +0x98
github.com/swaggo/swag.(*PackagesDefinitions).ParseTypes(0x140001a8828)
/Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:110 +0xa0
github.com/swaggo/swag.(*Parser).ParseAPIMultiSearchDir(0x140002602a0, {0x1400028e060?, 0x1?, 0x0?}, {0x16fb93765?, 0xe?}, 0x64)
/Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/parser.go:362 +0x2b0
github.com/swaggo/swag/gen.(*Gen).Build(0x14000259980, 0x1400025f380)
/Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/gen/gen.go:177 +0x4a8
main.initAction(0x1400022b8c0?)
/Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:151 +0x658
github.com/urfave/cli/v2.(*Command).Run(0x140002298c0, 0x1400026e4c0)
/Users/movpushmov/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/command.go:163 +0x50c
github.com/urfave/cli/v2.(*App).RunContext(0x14000251520, {0x10082d670?, 0x140001b4008}, {0x140001b8000, 0x9, 0x9})
/Users/movpushmov/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:313 +0x888
github.com/urfave/cli/v2.(*App).Run(...)
/Users/movpushmov/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:224
main.main()
/Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:221 +0x660
make: *** [generate-swagger] Error 2

@mstrYoda
Copy link
Contributor

mstrYoda commented Sep 12, 2022

[Duplicate] Same on here: #1309
Fixed with the merged pr: #1310
You can use latest stable version v1.8.4.

@mstrYoda

v1.8.4 doesn't work for me.

panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x2 addr=0x8 pc=0x10065cfc8]

goroutine 1 [running]: github.com/swaggo/swag.(*PackagesDefinitions).parseFunctionScopedTypesFromFile(0x140001a8828, 0x140031c2a80, {0x140003bf8f0, 0x29}, 0x140057223c0) /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:168 +0x98 github.com/swaggo/swag.(*PackagesDefinitions).ParseTypes(0x140001a8828) /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/packages.go:110 +0xa0 github.com/swaggo/swag.(*Parser).ParseAPIMultiSearchDir(0x140002602a0, {0x1400028e060?, 0x1?, 0x0?}, {0x16fb93765?, 0xe?}, 0x64) /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/parser.go:362 +0x2b0 github.com/swaggo/swag/gen.(*Gen).Build(0x14000259980, 0x1400025f380) /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/gen/gen.go:177 +0x4a8 main.initAction(0x1400022b8c0?) /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:151 +0x658 github.com/urfave/cli/v2.(*Command).Run(0x140002298c0, 0x1400026e4c0) /Users/movpushmov/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/command.go:163 +0x50c github.com/urfave/cli/v2.(*App).RunContext(0x14000251520, {0x10082d670?, 0x140001b4008}, {0x140001b8000, 0x9, 0x9}) /Users/movpushmov/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:313 +0x888 github.com/urfave/cli/v2.(*App).Run(...) /Users/movpushmov/go/pkg/mod/github.com/urfave/cli/v2@v2.3.0/app.go:224 main.main() /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:221 +0x660 make: *** [generate-swagger] Error 2

Are you sure?

Your error message says that you are still using v1.8.5: /Users/movpushmov/go/pkg/mod/github.com/swaggo/swag@v1.8.5/cmd/swag/main.go:221

You can use v1.8.4 in your go.mod file

@FabianMartin
Copy link
Contributor

try the master branch of swaggo. You can install it with the following command.

go install github.com/swaggo/swag/cmd/swag@master

@ghoshabhi
Copy link

ghoshabhi commented Sep 15, 2022

I am still getting the same error on latest master @FabianMartin

2022/09/15 09:16:34 ParseComment error in file /Users/username/workspace/my-project/pkg/handlers/completed_form.go :cannot find type definition: PaginatedResponse[MyTemplateType]

type Pagination struct {
	Index int `json:"index"`
	Size  int `json:"size"`
	Total int `json:"total"`
}
type PaginatedResponse[T any] struct {
	Data       []T        `json:"data"`
	Pagination Pagination `json:"pagination"`
}

type MyTemplateType struct {
	ID int `json:"id"`
        Name string `json:"name"`
}

Swag annotations:

// @Summary ....
// @Description ...
// @Tags ...
// @Security ...
// @Success 200 {object} PaginatedResponse[MyTemplateType]
// @Failure 403
// @Failure 500
// @Router ... [get]

Swaggo version:

My project has: github.com/swaggo/swag v1.8.6-0.20220915145914-b8f53dafbbf8
But for some reason when i install i get v1.8.5.
Could this be why ?

@FabianMartin
Copy link
Contributor

@ghoshabhi Can you provide a test repository?

@ghoshabhi
Copy link

i have an example now @FabianMartin https://github.com/ghoshabhi/go-swaggo-test

image

FabianMartin added a commit to FabianMartin/swag that referenced this issue Sep 15, 2022
- Generic detection moved to own method
- prepend pkg path if not provided

refs swaggo#1319
@FabianMartin
Copy link
Contributor

@ghoshabhi PR #1328 contains a fix for your issue.

Currently, the generics are only detected if the definition contains the pkg name
If you change the following line

// @Success 200 {object} PaginatedResponse[ResponseItem]

to

// @Success 200 {object} api.PaginatedResponse[api.ResponseItem]

it should find the definition.

After merging the PR, swaggo will also detect generics without pkg name.

@ghoshabhi
Copy link

thanks @FabianMartin ! let me try moving the definitions to a separate pkg. No rush, but do you have a rough eta on when v1.8.6 will be released ?

@ghoshabhi
Copy link

I got a different error after moving type definitions to a different pkg:
image

Updated the test repo if you want to test

@FabianMartin
Copy link
Contributor

The warning contains the solution to your issue. It cannot determine the package name.

Change your call to the following, and it should work.

swag init --parseDependency --parseInternal --dir ./cmd/api

@FabianMartin
Copy link
Contributor

FabianMartin commented Sep 15, 2022

thanks @FabianMartin ! let me try moving the definitions to a separate pkg. No rush, but do you have a rough eta on when v1.8.6 will be released ?

@ubogdan releases new versions and in #1310 (comment) he wrote that a new version will be released soon.

@ghoshabhi
Copy link

```shell
swag init --parseDependency --parseInternal --dir ./cmd/api

aha! yeah that worked. Weird, according to docs -g should've had the same effect. But glad latest version is working, can't wait for the release now! Thank you so much for working on this

ubogdan pushed a commit that referenced this issue Sep 16, 2022
- Generic detection moved to own method
- prepend pkg path if not provided

refs #1319
@hoppfrosch
Copy link

hoppfrosch commented Oct 21, 2022

Same here ... happens with (swagg@v1.8.5):
`
import (
"encoding/xml"
)

type MyTest struct {
XMLName xml.Name xml:"xmltest"
....
)
`

Running *swag init .... * (without --parseDependency) leads to following error: ".... :cannot find type definition: xml.Name"

Whereas swag init --parseDependency .... leads to the described error *github.com/swaggo/swag.(PackagesDefinitions).parseFunctionScopedTypesFromFile(0x1400000e858, 0x14001732880, {0x1400037d8f0, 0x15}, 0x140026b3ad0)

Switching back to swag@v1.8.4 does not show the --parseDependency error.

@FabianMartin
Copy link
Contributor

1.8.6 contains a fix for this. Current Version is 1.8.7. Please update and test again.

@hoppfrosch
Copy link

hoppfrosch commented Oct 21, 2022

Thx for fast response - updated to swag@v1.8.7

*swag init --parseDependency .... works correctly

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

9 participants