Skip to content

cmd/go/internal/imports: recognize "unix" build tag #54716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cmd/dist/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,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,
Expand Down
42 changes: 33 additions & 9 deletions src/cmd/go/internal/imports/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package imports

import (
"bytes"
"cmd/go/internal/cfg"
"errors"
"fmt"
"go/build/constraint"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
32 changes: 32 additions & 0 deletions src/cmd/go/testdata/script/import_unix_tag.txt
Original file line number Diff line number Diff line change
@@ -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() {}
3 changes: 2 additions & 1 deletion src/go/build/syslist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down