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/build: imports from cgo files included in Imports when CGO_ENABLED=0 #35946

Closed
jayconrod opened this issue Dec 3, 2019 · 1 comment
Closed
Labels
FrozenDueToAge NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@jayconrod
Copy link
Contributor

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

$ go version
go version devel +2ac1ca9160 Tue Dec 3 15:26:50 2019 +0000 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"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/jayconrod/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/Users/jayconrod/Code/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/Users/jayconrod/Code/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/jayconrod/Code/go/src/cmd/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-build021162515=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

go run main.go
! stdout '^fmt$'

-- main.go --
package main

import (
	"fmt"
	"go/build"
	"log"
	"os"
	"path/filepath"
	"strings"
)

func main() {
	ctxt := new(build.Context)
	*ctxt = build.Default
	ctxt.CgoEnabled = false

	wd, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
	aDir := filepath.Join(wd, "a")
	p, err := ctxt.ImportDir(aDir, 0)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(strings.Join(p.Imports, "\n"))
}

-- a/cgo_enabled.go --
package a

import "C"

import _ "fmt"

-- a/empty.go --
package a

What did you expect to see?

The test script fails.

fmt should not appear in the output. It's only imported by cgo_enabled.go, which contains import "C". This file is ignored because ctxt.CgoEnabled is false. Its imports should be ignored, too.

Note that if we add // +build cgo to cgo_enabled.go, the imports from that file are not included in the output.

What did you see instead?

The test script should pass.

This can be tested more concisely with go list -f {{.Imports}} ./a. The root cause of the issue is in go/build though.

@jayconrod jayconrod added this to the Go1.14 milestone Dec 3, 2019
@jayconrod jayconrod added the NeedsFix The path to resolution is known, but the work has not been done. label Dec 3, 2019
@jayconrod jayconrod self-assigned this Dec 3, 2019
@gopherbot
Copy link

Change https://golang.org/cl/209660 mentions this issue: go/build: don't include imports from cgo files when CGO_ENABLED=0

@golang golang locked and limited conversation to collaborators Dec 3, 2020
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.
Projects
None yet
Development

No branches or pull requests

2 participants