aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZeke Lu <lvzecai@gmail.com>2022-08-29 23:29:48 +0000
committerHeschi Kreinick <heschi@google.com>2022-08-30 20:08:38 +0000
commit4580d6dc6d8c7c06de9a5c07d4232a63c3a06da1 (patch)
tree302339bcf04457ccccf10fc4445da56095b7e2b7
parent62aa93010dd2e49ed1b98d54c088a875558fed56 (diff)
downloadgo-4580d6dc6d8c7c06de9a5c07d4232a63c3a06da1.tar.gz
go-4580d6dc6d8c7c06de9a5c07d4232a63c3a06da1.zip
[release-branch.go1.19] cmd/go/internal/imports: recognize "unix" build tag
For #20322 For #51572 Updates #54712 Fixes #54736 Change-Id: I22fcfa820e83323bfdf1a40deee7286240f02b3e GitHub-Last-Rev: cd2c6536b0298baf3c54e5bdbc456a814545cff4 GitHub-Pull-Request: golang/go#54716 Reviewed-on: https://go-review.googlesource.com/c/go/+/426296 Run-TryBot: Heschi Kreinick <heschi@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Heschi Kreinick <heschi@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> (cherry picked from commit 3c6a5cdb9a29c0e3b12cdaa8ab81ba22d989e3b0) Reviewed-on: https://go-review.googlesource.com/c/go/+/426814 Run-TryBot: Bryan Mills <bcmills@google.com>
-rw-r--r--src/cmd/dist/build.go3
-rw-r--r--src/cmd/go/internal/imports/build.go42
-rw-r--r--src/cmd/go/testdata/script/import_unix_tag.txt32
-rw-r--r--src/go/build/syslist.go3
4 files changed, 69 insertions, 11 deletions
diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go
index 7c44c4a605..14c6db7104 100644
--- a/src/cmd/dist/build.go
+++ b/src/cmd/dist/build.go
@@ -938,7 +938,8 @@ func packagefile(pkg string) string {
}
// unixOS is the set of GOOS values matched by the "unix" build tag.
-// This is the same list as in go/build/syslist.go.
+// This is the same list as in go/build/syslist.go and
+// cmd/go/internal/imports/build.go.
var unixOS = map[string]bool{
"aix": true,
"android": true,
diff --git a/src/cmd/go/internal/imports/build.go b/src/cmd/go/internal/imports/build.go
index 957113686c..bbe08da6b3 100644
--- a/src/cmd/go/internal/imports/build.go
+++ b/src/cmd/go/internal/imports/build.go
@@ -20,6 +20,7 @@ package imports
import (
"bytes"
+ "cmd/go/internal/cfg"
"errors"
"fmt"
"go/build/constraint"
@@ -201,17 +202,22 @@ func matchTag(name string, tags map[string]bool, prefer bool) bool {
return prefer
}
- have := tags[name]
- if name == "linux" {
- have = have || tags["android"]
- }
- if name == "solaris" {
- have = have || tags["illumos"]
+ if tags[name] {
+ return true
}
- if name == "darwin" {
- have = have || tags["ios"]
+
+ switch name {
+ case "linux":
+ return tags["android"]
+ case "solaris":
+ return tags["illumos"]
+ case "darwin":
+ return tags["ios"]
+ case "unix":
+ return unixOS[cfg.BuildContext.GOOS]
+ default:
+ return false
}
- return have
}
// eval is like
@@ -322,6 +328,24 @@ var KnownOS = map[string]bool{
"zos": true,
}
+// unixOS is the set of GOOS values matched by the "unix" build tag.
+// This is not used for filename matching.
+// This is the same list as in go/build/syslist.go and cmd/dist/build.go.
+var unixOS = map[string]bool{
+ "aix": true,
+ "android": true,
+ "darwin": true,
+ "dragonfly": true,
+ "freebsd": true,
+ "hurd": true,
+ "illumos": true,
+ "ios": true,
+ "linux": true,
+ "netbsd": true,
+ "openbsd": true,
+ "solaris": true,
+}
+
var KnownArch = map[string]bool{
"386": true,
"amd64": true,
diff --git a/src/cmd/go/testdata/script/import_unix_tag.txt b/src/cmd/go/testdata/script/import_unix_tag.txt
new file mode 100644
index 0000000000..b88ca1e2ee
--- /dev/null
+++ b/src/cmd/go/testdata/script/import_unix_tag.txt
@@ -0,0 +1,32 @@
+# Regression test for https://go.dev/issue/54712: the "unix" build constraint
+# was not applied consistently during package loading.
+
+go list -x -f '{{if .Module}}{{.ImportPath}}{{end}}' -deps .
+stdout 'example.com/version'
+
+-- go.mod --
+module example
+
+go 1.19
+
+require example.com/version v1.1.0
+-- go.sum --
+example.com/version v1.1.0 h1:VdPnGmIF1NJrntStkxGrF3L/OfhaL567VzCjncGUgtM=
+example.com/version v1.1.0/go.mod h1:S7K9BnT4o5wT4PCczXPfWVzpjD4ud4e7AJMQJEgiu2Q=
+-- main_notunix.go --
+//go:build !(aix || darwin || dragonfly || freebsd || hurd || linux || netbsd || openbsd || solaris)
+
+package main
+
+import _ "example.com/version"
+
+func main() {}
+
+-- main_unix.go --
+//go:build unix
+
+package main
+
+import _ "example.com/version"
+
+func main() {}
diff --git a/src/go/build/syslist.go b/src/go/build/syslist.go
index 35cffce6dc..78ca565ce2 100644
--- a/src/go/build/syslist.go
+++ b/src/go/build/syslist.go
@@ -33,7 +33,8 @@ var knownOS = map[string]bool{
// unixOS is the set of GOOS values matched by the "unix" build tag.
// This is not used for filename matching.
-// This list also appears in cmd/dist/build.go.
+// This list also appears in cmd/dist/build.go and
+// cmd/go/internal/imports/build.go.
var unixOS = map[string]bool{
"aix": true,
"android": true,