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

go/packages: ignore cgo errors #33462

Closed
bwmarrin opened this issue Aug 5, 2019 · 13 comments

Comments

@bwmarrin
Copy link

commented Aug 5, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12.7 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/bwmarrin/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/bwmarrin/.go"
GOPROXY=""
GORACE=""
GOROOT="/home/bwmarrin/.go/root"
GOTMPDIR=""
GOTOOLDIR="/home/bwmarrin/.go/root/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build824270571=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Opened a new temp package in my GOPATH src folder. Tested auto completion and it worked fine. Then I added an import for the http package. Then I tested and auto completion no longer worked for items within my temp package.

After hunting down some other issues, I found the below command.

go list -e -json -compiled

Running that without the http package returned details without an error. Running it with the http package returned an error, along with the details I expected.

exec: "gcc": executable file not found in $PATH

Which, I assume is to say gcc is required for this to work right. So I installed gcc, and now everything works as expected.

I'm not sure if this is a bug, or working as intended. It took me several hours to figure out why my auto completion wasn't working and thought I'd share it here in case it wasn't intended behaviour and so maybe others might be helped, possibly.

What did you expect to see?

I expected go auto completion to "just work" even after adding the http package without needing to install a compiler I didn't think I needed.

What did you see instead?

Auto completion failed to work for things within the test package after adding http import.

@gopherbot gopherbot added the gopls label Aug 5, 2019

@odeke-em odeke-em changed the title go list error on http package causing gopls autocomplete to not work when gcc is not installed. x/tools/gopls: go list error on http package cause autocomplete to not work when gcc is not installed Aug 5, 2019

@gopherbot gopherbot added this to the Unreleased milestone Aug 5, 2019

@stamblerre

This comment has been minimized.

Copy link
Contributor

commented Aug 5, 2019

To confirm, this was when you were importing the net/http package in the standard library?

@bwmarrin

This comment has been minimized.

Copy link
Author

commented Aug 5, 2019

Correct.

@stamblerre

This comment has been minimized.

Copy link
Contributor

commented Aug 5, 2019

/cc @jayconrod for more clarity on why go list would behave this way. This sound similar to a few other issues users have encountered with gopls, specifically #33107 (comment) and #33018 (comment).

@bwmarrin: And this code compiles without any problems?

@jayconrod

This comment has been minimized.

Copy link
Contributor

commented Aug 5, 2019

net/http depends on packages that have cgo code. It looks like these are runtime/cgo, net, and crypto/x509.

If the -compiled flag is passed to go list, listed packages and their dependencies are compiled, and the paths to the compiled .a files are included in the output. If cgo is enabled, a C compiler is required to build those packages. I'm guessing gopls uses the compiled .a files for type information, but even if it didn't, cgo may be required for type checking.

@bwmarrin Have you tried disabling cgo by setting CGO_ENABLED=0 in your environment? I don't believe cgo is disabled automatically depending on whether there is a C compiler in PATH.

@stamblerre stamblerre changed the title x/tools/gopls: go list error on http package cause autocomplete to not work when gcc is not installed x/tools/gopls: C compiler needed for go/packages to work on dependencies Aug 5, 2019

@bwmarrin

This comment has been minimized.

Copy link
Author

commented Aug 5, 2019

@stamblerre - yes, the test code compiled fine. I could create a repo with an example, if that helped.

@jayconrod - No, I didn't try disabling cgo. Once I found the error I just went ahead and installed gcc. I just hadn't needed it on this dev machine yet so never installed it but it wasn't a big deal to add it if that's what it took to get things working so I could get back to work ;)

@stamblerre

This comment has been minimized.

Copy link
Contributor

commented Aug 13, 2019

I don't believe cgo is disabled automatically depending on whether there is a C compiler in PATH.

@jayconrod: Is this something we could possibly do in the go command, or would go/packages have to handle this error?

@gopherbot

This comment has been minimized.

Copy link

commented Aug 13, 2019

Change https://golang.org/cl/190177 mentions this issue: go/packages: add test case for golang/go#33462

@stamblerre

This comment has been minimized.

Copy link
Contributor

commented Aug 13, 2019

I think this is fairly fixable in go/packages. Sent @matloob a test case so that this issue can be resolved there.

@stamblerre stamblerre changed the title x/tools/gopls: C compiler needed for go/packages to work on dependencies go/packages: ignore cgo errors Aug 14, 2019

@hyangah

This comment has been minimized.

Copy link
Contributor

commented Aug 14, 2019

golang.org/x/mobile/cmd/gobind experienced the same issue and works around by disabling cgo explicitly with CGO_ENABLED=0.

https://go-review.googlesource.com/c/mobile/+/189597/2/cmd/gobind/main.go#62
cc @hajimehoshi

I am curious what's the consequence of disabling cgo - will it exclude the files that have 'import "C"' or 'cgo' build constraints in the code analysis completely?

@jayconrod

This comment has been minimized.

Copy link
Contributor

commented Aug 14, 2019

will it exclude the files that have 'import "C"' or 'cgo' build constraints in the code analysis completely?

Yes, exactly. Packages that only have cgo sources may not build at all.

gopherbot pushed a commit to golang/tools that referenced this issue Aug 14, 2019

go/packages: add test case for golang/go#33462
Updates golang/go#33462

Change-Id: I66e0ff1a87eab1958eed87a30e0e09b47a62d43c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/190177
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
@gopherbot

This comment has been minimized.

Copy link

commented Aug 14, 2019

Change https://golang.org/cl/190339 mentions this issue: go/packages: work around another case where go list doesn't respect -e

@bwmarrin

This comment has been minimized.

Copy link
Author

commented Aug 15, 2019

Thanks for all the work, everyone :)

@gopherbot

This comment has been minimized.

Copy link

commented Aug 16, 2019

Change https://golang.org/cl/190477 mentions this issue: cmd/gobind: remove the workaround for go/packages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.