Skip to content

Commit 3c6a5cd

Browse files
ZekeLuBryan Mills
authored and
Bryan Mills
committed
cmd/go/internal/imports: recognize "unix" build tag
For #20322 For #51572 Fixes #54712 Change-Id: I22fcfa820e83323bfdf1a40deee7286240f02b3e GitHub-Last-Rev: cd2c653 GitHub-Pull-Request: #54716 Reviewed-on: https://go-review.googlesource.com/c/go/+/426296 Run-TryBot: Heschi Kreinick <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent f21514c commit 3c6a5cd

File tree

4 files changed

+69
-11
lines changed

4 files changed

+69
-11
lines changed

src/cmd/dist/build.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,8 @@ func packagefile(pkg string) string {
939939
}
940940

941941
// unixOS is the set of GOOS values matched by the "unix" build tag.
942-
// This is the same list as in go/build/syslist.go.
942+
// This is the same list as in go/build/syslist.go and
943+
// cmd/go/internal/imports/build.go.
943944
var unixOS = map[string]bool{
944945
"aix": true,
945946
"android": true,

src/cmd/go/internal/imports/build.go

+33-9
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package imports
2020

2121
import (
2222
"bytes"
23+
"cmd/go/internal/cfg"
2324
"errors"
2425
"fmt"
2526
"go/build/constraint"
@@ -201,17 +202,22 @@ func matchTag(name string, tags map[string]bool, prefer bool) bool {
201202
return prefer
202203
}
203204

204-
have := tags[name]
205-
if name == "linux" {
206-
have = have || tags["android"]
207-
}
208-
if name == "solaris" {
209-
have = have || tags["illumos"]
205+
if tags[name] {
206+
return true
210207
}
211-
if name == "darwin" {
212-
have = have || tags["ios"]
208+
209+
switch name {
210+
case "linux":
211+
return tags["android"]
212+
case "solaris":
213+
return tags["illumos"]
214+
case "darwin":
215+
return tags["ios"]
216+
case "unix":
217+
return unixOS[cfg.BuildContext.GOOS]
218+
default:
219+
return false
213220
}
214-
return have
215221
}
216222

217223
// eval is like
@@ -322,6 +328,24 @@ var KnownOS = map[string]bool{
322328
"zos": true,
323329
}
324330

331+
// unixOS is the set of GOOS values matched by the "unix" build tag.
332+
// This is not used for filename matching.
333+
// This is the same list as in go/build/syslist.go and cmd/dist/build.go.
334+
var unixOS = map[string]bool{
335+
"aix": true,
336+
"android": true,
337+
"darwin": true,
338+
"dragonfly": true,
339+
"freebsd": true,
340+
"hurd": true,
341+
"illumos": true,
342+
"ios": true,
343+
"linux": true,
344+
"netbsd": true,
345+
"openbsd": true,
346+
"solaris": true,
347+
}
348+
325349
var KnownArch = map[string]bool{
326350
"386": true,
327351
"amd64": true,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Regression test for https://go.dev/issue/54712: the "unix" build constraint
2+
# was not applied consistently during package loading.
3+
4+
go list -x -f '{{if .Module}}{{.ImportPath}}{{end}}' -deps .
5+
stdout 'example.com/version'
6+
7+
-- go.mod --
8+
module example
9+
10+
go 1.19
11+
12+
require example.com/version v1.1.0
13+
-- go.sum --
14+
example.com/version v1.1.0 h1:VdPnGmIF1NJrntStkxGrF3L/OfhaL567VzCjncGUgtM=
15+
example.com/version v1.1.0/go.mod h1:S7K9BnT4o5wT4PCczXPfWVzpjD4ud4e7AJMQJEgiu2Q=
16+
-- main_notunix.go --
17+
//go:build !(aix || darwin || dragonfly || freebsd || hurd || linux || netbsd || openbsd || solaris)
18+
19+
package main
20+
21+
import _ "example.com/version"
22+
23+
func main() {}
24+
25+
-- main_unix.go --
26+
//go:build unix
27+
28+
package main
29+
30+
import _ "example.com/version"
31+
32+
func main() {}

src/go/build/syslist.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ var knownOS = map[string]bool{
3333

3434
// unixOS is the set of GOOS values matched by the "unix" build tag.
3535
// This is not used for filename matching.
36-
// This list also appears in cmd/dist/build.go.
36+
// This list also appears in cmd/dist/build.go and
37+
// cmd/go/internal/imports/build.go.
3738
var unixOS = map[string]bool{
3839
"aix": true,
3940
"android": true,

0 commit comments

Comments
 (0)