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

x/tools/gopls: check CGO_ENABLED when symbol is undefined because of implicit build constraint #70863

Open
xieyuschen opened this issue Dec 16, 2024 · 2 comments
Labels
gopls Issues related to the Go language server, gopls.

Comments

@xieyuschen
Copy link
Contributor

Proposal Details

CGO docs says the import "C" implies the "cgo" build constraint:

The go tool will set the build constraint "cgo" if cgo is enabled. The special import "C" implies the "cgo" build constraint, as though the file also said "//go:build cgo".

Hence, when the code is arranged as this, the go tool reports ./main.go:8:6: undefined: sub.API for the following project in txtar format when I run CGO_ENABLED=0 go build. I want to propose this because when a 3rd library uses import "C" in a go file with several package level functions and my local CGO_ENABLED=0, the build fails without any useful error hint.

The gopls emits the same error hint when the CGO_ENABLED=0.

image

I proposed the gopls to check the files with import "C" additionally after gopls found an undefined error from go tool when CGO is disabled. It should give an hint that "undefined: sub.API. CGO is disabled so symbols inside file sub.go are excluded" if we can find the symbol API inside file sub.go.

This may apply to the undefined symbols which exist in the certain build contraints under the same package. But I would like to narrow the proposal down to CGO only because CGO build constraint is implicit convention but the others are explicit. Moreover, the import "C" affects the other functions inside the same file as build contraint is file level.

-- go.mod --
module github.com/xieyuschen/example

go 1.21
-- main.go --
package main

import (
	"github.com/xieyuschen/example/sub"
)

func main() {
	sub.API()
}
-- sub/sub.go --
package sub

// int fortytwo()
// {
//	    return 42;
// }
import "C"

func CAPI() {
	C.fortytwo()
}

func API() {
	C.fortytwo()
}
-- sub/sub2.go --
package sub
// this file is used to make a go file available for package sub
@gopherbot gopherbot added this to the Proposal milestone Dec 16, 2024
@seankhliao seankhliao changed the title proposal: x/tools/gopls/: check CGO_ENABLED when symbol is undefined because of implicit build constraint proposal: x/tools/gopls: check CGO_ENABLED when symbol is undefined because of implicit build constraint Dec 16, 2024
@ianlancetaylor
Copy link
Member

I don't see a reason for this to be a proposal. Taking it out of the proposal process.

@ianlancetaylor ianlancetaylor changed the title proposal: x/tools/gopls: check CGO_ENABLED when symbol is undefined because of implicit build constraint x/tools/gopls: check CGO_ENABLED when symbol is undefined because of implicit build constraint Dec 18, 2024
@ianlancetaylor ianlancetaylor removed this from the Proposal milestone Dec 18, 2024
@ianlancetaylor ianlancetaylor added gopls Issues related to the Go language server, gopls. and removed Proposal labels Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls Issues related to the Go language server, gopls.
Projects
None yet
Development

No branches or pull requests

4 participants