]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: ignore import "C" files in module loader in non-cgo mode
authorRuss Cox <rsc@golang.org>
Sat, 11 Aug 2018 00:05:44 +0000 (20:05 -0400)
committerRuss Cox <rsc@golang.org>
Fri, 17 Aug 2018 14:41:26 +0000 (14:41 +0000)
Obviously, including files that import "C" when cgo is disabled is wrong.
The package load step correctly excludes them and finds no files at all,
which then causes a failure.

Fixes #26927.

Change-Id: I00e6d6450e783d467d20bde99e91240ecb0db837
Reviewed-on: https://go-review.googlesource.com/129062
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Reviewed-by: David du Colombier <0intro@gmail.com>
src/cmd/go/internal/imports/scan.go
src/cmd/go/testdata/script/mod_patterns.txt

index 095bb64a8dd4b7cd13af4e062b60721d0c05f04c..bae6b934bcad2daf57113918d61e6746a8997e7d 100644 (file)
@@ -37,6 +37,7 @@ func scanFiles(files []string, tags map[string]bool, explicitFiles bool) ([]stri
        imports := make(map[string]bool)
        testImports := make(map[string]bool)
        numFiles := 0
+Files:
        for _, name := range files {
                r, err := os.Open(name)
                if err != nil {
@@ -48,6 +49,12 @@ func scanFiles(files []string, tags map[string]bool, explicitFiles bool) ([]stri
                if err != nil {
                        return nil, nil, fmt.Errorf("reading %s: %v", name, err)
                }
+               // import "C" is implicit requirement of cgo tag
+               for _, path := range list {
+                       if path == `"C"` && !tags["cgo"] && !tags["*"] {
+                               continue Files
+                       }
+               }
                if !explicitFiles && !ShouldBuild(data, tags) {
                        continue
                }
index 36d738a867df1de76e2dd607a18dd49f7a062981..a43fe8248957d37f5f4c87df26c558a67083fcfd 100644 (file)
@@ -43,6 +43,10 @@ stdout example.com/m/useunsafe
 [cgo] stdout example.com/m/useC
 [!cgo] ! stdout example.com/m/useC
 
+env CGO_ENABLED=0
+go list -f '{{.ImportPath}}: {{.Match}}' all ... example.com/m/... ./... ./xyz...
+! stdout example.com/m/useC
+
 -- m/go.mod --
 module example.com/m