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/go/packages: could not import C (no metadata for C) #36441

Closed
jayconrod opened this issue Jan 7, 2020 · 3 comments
Closed

x/tools/go/packages: could not import C (no metadata for C) #36441

jayconrod opened this issue Jan 7, 2020 · 3 comments
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@jayconrod
Copy link
Contributor

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

$ go version
go version go1.13.5 darwin/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
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/jayconrod/Library/Caches/go-build"
GOENV="/Users/jayconrod/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/jayconrod/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/opt/go/installed"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/opt/go/installed/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/jayconrod/Code/test/go.mod"
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=/var/folders/rq/x0692kqj6ml8cvrhcqh5bswc008xj1/T/go-build071831140=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I tried to load a cgo package with golang.org/x/tools/go/packages.Load using the mode NeedName | NeedTypes | NeedImports | NeedDeps. See the testscript below.

go run load.go

-- go.mod --
module example.com/c

go 1.13

require golang.org/x/tools v0.0.0-20200107184032-11e9d9cc0042 // indirect
-- c.go --
package c

import "C"
-- load.go --
// +build ignore

package main

import (
	"fmt"
	"log"

	"golang.org/x/tools/go/packages"
)

func main() {
	cfg := &packages.Config{
		Mode: packages.NeedName | packages.NeedTypes | packages.NeedImports | packages.NeedDeps,
	}
	pkgs, err := packages.Load(cfg, "example.com/c")
	if err != nil {
		log.Fatal(err)
	}
	for _, pkg := range pkgs {
		fmt.Printf("package %s\n", pkg.PkgPath)
		for _, err := range pkg.Errors {
			fmt.Printf("\t%s\n", err)
		}
	}
}

What did you expect to see?

package example.com/c

(no errors reported)

What did you see instead?

package example.com/c
	/Users/jayconrod/Code/test/c.go:3:8: could not import C (no metadata for C)

This works when I add packages.NeedSyntax to the mode. For the tool I'm writing, I just need the types of exported definitions. I don't need to access the AST.

cc @matloob

@jayconrod jayconrod added NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository. labels Jan 7, 2020
@jayconrod jayconrod added this to the Unreleased milestone Jan 7, 2020
@hajimehoshi
Copy link
Member

hajimehoshi commented Jan 14, 2020

I found the same error even with packages.NeedSyntax.

EDIT: nvm, this was my mistake. Actually NeedSyntax suppressed the error.

@gopherbot
Copy link

Change https://golang.org/cl/214738 mentions this issue: go/packages: ignore importing C

@gopherbot
Copy link

Change https://golang.org/cl/214943 mentions this issue: go/packges: fix incorrect needtypes and needsrcs logic

@golang golang locked and limited conversation to collaborators Jan 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

3 participants