update third_party go/build to go1.6

pull/6/head
Tim Hockin 2016-04-24 21:19:55 -07:00
parent 9a83015e60
commit 492aad6de3
4 changed files with 281 additions and 93 deletions

View File

@ -8,6 +8,10 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"go/ast"
"go/doc"
"go/parser"
"go/token"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -20,11 +24,6 @@ import (
"strings" "strings"
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"k8s.io/kubernetes/third_party/golang/go/ast"
"k8s.io/kubernetes/third_party/golang/go/doc"
"k8s.io/kubernetes/third_party/golang/go/parser"
"k8s.io/kubernetes/third_party/golang/go/token"
) )
// A Context specifies the supporting context for a build. // A Context specifies the supporting context for a build.
@ -111,7 +110,7 @@ func (ctxt *Context) splitPathList(s string) []string {
return filepath.SplitList(s) return filepath.SplitList(s)
} }
// isAbsPath calls ctxt.IsAbsSPath (if not nil) or else filepath.IsAbs. // isAbsPath calls ctxt.IsAbsPath (if not nil) or else filepath.IsAbs.
func (ctxt *Context) isAbsPath(path string) bool { func (ctxt *Context) isAbsPath(path string) bool {
if f := ctxt.IsAbsPath; f != nil { if f := ctxt.IsAbsPath; f != nil {
return f(path) return f(path)
@ -290,7 +289,7 @@ func defaultContext() Context {
c.GOARCH = envOr("GOARCH", runtime.GOARCH) c.GOARCH = envOr("GOARCH", runtime.GOARCH)
c.GOOS = envOr("GOOS", runtime.GOOS) c.GOOS = envOr("GOOS", runtime.GOOS)
c.GOROOT = runtime.GOROOT() c.GOROOT = pathpkg.Clean(runtime.GOROOT())
c.GOPATH = envOr("GOPATH", "") c.GOPATH = envOr("GOPATH", "")
c.Compiler = runtime.Compiler c.Compiler = runtime.Compiler
@ -299,7 +298,7 @@ func defaultContext() Context {
// in all releases >= Go 1.x. Code that requires Go 1.x or later should // in all releases >= Go 1.x. Code that requires Go 1.x or later should
// say "+build go1.x", and code that should only be built before Go 1.x // say "+build go1.x", and code that should only be built before Go 1.x
// (perhaps it is the stub to use in that case) should say "+build !go1.x". // (perhaps it is the stub to use in that case) should say "+build !go1.x".
c.ReleaseTags = []string{"go1.1", "go1.2", "go1.3", "go1.4", "go1.5"} c.ReleaseTags = []string{"go1.1", "go1.2", "go1.3", "go1.4", "go1.5", "go1.6"}
switch os.Getenv("CGO_ENABLED") { switch os.Getenv("CGO_ENABLED") {
case "1": case "1":
@ -344,6 +343,21 @@ const (
// or finds conflicting comments in multiple source files. // or finds conflicting comments in multiple source files.
// See golang.org/s/go14customimport for more information. // See golang.org/s/go14customimport for more information.
ImportComment ImportComment
// By default, Import searches vendor directories
// that apply in the given source directory before searching
// the GOROOT and GOPATH roots.
// If an Import finds and returns a package using a vendor
// directory, the resulting ImportPath is the complete path
// to the package, including the path elements leading up
// to and including "vendor".
// For example, if Import("y", "x/subdir", 0) finds
// "x/vendor/y", the returned package's ImportPath is "x/vendor/y",
// not plain "y".
// See golang.org/s/go15vendor for more information.
//
// Setting IgnoreVendor ignores vendor directories.
IgnoreVendor
) )
// A Package describes the Go package found in a directory. // A Package describes the Go package found in a directory.
@ -367,6 +381,7 @@ type Package struct {
GoFiles []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles) GoFiles []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
CgoFiles []string // .go source files that import "C" CgoFiles []string // .go source files that import "C"
IgnoredGoFiles []string // .go source files ignored for this build IgnoredGoFiles []string // .go source files ignored for this build
InvalidGoFiles []string // .go source files with detected problems (parse error, wrong package name, and so on)
CFiles []string // .c source files CFiles []string // .c source files
CXXFiles []string // .cc, .cpp and .cxx source files CXXFiles []string // .cc, .cpp and .cxx source files
MFiles []string // .m (Objective-C) source files MFiles []string // .m (Objective-C) source files
@ -475,15 +490,22 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa
switch ctxt.Compiler { switch ctxt.Compiler {
case "gccgo": case "gccgo":
pkgtargetroot = "pkg/gccgo_" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix pkgtargetroot = "pkg/gccgo_" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix
dir, elem := pathpkg.Split(p.ImportPath)
pkga = pkgtargetroot + "/" + dir + "lib" + elem + ".a"
case "gc": case "gc":
pkgtargetroot = "pkg/" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix pkgtargetroot = "pkg/" + ctxt.GOOS + "_" + ctxt.GOARCH + suffix
pkga = pkgtargetroot + "/" + p.ImportPath + ".a"
default: default:
// Save error for end of function. // Save error for end of function.
pkgerr = fmt.Errorf("import %q: unknown compiler %q", path, ctxt.Compiler) pkgerr = fmt.Errorf("import %q: unknown compiler %q", path, ctxt.Compiler)
} }
setPkga := func() {
switch ctxt.Compiler {
case "gccgo":
dir, elem := pathpkg.Split(p.ImportPath)
pkga = pkgtargetroot + "/" + dir + "lib" + elem + ".a"
case "gc":
pkga = pkgtargetroot + "/" + p.ImportPath + ".a"
}
}
setPkga()
binaryOnly := false binaryOnly := false
if IsLocalImport(path) { if IsLocalImport(path) {
@ -544,9 +566,50 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa
// tried records the location of unsuccessful package lookups // tried records the location of unsuccessful package lookups
var tried struct { var tried struct {
vendor []string
goroot string goroot string
gopath []string gopath []string
} }
gopath := ctxt.gopath()
// Vendor directories get first chance to satisfy import.
if mode&IgnoreVendor == 0 && srcDir != "" {
searchVendor := func(root string, isGoroot bool) bool {
sub, ok := ctxt.hasSubdir(root, srcDir)
if !ok || !strings.HasPrefix(sub, "src/") || strings.Contains(sub, "/testdata/") {
return false
}
for {
vendor := ctxt.joinPath(root, sub, "vendor")
if ctxt.isDir(vendor) {
dir := ctxt.joinPath(vendor, path)
if ctxt.isDir(dir) && hasGoFiles(ctxt, dir) {
p.Dir = dir
p.ImportPath = strings.TrimPrefix(pathpkg.Join(sub, "vendor", path), "src/")
p.Goroot = isGoroot
p.Root = root
setPkga() // p.ImportPath changed
return true
}
tried.vendor = append(tried.vendor, dir)
}
i := strings.LastIndex(sub, "/")
if i < 0 {
break
}
sub = sub[:i]
}
return false
}
if searchVendor(ctxt.GOROOT, true) {
goto Found
}
for _, root := range gopath {
if searchVendor(root, false) {
goto Found
}
}
}
// Determine directory from import path. // Determine directory from import path.
if ctxt.GOROOT != "" { if ctxt.GOROOT != "" {
@ -561,7 +624,7 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa
} }
tried.goroot = dir tried.goroot = dir
} }
for _, root := range ctxt.gopath() { for _, root := range gopath {
dir := ctxt.joinPath(root, "src", path) dir := ctxt.joinPath(root, "src", path)
isDir := ctxt.isDir(dir) isDir := ctxt.isDir(dir)
binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(root, pkga)) binaryOnly = !isDir && mode&AllowBinary != 0 && pkga != "" && ctxt.isFile(ctxt.joinPath(root, pkga))
@ -575,20 +638,22 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa
// package was not found // package was not found
var paths []string var paths []string
format := "\t%s (vendor tree)"
for _, dir := range tried.vendor {
paths = append(paths, fmt.Sprintf(format, dir))
format = "\t%s"
}
if tried.goroot != "" { if tried.goroot != "" {
paths = append(paths, fmt.Sprintf("\t%s (from $GOROOT)", tried.goroot)) paths = append(paths, fmt.Sprintf("\t%s (from $GOROOT)", tried.goroot))
} else { } else {
paths = append(paths, "\t($GOROOT not set)") paths = append(paths, "\t($GOROOT not set)")
} }
var i int format = "\t%s (from $GOPATH)"
var format = "\t%s (from $GOPATH)" for _, dir := range tried.gopath {
for ; i < len(tried.gopath); i++ { paths = append(paths, fmt.Sprintf(format, dir))
if i > 0 { format = "\t%s"
format = "\t%s"
}
paths = append(paths, fmt.Sprintf(format, tried.gopath[i]))
} }
if i == 0 { if len(tried.gopath) == 0 {
paths = append(paths, "\t($GOPATH not set)") paths = append(paths, "\t($GOPATH not set)")
} }
return p, fmt.Errorf("cannot find package %q in any of:\n%s", path, strings.Join(paths, "\n")) return p, fmt.Errorf("cannot find package %q in any of:\n%s", path, strings.Join(paths, "\n"))
@ -617,6 +682,7 @@ Found:
return p, err return p, err
} }
var badGoError error
var Sfiles []string // files with ".S" (capital S) var Sfiles []string // files with ".S" (capital S)
var firstFile, firstCommentFile string var firstFile, firstCommentFile string
imported := make(map[string][]token.Position) imported := make(map[string][]token.Position)
@ -632,9 +698,17 @@ Found:
name := d.Name() name := d.Name()
ext := nameExt(name) ext := nameExt(name)
badFile := func(err error) {
if badGoError == nil {
badGoError = err
}
p.InvalidGoFiles = append(p.InvalidGoFiles, name)
}
match, data, filename, err := ctxt.matchFile(p.Dir, name, true, allTags) match, data, filename, err := ctxt.matchFile(p.Dir, name, true, allTags)
if err != nil { if err != nil {
return p, err badFile(err)
continue
} }
if !match { if !match {
if ext == ".go" { if ext == ".go" {
@ -679,7 +753,8 @@ Found:
pf, err := parser.ParseFile(fset, filename, data, parser.ImportsOnly|parser.ParseComments) pf, err := parser.ParseFile(fset, filename, data, parser.ImportsOnly|parser.ParseComments)
if err != nil { if err != nil {
return p, err badFile(err)
continue
} }
pkg := pf.Name.Name pkg := pf.Name.Name
@ -699,11 +774,12 @@ Found:
p.Name = pkg p.Name = pkg
firstFile = name firstFile = name
} else if pkg != p.Name { } else if pkg != p.Name {
return p, &MultiplePackageError{ badFile(&MultiplePackageError{
Dir: p.Dir, Dir: p.Dir,
Packages: []string{p.Name, pkg}, Packages: []string{p.Name, pkg},
Files: []string{firstFile, name}, Files: []string{firstFile, name},
} })
p.InvalidGoFiles = append(p.InvalidGoFiles, name)
} }
if pf.Doc != nil && p.Doc == "" { if pf.Doc != nil && p.Doc == "" {
p.Doc = doc.Synopsis(pf.Doc.Text()) p.Doc = doc.Synopsis(pf.Doc.Text())
@ -714,13 +790,12 @@ Found:
if line != 0 { if line != 0 {
com, err := strconv.Unquote(qcom) com, err := strconv.Unquote(qcom)
if err != nil { if err != nil {
return p, fmt.Errorf("%s:%d: cannot parse import comment", filename, line) badFile(fmt.Errorf("%s:%d: cannot parse import comment", filename, line))
} } else if p.ImportComment == "" {
if p.ImportComment == "" {
p.ImportComment = com p.ImportComment = com
firstCommentFile = name firstCommentFile = name
} else if p.ImportComment != com { } else if p.ImportComment != com {
return p, fmt.Errorf("found import comments %q (%s) and %q (%s) in %s", p.ImportComment, firstCommentFile, com, name, p.Dir) badFile(fmt.Errorf("found import comments %q (%s) and %q (%s) in %s", p.ImportComment, firstCommentFile, com, name, p.Dir))
} }
} }
} }
@ -751,18 +826,19 @@ Found:
} }
if path == "C" { if path == "C" {
if isTest { if isTest {
return p, fmt.Errorf("use of cgo in test %s not supported", filename) badFile(fmt.Errorf("use of cgo in test %s not supported", filename))
} } else {
cg := spec.Doc cg := spec.Doc
if cg == nil && len(d.Specs) == 1 { if cg == nil && len(d.Specs) == 1 {
cg = d.Doc cg = d.Doc
}
if cg != nil {
if err := ctxt.saveCgo(filename, p, cg); err != nil {
return p, err
} }
if cg != nil {
if err := ctxt.saveCgo(filename, p, cg); err != nil {
badFile(err)
}
}
isCgo = true
} }
isCgo = true
} }
} }
} }
@ -781,6 +857,9 @@ Found:
p.GoFiles = append(p.GoFiles, name) p.GoFiles = append(p.GoFiles, name)
} }
} }
if badGoError != nil {
return p, badGoError
}
if len(p.GoFiles)+len(p.CgoFiles)+len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 { if len(p.GoFiles)+len(p.CgoFiles)+len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
return p, &NoGoError{p.Dir} return p, &NoGoError{p.Dir}
} }
@ -805,6 +884,20 @@ Found:
return p, pkgerr return p, pkgerr
} }
// hasGoFiles reports whether dir contains any files with names ending in .go.
// For a vendor check we must exclude directories that contain no .go files.
// Otherwise it is not possible to vendor just a/b/c and still import the
// non-vendored a/b. See golang.org/issue/13832.
func hasGoFiles(ctxt *Context, dir string) bool {
ents, _ := ctxt.readDir(dir)
for _, ent := range ents {
if !ent.IsDir() && strings.HasSuffix(ent.Name(), ".go") {
return true
}
}
return false
}
func findImportComment(data []byte) (s string, line int) { func findImportComment(data []byte) (s string, line int) {
// expect keyword package // expect keyword package
word, data := parseWord(data) word, data := parseWord(data)
@ -1128,9 +1221,9 @@ func (ctxt *Context) saveCgo(filename string, di *Package, cg *ast.CommentGroup)
if err != nil { if err != nil {
return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig) return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig)
} }
var ok bool
for i, arg := range args { for i, arg := range args {
arg = expandSrcDir(arg, di.Dir) if arg, ok = expandSrcDir(arg, di.Dir); !ok {
if !safeCgoName(arg) {
return fmt.Errorf("%s: malformed #cgo argument: %s", filename, arg) return fmt.Errorf("%s: malformed #cgo argument: %s", filename, arg)
} }
args[i] = arg args[i] = arg
@ -1154,25 +1247,47 @@ func (ctxt *Context) saveCgo(filename string, di *Package, cg *ast.CommentGroup)
return nil return nil
} }
func expandSrcDir(str string, srcdir string) string { // expandSrcDir expands any occurrence of ${SRCDIR}, making sure
// the result is safe for the shell.
func expandSrcDir(str string, srcdir string) (string, bool) {
// "\" delimited paths cause safeCgoName to fail // "\" delimited paths cause safeCgoName to fail
// so convert native paths with a different delimeter // so convert native paths with a different delimeter
// to "/" before starting (eg: on windows) // to "/" before starting (eg: on windows).
srcdir = filepath.ToSlash(srcdir) srcdir = filepath.ToSlash(srcdir)
return strings.Replace(str, "${SRCDIR}", srcdir, -1)
// Spaces are tolerated in ${SRCDIR}, but not anywhere else.
chunks := strings.Split(str, "${SRCDIR}")
if len(chunks) < 2 {
return str, safeCgoName(str, false)
}
ok := true
for _, chunk := range chunks {
ok = ok && (chunk == "" || safeCgoName(chunk, false))
}
ok = ok && (srcdir == "" || safeCgoName(srcdir, true))
res := strings.Join(chunks, srcdir)
return res, ok && res != ""
} }
// NOTE: $ is not safe for the shell, but it is allowed here because of linker options like -Wl,$ORIGIN. // NOTE: $ is not safe for the shell, but it is allowed here because of linker options like -Wl,$ORIGIN.
// We never pass these arguments to a shell (just to programs we construct argv for), so this should be okay. // We never pass these arguments to a shell (just to programs we construct argv for), so this should be okay.
// See golang.org/issue/6038. // See golang.org/issue/6038.
var safeBytes = []byte("+-.,/0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz:$") // The @ is for OS X. See golang.org/issue/13720.
const safeString = "+-.,/0123456789=ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz:$@"
const safeSpaces = " "
func safeCgoName(s string) bool { var safeBytes = []byte(safeSpaces + safeString)
func safeCgoName(s string, spaces bool) bool {
if s == "" { if s == "" {
return false return false
} }
safe := safeBytes
if !spaces {
safe = safe[len(safeSpaces):]
}
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
if c := s[i]; c < 0x80 && bytes.IndexByte(safeBytes, c) < 0 { if c := s[i]; c < 0x80 && bytes.IndexByte(safe, c) < 0 {
return false return false
} }
} }

View File

@ -5,6 +5,7 @@
package build package build
import ( import (
"internal/testenv"
"io" "io"
"os" "os"
"path/filepath" "path/filepath"
@ -109,7 +110,6 @@ func TestMultiplePackageImport(t *testing.T) {
} }
func TestLocalDirectory(t *testing.T) { func TestLocalDirectory(t *testing.T) {
return // go1.3 not happy with this test.
if runtime.GOOS == "darwin" { if runtime.GOOS == "darwin" {
switch runtime.GOARCH { switch runtime.GOARCH {
case "arm", "arm64": case "arm", "arm64":
@ -126,8 +126,8 @@ func TestLocalDirectory(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if p.ImportPath != "k8s.io/kubernetes/third_party/golang/go/build" { if p.ImportPath != "go/build" {
t.Fatalf("ImportPath=%q, want %q", p.ImportPath, "k8s.io/kubernetes/third_party/golang/go/build") t.Fatalf("ImportPath=%q, want %q", p.ImportPath, "go/build")
} }
} }
@ -231,7 +231,6 @@ func TestMatchFile(t *testing.T) {
} }
func TestImportCmd(t *testing.T) { func TestImportCmd(t *testing.T) {
return // go1.3 not happy with this test
if runtime.GOOS == "darwin" { if runtime.GOOS == "darwin" {
switch runtime.GOARCH { switch runtime.GOARCH {
case "arm", "arm64": case "arm", "arm64":
@ -269,7 +268,7 @@ var expandSrcDirTests = []struct {
func TestExpandSrcDir(t *testing.T) { func TestExpandSrcDir(t *testing.T) {
for _, test := range expandSrcDirTests { for _, test := range expandSrcDirTests {
output := expandSrcDir(test.input, expandSrcDirPath) output, _ := expandSrcDir(test.input, expandSrcDirPath)
if output != test.expected { if output != test.expected {
t.Errorf("%q expands to %q with SRCDIR=%q when %q is expected", test.input, output, expandSrcDirPath, test.expected) t.Errorf("%q expands to %q with SRCDIR=%q when %q is expected", test.input, output, expandSrcDirPath, test.expected)
} else { } else {
@ -277,3 +276,73 @@ func TestExpandSrcDir(t *testing.T) {
} }
} }
} }
func TestShellSafety(t *testing.T) {
tests := []struct {
input, srcdir, expected string
result bool
}{
{"-I${SRCDIR}/../include", "/projects/src/issue 11868", "-I/projects/src/issue 11868/../include", true},
{"-X${SRCDIR}/1,${SRCDIR}/2", "/projects/src/issue 11868", "-X/projects/src/issue 11868/1,/projects/src/issue 11868/2", true},
{"-I/tmp -I/tmp", "/tmp2", "-I/tmp -I/tmp", false},
{"-I/tmp", "/tmp/[0]", "-I/tmp", true},
{"-I${SRCDIR}/dir", "/tmp/[0]", "-I/tmp/[0]/dir", false},
}
for _, test := range tests {
output, ok := expandSrcDir(test.input, test.srcdir)
if ok != test.result {
t.Errorf("Expected %t while %q expands to %q with SRCDIR=%q; got %t", test.result, test.input, output, test.srcdir, ok)
}
if output != test.expected {
t.Errorf("Expected %q while %q expands with SRCDIR=%q; got %q", test.expected, test.input, test.srcdir, output)
}
}
}
func TestImportVendor(t *testing.T) {
t.Skip("skipping; hpack has moved to internal for now; golang.org/issue/14047")
testenv.MustHaveGoBuild(t) // really must just have source
ctxt := Default
ctxt.GOPATH = ""
p, err := ctxt.Import("golang.org/x/net/http2/hpack", filepath.Join(ctxt.GOROOT, "src/net/http"), 0)
if err != nil {
t.Fatalf("cannot find vendored golang.org/x/net/http2/hpack from net/http directory: %v", err)
}
want := "vendor/golang.org/x/net/http2/hpack"
if p.ImportPath != want {
t.Fatalf("Import succeeded but found %q, want %q", p.ImportPath, want)
}
}
func TestImportVendorFailure(t *testing.T) {
testenv.MustHaveGoBuild(t) // really must just have source
ctxt := Default
ctxt.GOPATH = ""
p, err := ctxt.Import("x.com/y/z", filepath.Join(ctxt.GOROOT, "src/net/http"), 0)
if err == nil {
t.Fatalf("found made-up package x.com/y/z in %s", p.Dir)
}
e := err.Error()
if !strings.Contains(e, " (vendor tree)") {
t.Fatalf("error on failed import does not mention GOROOT/src/vendor directory:\n%s", e)
}
}
func TestImportVendorParentFailure(t *testing.T) {
testenv.MustHaveGoBuild(t) // really must just have source
ctxt := Default
ctxt.GOPATH = ""
// This import should fail because the vendor/golang.org/x/net/http2 directory has no source code.
p, err := ctxt.Import("golang.org/x/net/http2", filepath.Join(ctxt.GOROOT, "src/net/http"), 0)
if err == nil {
t.Fatalf("found empty parent in %s", p.Dir)
}
if p != nil && p.Dir != "" {
t.Fatalf("decided to use %s", p.Dir)
}
e := err.Error()
if !strings.Contains(e, " (vendor tree)") {
t.Fatalf("error on failed import does not mention GOROOT/src/vendor directory:\n%s", e)
}
}

View File

@ -34,17 +34,21 @@ import (
// //
var pkgDeps = map[string][]string{ var pkgDeps = map[string][]string{
// L0 is the lowest level, core, nearly unavoidable packages. // L0 is the lowest level, core, nearly unavoidable packages.
"errors": {}, "errors": {},
"io": {"errors", "sync"}, "io": {"errors", "sync"},
"runtime": {"unsafe"}, "runtime": {"unsafe", "runtime/internal/atomic", "runtime/internal/sys"},
"sync": {"runtime", "sync/atomic", "unsafe"}, "runtime/internal/sys": {},
"sync/atomic": {"unsafe"}, "runtime/internal/atomic": {"unsafe", "runtime/internal/sys"},
"unsafe": {}, "internal/race": {"runtime", "unsafe"},
"sync": {"internal/race", "runtime", "sync/atomic", "unsafe"},
"sync/atomic": {"unsafe"},
"unsafe": {},
"L0": { "L0": {
"errors", "errors",
"io", "io",
"runtime", "runtime",
"runtime/internal/atomic",
"sync", "sync",
"sync/atomic", "sync/atomic",
"unsafe", "unsafe",
@ -128,17 +132,16 @@ var pkgDeps = map[string][]string{
// End of linear dependency definitions. // End of linear dependency definitions.
// Operating system access. // Operating system access.
"syscall": {"L0", "unicode/utf16"}, "syscall": {"L0", "internal/race", "internal/syscall/windows/sysdll", "unicode/utf16"},
"internal/syscall/unix": {"L0", "syscall"}, "internal/syscall/unix": {"L0", "syscall"},
"internal/syscall/windows": {"L0", "syscall"}, "internal/syscall/windows": {"L0", "syscall", "internal/syscall/windows/sysdll"},
"internal/syscall/windows/registry": {"L0", "syscall", "unicode/utf16"}, "internal/syscall/windows/registry": {"L0", "syscall", "internal/syscall/windows/sysdll", "unicode/utf16"},
"time": {"L0", "syscall", "internal/syscall/windows/registry"}, "time": {"L0", "syscall", "internal/syscall/windows/registry"},
"os": {"L1", "os", "syscall", "time", "internal/syscall/windows"}, "os": {"L1", "os", "syscall", "time", "internal/syscall/windows"},
"path/filepath": {"L2", "os", "syscall"}, "path/filepath": {"L2", "os", "syscall"},
"io/ioutil": {"L2", "os", "path/filepath", "time"}, "io/ioutil": {"L2", "os", "path/filepath", "time"},
"os/exec": {"L2", "os", "path/filepath", "syscall"}, "os/exec": {"L2", "os", "path/filepath", "syscall"},
"os/signal": {"L2", "os", "syscall"}, "os/signal": {"L2", "os", "syscall"},
"internal/syscall": {"L2", "runtime", "syscall", "sync/atomic", "unsafe"},
// OS enables basic operating system functionality, // OS enables basic operating system functionality,
// but not direct use of package syscall, nor os/signal. // but not direct use of package syscall, nor os/signal.
@ -162,7 +165,7 @@ var pkgDeps = map[string][]string{
"runtime/trace": {"L0"}, "runtime/trace": {"L0"},
"text/tabwriter": {"L2"}, "text/tabwriter": {"L2"},
"testing": {"L2", "flag", "fmt", "os", "runtime/pprof", "runtime/trace", "time"}, "testing": {"L2", "flag", "fmt", "os", "runtime/debug", "runtime/pprof", "runtime/trace", "time"},
"testing/iotest": {"L2", "log"}, "testing/iotest": {"L2", "log"},
"testing/quick": {"L2", "flag", "fmt", "reflect"}, "testing/quick": {"L2", "flag", "fmt", "reflect"},
"internal/testenv": {"L2", "os", "testing"}, "internal/testenv": {"L2", "os", "testing"},
@ -215,7 +218,7 @@ var pkgDeps = map[string][]string{
"database/sql": {"L4", "container/list", "database/sql/driver"}, "database/sql": {"L4", "container/list", "database/sql/driver"},
"database/sql/driver": {"L4", "time"}, "database/sql/driver": {"L4", "time"},
"debug/dwarf": {"L4"}, "debug/dwarf": {"L4"},
"debug/elf": {"L4", "OS", "debug/dwarf"}, "debug/elf": {"L4", "OS", "debug/dwarf", "compress/zlib"},
"debug/gosym": {"L4"}, "debug/gosym": {"L4"},
"debug/macho": {"L4", "OS", "debug/dwarf"}, "debug/macho": {"L4", "OS", "debug/dwarf"},
"debug/pe": {"L4", "OS", "debug/dwarf"}, "debug/pe": {"L4", "OS", "debug/dwarf"},
@ -257,6 +260,8 @@ var pkgDeps = map[string][]string{
}, },
// Cgo. // Cgo.
// If you add a dependency on CGO, you must add the package to
// cgoPackages in cmd/dist/test.go.
"runtime/cgo": {"L0", "C"}, "runtime/cgo": {"L0", "C"},
"CGO": {"C", "runtime/cgo"}, "CGO": {"C", "runtime/cgo"},
@ -264,16 +269,17 @@ var pkgDeps = map[string][]string{
// that shows up in programs that use cgo. // that shows up in programs that use cgo.
"C": {}, "C": {},
// Race detector uses cgo. // Race detector/MSan uses cgo.
"runtime/race": {"C"}, "runtime/race": {"C"},
"runtime/msan": {"C"},
// Plan 9 alone needs io/ioutil and os. // Plan 9 alone needs io/ioutil and os.
"os/user": {"L4", "CGO", "io/ioutil", "os", "syscall"}, "os/user": {"L4", "CGO", "io/ioutil", "os", "syscall"},
// Basic networking. // Basic networking.
// Because net must be used by any package that wants to // Because net must be used by any package that wants to
// do networking portably, it must have a small dependency set: just L1+basic os. // do networking portably, it must have a small dependency set: just L0+basic os.
"net": {"L1", "CGO", "os", "syscall", "time", "internal/syscall/windows", "internal/singleflight"}, "net": {"L0", "CGO", "math/rand", "os", "sort", "syscall", "time", "internal/syscall/windows", "internal/singleflight", "internal/race"},
// NET enables use of basic network-related packages. // NET enables use of basic network-related packages.
"NET": { "NET": {
@ -312,7 +318,7 @@ var pkgDeps = map[string][]string{
// Random byte, number generation. // Random byte, number generation.
// This would be part of core crypto except that it imports // This would be part of core crypto except that it imports
// math/big, which imports fmt. // math/big, which imports fmt.
"crypto/rand": {"L4", "CRYPTO", "OS", "math/big", "syscall", "internal/syscall/unix", "internal/syscall"}, "crypto/rand": {"L4", "CRYPTO", "OS", "math/big", "syscall", "internal/syscall/unix"},
// Mathematical crypto: dependencies on fmt (L4) and math/big. // Mathematical crypto: dependencies on fmt (L4) and math/big.
// We could avoid some of the fmt, but math/big imports fmt anyway. // We could avoid some of the fmt, but math/big imports fmt anyway.
@ -334,7 +340,7 @@ var pkgDeps = map[string][]string{
// SSL/TLS. // SSL/TLS.
"crypto/tls": { "crypto/tls": {
"L4", "CRYPTO-MATH", "CGO", "OS", "L4", "CRYPTO-MATH", "OS",
"container/list", "crypto/x509", "encoding/pem", "net", "syscall", "container/list", "crypto/x509", "encoding/pem", "net", "syscall",
}, },
"crypto/x509": { "crypto/x509": {
@ -352,6 +358,7 @@ var pkgDeps = map[string][]string{
"L4", "NET", "OS", "L4", "NET", "OS",
"compress/gzip", "crypto/tls", "mime/multipart", "runtime/debug", "compress/gzip", "crypto/tls", "mime/multipart", "runtime/debug",
"net/http/internal", "net/http/internal",
"internal/golang.org/x/net/http2/hpack",
}, },
"net/http/internal": {"L4"}, "net/http/internal": {"L4"},
@ -360,7 +367,7 @@ var pkgDeps = map[string][]string{
"net/http/cgi": {"L4", "NET", "OS", "crypto/tls", "net/http", "regexp"}, "net/http/cgi": {"L4", "NET", "OS", "crypto/tls", "net/http", "regexp"},
"net/http/cookiejar": {"L4", "NET", "net/http"}, "net/http/cookiejar": {"L4", "NET", "net/http"},
"net/http/fcgi": {"L4", "NET", "OS", "net/http", "net/http/cgi"}, "net/http/fcgi": {"L4", "NET", "OS", "net/http", "net/http/cgi"},
"net/http/httptest": {"L4", "NET", "OS", "crypto/tls", "flag", "net/http"}, "net/http/httptest": {"L4", "NET", "OS", "crypto/tls", "flag", "net/http", "net/http/internal"},
"net/http/httputil": {"L4", "NET", "OS", "net/http", "net/http/internal"}, "net/http/httputil": {"L4", "NET", "OS", "net/http", "net/http/internal"},
"net/http/pprof": {"L4", "OS", "html/template", "net/http", "runtime/pprof", "runtime/trace"}, "net/http/pprof": {"L4", "OS", "html/template", "net/http", "runtime/pprof", "runtime/trace"},
"net/rpc": {"L4", "NET", "encoding/gob", "html/template", "net/http"}, "net/rpc": {"L4", "NET", "encoding/gob", "html/template", "net/http"},
@ -441,7 +448,6 @@ func listStdPkgs(goroot string) ([]string, error) {
} }
func TestDependencies(t *testing.T) { func TestDependencies(t *testing.T) {
return // go1.3 is really not happy with this test
iOS := runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64") iOS := runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64")
if runtime.GOOS == "nacl" || iOS { if runtime.GOOS == "nacl" || iOS {
// Tests run in a limited file system and we do not // Tests run in a limited file system and we do not
@ -456,26 +462,23 @@ func TestDependencies(t *testing.T) {
} }
sort.Strings(all) sort.Strings(all)
test := func(mustImport bool) { for _, pkg := range all {
for _, pkg := range all { imports, err := findImports(pkg)
imports, err := findImports(pkg) if err != nil {
if err != nil { t.Error(err)
t.Error(err) continue
continue }
} ok := allowed(pkg)
ok := allowed(pkg) var bad []string
var bad []string for _, imp := range imports {
for _, imp := range imports { if !ok[imp] {
if !ok[imp] { bad = append(bad, imp)
bad = append(bad, imp)
}
}
if bad != nil {
t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
} }
} }
if bad != nil {
t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
}
} }
test(true)
} }
var buildIgnore = []byte("\n// +build ignore") var buildIgnore = []byte("\n// +build ignore")

View File

@ -102,6 +102,7 @@
// - "go1.3", from Go version 1.3 onward // - "go1.3", from Go version 1.3 onward
// - "go1.4", from Go version 1.4 onward // - "go1.4", from Go version 1.4 onward
// - "go1.5", from Go version 1.5 onward // - "go1.5", from Go version 1.5 onward
// - "go1.6", from Go version 1.6 onward
// - any additional words listed in ctxt.BuildTags // - any additional words listed in ctxt.BuildTags
// //
// If a file's name, after stripping the extension and a possible _test suffix, // If a file's name, after stripping the extension and a possible _test suffix,