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

cmd/go: go list -e -compiled -json does not populate Error when cgo preprocessing fails #38990

Open
dominikh opened this issue May 11, 2020 · 4 comments
Labels
GoCommand NeedsFix
Milestone

Comments

@dominikh
Copy link
Member

@dominikh dominikh commented May 11, 2020

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

$ go version
go version go1.14.1 linux/amd64
go version devel +57e32c4fbd Sun May 10 14:43:46 2020 +0000 linux/amd64

What's wrong

When cgo preprocessing fails, go list -json -compiled -e prints an error on stderr and exits with a non-zero status, but it does not populate the Error field of the package. It is possible to detect that something went wrong because CompiledGoFiles isn't populated, but we can't tell why it failed. Two common reasons are a missing C compiler, and missing libraries.

Example output

$ CC=/does/not/exist go list -json -compiled -e runtime/cgo
# runtime/cgo
fork/exec /does/not/exist: no such file or directory
{
	"Dir": "/nix/store/sylhlk3fd3xik0d8hqrm20zxnfc3x7m4-go-1.14.1/share/go/src/runtime/cgo",
	"ImportPath": "runtime/cgo",
	"Name": "cgo",
	"Doc": "Package cgo contains runtime support for code generated by the cgo tool.",
	"Target": "/nix/store/sylhlk3fd3xik0d8hqrm20zxnfc3x7m4-go-1.14.1/share/go/pkg/linux_amd64/runtime/cgo.a",
	"Root": "/nix/store/sylhlk3fd3xik0d8hqrm20zxnfc3x7m4-go-1.14.1/share/go",
	"Match": [
		"runtime/cgo"
	],
	"Goroot": true,
	"Standard": true,
	"Stale": true,
	"StaleReason": "build ID mismatch",
	"GoFiles": [
		"callbacks.go",
		"callbacks_traceback.go",
		"iscgo.go",
		"mmap.go",
		"setenv.go",
		"sigaction.go"
	],
	"CgoFiles": [
		"cgo.go"
	],
	"IgnoredGoFiles": [
		"callbacks_aix.go",
		"dragonfly.go",
		"freebsd.go",
		"netbsd.go",
		"openbsd.go",
		"signal_darwin_armx.go"
	],
	"CFiles": [
		"gcc_context.c",
		"gcc_fatalf.c",
		"gcc_libinit.c",
		"gcc_linux_amd64.c",
		"gcc_mmap.c",
		"gcc_setenv.c",
		"gcc_sigaction.c",
		"gcc_traceback.c",
		"gcc_util.c"
	],
	"HFiles": [
		"libcgo.h",
		"libcgo_unix.h"
	],
	"SFiles": [
		"asm_amd64.s",
		"gcc_amd64.S"
	],
	"CgoCFLAGS": [
		"-Wall",
		"-Werror"
	],
	"CgoLDFLAGS": [
		"-lpthread"
	],
	"Imports": [
		"C",
		"unsafe"
	],
	"Deps": [
		"unsafe"
	]
}

Related: #34229 and #25842

/cc @bcmills @matloob @heschik

@dominikh dominikh added NeedsInvestigation GoCommand labels May 11, 2020
@jayconrod
Copy link
Contributor

@jayconrod jayconrod commented May 11, 2020

This has the same root cause as the two linked issues: #34229 and #25842

Currently, go list -e populates the Error field with errors it encounters when loading the package dependency graph. It should also populate the Error field with errors encountered when running cgo, the compiler, and other commands.

@jayconrod jayconrod added NeedsFix and removed NeedsInvestigation labels May 11, 2020
@jayconrod jayconrod added this to the Backlog milestone May 11, 2020
@SeanTolstoyevski
Copy link

@SeanTolstoyevski SeanTolstoyevski commented Nov 15, 2021

This issue also occurs when coding a CGO module with VSCode.

now is there anything that can be making?

@thalesfsp
Copy link

@thalesfsp thalesfsp commented Jan 24, 2022

This issue also occurs when coding a CGO module with VSCode.

now is there anything that can be making?

Do u know how to disable this message on VSCode?

@zyfdegh
Copy link

@zyfdegh zyfdegh commented May 7, 2022

There's no quick solution currently, but to figure out what's wrong VSCode running the go list command, execute this manually @SeanTolstoyevski @thalesfsp

go list -e -json -compiled -x  /absolute/path/to/yourpkg/src.go

For me, it outputs

WORK=/tmp/go-build2892322839
mkdir -p $WORK/b001/
cd /absolute/path/to/yourpkg
TERM='dumb' CGO_LDFLAGS='"-g" "-O2" "-L/absolute/path/to/yourpkg/lib" "-lYourFunc"' /usr/lib/go/pkg/tool/linux_amd64/cgo -objdir $WORK/b001/ -importpath command-line-arguments -- -I $WORK/b001/ -g -O2 -I/absolute/path/to/yourpkg/include -std=c99 ./src.go
# command-line-arguments
In file included from yourpkg/src.go:10:
yourpkg/include/SomeWapper.h:1:10: fatal error: iostream: No such file or directory
    1 | #include <iostream>
      |          ^~~~~~~~~~
compilation terminated.
{
        "Dir": "/absolute/path/to/yourpkg",
        "ImportPath": "command-line-arguments",
        "Name": "yourpkg",
        "Match": [
                "/absolute/path/to/yourpkg/src.go"
        ],
        "Stale": true,
        "StaleReason": "build ID mismatch",
        "CgoFiles": [
                "src.go"
        ],

# ... more JSON, but no 'CompiledGoFiles' field, VSCode cannot interpret

So I know it's due to the C++ headers included, which is not allowed. Refer golang-nuts

To dismiss the warnings, ignore files (refer) in vscode settings.json (not recommended, file tree won't show)

    "files.exclude": {
        "**/vscode/relative/yourpkg": true,
    },

On the PROBLEMS tab, click filter icon to the right, Hide Excluded Files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand NeedsFix
Projects
None yet
Development

No branches or pull requests

5 participants