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

swag init treats CGo warnings as fatal #1249

Open
MrDOS opened this issue Jul 5, 2022 · 1 comment
Open

swag init treats CGo warnings as fatal #1249

MrDOS opened this issue Jul 5, 2022 · 1 comment

Comments

@MrDOS
Copy link
Contributor

MrDOS commented Jul 5, 2022

Describe the bug
When C source code compiled by CGo produces warnings, swag init treats these errors as fatal, and doc generation fails.

To Reproduce
go.mod:

module swag-1232

go 1.18

main.go:

package main

// #pragma GCC warning "test"
import "C"

func main() {}

Then generate Swagger documentations with dependency parsing:

$ swag init --parseDependency
2022/07/05 18:04:08 Generate swagger docs....
2022/07/05 18:04:08 Generate general API Info, search dir:./
2022/07/05 18:04:08 pkg /.../swag-1232 cannot find all dependencies, <nil>
# swag-1232
./main.go:3:14: warning: test [-W#pragma-messages]

Expected behavior
I would expect the Swagger documentation to be generated successfully, instead of failing due to the C compilation warning.

Your swag version

$ swag --version
swag version v1.8.3

This does not occur in swag v1.8.2 or earlier.

Your go version

$ go version
go version go1.18.3 darwin/arm64

Desktop (please complete the following information):

  • OS: macOS 12.4
  • Browser: n/a
  • Version: n/a

Additional context
As of #1108, released in swag v1.8.3, swag init invokes go list under the hood. When CGo is enabled, that command will invoke the C compiler to evaluate C source; the C compiler, in turn, sends compilation errors and warnings to stderr. However, swag init treats any output to stderr by that command as fatal:

swag/golist.go

Lines 25 to 27 in bd21bb0

if stderrBuf.Len() > 0 {
finalErr = fmt.Errorf("%v\n%s", finalErr, stderrBuf.Bytes())
}

I think the most straightforward solution here is to disable CGo for the invocation of go list by setting the CGO_ENABLED environment variable to 0. I don't think this would break any existing API documentation, because swag init doesn't seem to understand the C pseudo-package anyway:

// @Success 200 {object} C.int
// @Router /some-route [get]
func SomeHandler() {}
$ swag init
2022/07/05 18:21:58 Generate swagger docs....
2022/07/05 18:21:58 Generate general API Info, search dir:./
2022/07/05 18:21:58 ParseComment error in file /.../main.go :cannot find type definition: C.int

This extends to the inclusion of CGo types embedded within Go structs.

I think this issue is separate from #1232. In that issue, go list failed to find any source files; here, go list runs successfully, and produces the expected dependency descriptions, but also includes extraneous output.

@MrDOS
Copy link
Contributor Author

MrDOS commented Jul 5, 2022

Another possible solution would be to remove the stderr checking, and rely on the error checking of cmd.Wait() to catch a non-zero exit status from the go list invocation. Conventionally, output to stderr shouldn't be considered fatal by itself, just informative, unless accompanied by a non-zero exit status.

I'm happy to implement either solution. Just let me know which direction you'd like me to go.

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

1 participant