]> Cypherpunks repositories - gostls13.git/commitdiff
go/build: set allTags even when short-circuiting x_GOOS_GOARCH.go
authorIan Lance Taylor <iant@golang.org>
Wed, 30 Mar 2022 23:36:14 +0000 (16:36 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 31 Mar 2022 19:36:46 +0000 (19:36 +0000)
Fixes #52053

Change-Id: I0e1f2737f97a4656913b34a731d8de2c77a15b4d
Reviewed-on: https://go-review.googlesource.com/c/go/+/396918
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/go/build/build.go
src/go/build/build_test.go
src/go/build/testdata/alltags/alltags.go [new file with mode: 0644]
src/go/build/testdata/alltags/x_netbsd_arm.go [new file with mode: 0644]

index df505312ce32a66f353977858eb93e096f0709e4..cc2585b6773efcae7ddb69d3a2099cf224bd745c 100644 (file)
@@ -1974,6 +1974,10 @@ func (ctxt *Context) goodOSArchFile(name string, allTags map[string]bool) bool {
        }
        n := len(l)
        if n >= 2 && knownOS[l[n-2]] && knownArch[l[n-1]] {
+               if allTags != nil {
+                       // In case we short-circuit on l[n-1].
+                       allTags[l[n-2]] = true
+               }
                return ctxt.matchTag(l[n-1], allTags) && ctxt.matchTag(l[n-2], allTags)
        }
        if n >= 1 && (knownOS[l[n-1]] || knownArch[l[n-1]]) {
index 6cd7f9b58903d4ef0101616f97b875483a3af338..36bcae179e3252f1f152e5191ae523992e6a166f 100644 (file)
@@ -731,3 +731,39 @@ func TestCgoImportsIgnored(t *testing.T) {
                }
        }
 }
+
+// Issue #52053. Check that if there is a file x_GOOS_GOARCH.go that both
+// GOOS and GOARCH show up in the Package.AllTags field. We test both the
+// case where the file matches and where the file does not match.
+// The latter case used to fail, incorrectly omitting GOOS.
+func TestAllTags(t *testing.T) {
+       ctxt := Default
+       ctxt.GOARCH = "arm"
+       ctxt.GOOS = "netbsd"
+       p, err := ctxt.ImportDir("testdata/alltags", 0)
+       if err != nil {
+               t.Fatal(err)
+       }
+       want := []string{"arm", "netbsd"}
+       if !reflect.DeepEqual(p.AllTags, want) {
+               t.Errorf("AllTags = %v, want %v", p.AllTags, want)
+       }
+       wantFiles := []string{"alltags.go", "x_netbsd_arm.go"}
+       if !reflect.DeepEqual(p.GoFiles, wantFiles) {
+               t.Errorf("GoFiles = %v, want %v", p.GoFiles, wantFiles)
+       }
+
+       ctxt.GOARCH = "amd64"
+       ctxt.GOOS = "linux"
+       p, err = ctxt.ImportDir("testdata/alltags", 0)
+       if err != nil {
+               t.Fatal(err)
+       }
+       if !reflect.DeepEqual(p.AllTags, want) {
+               t.Errorf("AllTags = %v, want %v", p.AllTags, want)
+       }
+       wantFiles = []string{"alltags.go"}
+       if !reflect.DeepEqual(p.GoFiles, wantFiles) {
+               t.Errorf("GoFiles = %v, want %v", p.GoFiles, wantFiles)
+       }
+}
diff --git a/src/go/build/testdata/alltags/alltags.go b/src/go/build/testdata/alltags/alltags.go
new file mode 100644 (file)
index 0000000..5d30855
--- /dev/null
@@ -0,0 +1,5 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package alltags
diff --git a/src/go/build/testdata/alltags/x_netbsd_arm.go b/src/go/build/testdata/alltags/x_netbsd_arm.go
new file mode 100644 (file)
index 0000000..5d30855
--- /dev/null
@@ -0,0 +1,5 @@
+// Copyright 2022 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package alltags