From: Ian Lance Taylor Date: Wed, 30 Mar 2022 23:36:14 +0000 (-0700) Subject: go/build: set allTags even when short-circuiting x_GOOS_GOARCH.go X-Git-Tag: go1.19beta1~838 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=9743fdd097171cada6231503974209edc5400ee8;p=gostls13.git go/build: set allTags even when short-circuiting x_GOOS_GOARCH.go Fixes #52053 Change-Id: I0e1f2737f97a4656913b34a731d8de2c77a15b4d Reviewed-on: https://go-review.googlesource.com/c/go/+/396918 Trust: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Bryan Mills Reviewed-by: Michael Matloob Auto-Submit: Ian Lance Taylor TryBot-Result: Gopher Robot --- diff --git a/src/go/build/build.go b/src/go/build/build.go index df505312ce..cc2585b677 100644 --- a/src/go/build/build.go +++ b/src/go/build/build.go @@ -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]]) { diff --git a/src/go/build/build_test.go b/src/go/build/build_test.go index 6cd7f9b589..36bcae179e 100644 --- a/src/go/build/build_test.go +++ b/src/go/build/build_test.go @@ -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 index 0000000000..5d308550d1 --- /dev/null +++ b/src/go/build/testdata/alltags/alltags.go @@ -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 index 0000000000..5d308550d1 --- /dev/null +++ b/src/go/build/testdata/alltags/x_netbsd_arm.go @@ -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