...
Run Format

Source file src/go/build/deps_test.go

Documentation: go/build

     1  // Copyright 2012 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This file exercises the import parser but also checks that
     6  // some low-level packages do not have new dependencies added.
     7  
     8  package build
     9  
    10  import (
    11  	"bytes"
    12  	"fmt"
    13  	"io/ioutil"
    14  	"os"
    15  	"path/filepath"
    16  	"runtime"
    17  	"sort"
    18  	"strconv"
    19  	"strings"
    20  	"testing"
    21  )
    22  
    23  // pkgDeps defines the expected dependencies between packages in
    24  // the Go source tree. It is a statement of policy.
    25  // Changes should not be made to this map without prior discussion.
    26  //
    27  // The map contains two kinds of entries:
    28  // 1) Lower-case keys are standard import paths and list the
    29  // allowed imports in that package.
    30  // 2) Upper-case keys define aliases for package sets, which can then
    31  // be used as dependencies by other rules.
    32  //
    33  // DO NOT CHANGE THIS DATA TO FIX BUILDS.
    34  //
    35  var pkgDeps = map[string][]string{
    36  	// L0 is the lowest level, core, nearly unavoidable packages.
    37  	"errors":                  {},
    38  	"io":                      {"errors", "sync", "sync/atomic"},
    39  	"runtime":                 {"unsafe", "runtime/internal/atomic", "runtime/internal/sys"},
    40  	"runtime/internal/sys":    {},
    41  	"runtime/internal/atomic": {"unsafe", "runtime/internal/sys"},
    42  	"internal/race":           {"runtime", "unsafe"},
    43  	"sync":                    {"internal/race", "runtime", "sync/atomic", "unsafe"},
    44  	"sync/atomic":             {"unsafe"},
    45  	"unsafe":                  {},
    46  	"internal/cpu":            {"runtime"},
    47  
    48  	"L0": {
    49  		"errors",
    50  		"io",
    51  		"runtime",
    52  		"runtime/internal/atomic",
    53  		"sync",
    54  		"sync/atomic",
    55  		"unsafe",
    56  		"internal/cpu",
    57  	},
    58  
    59  	// L1 adds simple functions and strings processing,
    60  	// but not Unicode tables.
    61  	"math":          {"internal/cpu", "unsafe"},
    62  	"math/bits":     {},
    63  	"math/cmplx":    {"math"},
    64  	"math/rand":     {"L0", "math"},
    65  	"strconv":       {"L0", "unicode/utf8", "math"},
    66  	"unicode/utf16": {},
    67  	"unicode/utf8":  {},
    68  
    69  	"L1": {
    70  		"L0",
    71  		"math",
    72  		"math/bits",
    73  		"math/cmplx",
    74  		"math/rand",
    75  		"sort",
    76  		"strconv",
    77  		"unicode/utf16",
    78  		"unicode/utf8",
    79  	},
    80  
    81  	// L2 adds Unicode and strings processing.
    82  	"bufio":   {"L0", "unicode/utf8", "bytes"},
    83  	"bytes":   {"L0", "unicode", "unicode/utf8"},
    84  	"path":    {"L0", "unicode/utf8", "strings"},
    85  	"strings": {"L0", "unicode", "unicode/utf8"},
    86  	"unicode": {},
    87  
    88  	"L2": {
    89  		"L1",
    90  		"bufio",
    91  		"bytes",
    92  		"path",
    93  		"strings",
    94  		"unicode",
    95  	},
    96  
    97  	// L3 adds reflection and some basic utility packages
    98  	// and interface definitions, but nothing that makes
    99  	// system calls.
   100  	"crypto":              {"L2", "hash"}, // interfaces
   101  	"crypto/cipher":       {"L2", "crypto/subtle"},
   102  	"crypto/subtle":       {},
   103  	"encoding/base32":     {"L2"},
   104  	"encoding/base64":     {"L2", "encoding/binary"},
   105  	"encoding/binary":     {"L2", "reflect"},
   106  	"hash":                {"L2"}, // interfaces
   107  	"hash/adler32":        {"L2", "hash"},
   108  	"hash/crc32":          {"L2", "hash"},
   109  	"hash/crc64":          {"L2", "hash"},
   110  	"hash/fnv":            {"L2", "hash"},
   111  	"image":               {"L2", "image/color"}, // interfaces
   112  	"image/color":         {"L2"},                // interfaces
   113  	"image/color/palette": {"L2", "image/color"},
   114  	"reflect":             {"L2"},
   115  	"sort":                {"reflect"},
   116  
   117  	"L3": {
   118  		"L2",
   119  		"crypto",
   120  		"crypto/cipher",
   121  		"crypto/internal/cipherhw",
   122  		"crypto/subtle",
   123  		"encoding/base32",
   124  		"encoding/base64",
   125  		"encoding/binary",
   126  		"hash",
   127  		"hash/adler32",
   128  		"hash/crc32",
   129  		"hash/crc64",
   130  		"hash/fnv",
   131  		"image",
   132  		"image/color",
   133  		"image/color/palette",
   134  		"reflect",
   135  	},
   136  
   137  	// End of linear dependency definitions.
   138  
   139  	// Operating system access.
   140  	"syscall":                           {"L0", "internal/race", "internal/syscall/windows/sysdll", "unicode/utf16"},
   141  	"internal/syscall/unix":             {"L0", "syscall"},
   142  	"internal/syscall/windows":          {"L0", "syscall", "internal/syscall/windows/sysdll"},
   143  	"internal/syscall/windows/registry": {"L0", "syscall", "internal/syscall/windows/sysdll", "unicode/utf16"},
   144  	"time": {
   145  		// "L0" without the "io" package:
   146  		"errors",
   147  		"runtime",
   148  		"runtime/internal/atomic",
   149  		"sync",
   150  		"sync/atomic",
   151  		"unsafe",
   152  		// Other time dependencies:
   153  		"internal/syscall/windows/registry",
   154  		"syscall",
   155  	},
   156  
   157  	"internal/poll":    {"L0", "internal/race", "syscall", "time", "unicode/utf16", "unicode/utf8", "internal/syscall/windows"},
   158  	"internal/testlog": {"L0"},
   159  	"os":               {"L1", "os", "syscall", "time", "internal/poll", "internal/syscall/windows", "internal/testlog"},
   160  	"path/filepath":    {"L2", "os", "syscall", "internal/syscall/windows"},
   161  	"io/ioutil":        {"L2", "os", "path/filepath", "time"},
   162  	"os/exec":          {"L2", "os", "context", "path/filepath", "syscall"},
   163  	"os/signal":        {"L2", "os", "syscall"},
   164  
   165  	// OS enables basic operating system functionality,
   166  	// but not direct use of package syscall, nor os/signal.
   167  	"OS": {
   168  		"io/ioutil",
   169  		"os",
   170  		"os/exec",
   171  		"path/filepath",
   172  		"time",
   173  	},
   174  
   175  	// Formatted I/O: few dependencies (L1) but we must add reflect.
   176  	"fmt": {"L1", "os", "reflect"},
   177  	"log": {"L1", "os", "fmt", "time"},
   178  
   179  	// Packages used by testing must be low-level (L2+fmt).
   180  	"regexp":         {"L2", "regexp/syntax"},
   181  	"regexp/syntax":  {"L2"},
   182  	"runtime/debug":  {"L2", "fmt", "io/ioutil", "os", "time"},
   183  	"runtime/pprof":  {"L2", "compress/gzip", "context", "encoding/binary", "fmt", "io/ioutil", "os", "text/tabwriter", "time"},
   184  	"runtime/trace":  {"L0"},
   185  	"text/tabwriter": {"L2"},
   186  
   187  	"testing":          {"L2", "flag", "fmt", "internal/race", "os", "runtime/debug", "runtime/pprof", "runtime/trace", "time"},
   188  	"testing/iotest":   {"L2", "log"},
   189  	"testing/quick":    {"L2", "flag", "fmt", "reflect", "time"},
   190  	"internal/testenv": {"L2", "OS", "flag", "testing", "syscall"},
   191  
   192  	// L4 is defined as L3+fmt+log+time, because in general once
   193  	// you're using L3 packages, use of fmt, log, or time is not a big deal.
   194  	"L4": {
   195  		"L3",
   196  		"fmt",
   197  		"log",
   198  		"time",
   199  	},
   200  
   201  	// Go parser.
   202  	"go/ast":     {"L4", "OS", "go/scanner", "go/token"},
   203  	"go/doc":     {"L4", "go/ast", "go/token", "regexp", "text/template"},
   204  	"go/parser":  {"L4", "OS", "go/ast", "go/scanner", "go/token"},
   205  	"go/printer": {"L4", "OS", "go/ast", "go/scanner", "go/token", "text/tabwriter"},
   206  	"go/scanner": {"L4", "OS", "go/token"},
   207  	"go/token":   {"L4"},
   208  
   209  	"GOPARSER": {
   210  		"go/ast",
   211  		"go/doc",
   212  		"go/parser",
   213  		"go/printer",
   214  		"go/scanner",
   215  		"go/token",
   216  	},
   217  
   218  	"go/format":       {"L4", "GOPARSER", "internal/format"},
   219  	"internal/format": {"L4", "GOPARSER"},
   220  
   221  	// Go type checking.
   222  	"go/constant":               {"L4", "go/token", "math/big"},
   223  	"go/importer":               {"L4", "go/build", "go/internal/gccgoimporter", "go/internal/gcimporter", "go/internal/srcimporter", "go/token", "go/types"},
   224  	"go/internal/gcimporter":    {"L4", "OS", "go/build", "go/constant", "go/token", "go/types", "text/scanner"},
   225  	"go/internal/gccgoimporter": {"L4", "OS", "debug/elf", "go/constant", "go/token", "go/types", "text/scanner"},
   226  	"go/internal/srcimporter":   {"L4", "fmt", "go/ast", "go/build", "go/parser", "go/token", "go/types", "path/filepath"},
   227  	"go/types":                  {"L4", "GOPARSER", "container/heap", "go/constant"},
   228  
   229  	// One of a kind.
   230  	"archive/tar":              {"L4", "OS", "syscall", "os/user"},
   231  	"archive/zip":              {"L4", "OS", "compress/flate"},
   232  	"container/heap":           {"sort"},
   233  	"compress/bzip2":           {"L4"},
   234  	"compress/flate":           {"L4"},
   235  	"compress/gzip":            {"L4", "compress/flate"},
   236  	"compress/lzw":             {"L4"},
   237  	"compress/zlib":            {"L4", "compress/flate"},
   238  	"context":                  {"errors", "fmt", "reflect", "sync", "time"},
   239  	"database/sql":             {"L4", "container/list", "context", "database/sql/driver", "database/sql/internal"},
   240  	"database/sql/driver":      {"L4", "context", "time", "database/sql/internal"},
   241  	"debug/dwarf":              {"L4"},
   242  	"debug/elf":                {"L4", "OS", "debug/dwarf", "compress/zlib"},
   243  	"debug/gosym":              {"L4"},
   244  	"debug/macho":              {"L4", "OS", "debug/dwarf"},
   245  	"debug/pe":                 {"L4", "OS", "debug/dwarf"},
   246  	"debug/plan9obj":           {"L4", "OS"},
   247  	"encoding":                 {"L4"},
   248  	"encoding/ascii85":         {"L4"},
   249  	"encoding/asn1":            {"L4", "math/big"},
   250  	"encoding/csv":             {"L4"},
   251  	"encoding/gob":             {"L4", "OS", "encoding"},
   252  	"encoding/hex":             {"L4"},
   253  	"encoding/json":            {"L4", "encoding"},
   254  	"encoding/pem":             {"L4"},
   255  	"encoding/xml":             {"L4", "encoding"},
   256  	"flag":                     {"L4", "OS"},
   257  	"go/build":                 {"L4", "OS", "GOPARSER"},
   258  	"html":                     {"L4"},
   259  	"image/draw":               {"L4", "image/internal/imageutil"},
   260  	"image/gif":                {"L4", "compress/lzw", "image/color/palette", "image/draw"},
   261  	"image/internal/imageutil": {"L4"},
   262  	"image/jpeg":               {"L4", "image/internal/imageutil"},
   263  	"image/png":                {"L4", "compress/zlib"},
   264  	"index/suffixarray":        {"L4", "regexp"},
   265  	"internal/singleflight":    {"sync"},
   266  	"internal/trace":           {"L4", "OS"},
   267  	"math/big":                 {"L4"},
   268  	"mime":                     {"L4", "OS", "syscall", "internal/syscall/windows/registry"},
   269  	"mime/quotedprintable":     {"L4"},
   270  	"net/internal/socktest":    {"L4", "OS", "syscall", "internal/syscall/windows"},
   271  	"net/url":                  {"L4"},
   272  	"plugin":                   {"L0", "OS", "CGO"},
   273  	"runtime/pprof/internal/profile": {"L4", "OS", "compress/gzip", "regexp"},
   274  	"testing/internal/testdeps":      {"L4", "internal/testlog", "runtime/pprof", "regexp"},
   275  	"text/scanner":                   {"L4", "OS"},
   276  	"text/template/parse":            {"L4"},
   277  
   278  	"html/template": {
   279  		"L4", "OS", "encoding/json", "html", "text/template",
   280  		"text/template/parse",
   281  	},
   282  	"text/template": {
   283  		"L4", "OS", "net/url", "text/template/parse",
   284  	},
   285  
   286  	// Cgo.
   287  	// If you add a dependency on CGO, you must add the package to
   288  	// cgoPackages in cmd/dist/test.go.
   289  	"runtime/cgo": {"L0", "C"},
   290  	"CGO":         {"C", "runtime/cgo"},
   291  
   292  	// Fake entry to satisfy the pseudo-import "C"
   293  	// that shows up in programs that use cgo.
   294  	"C": {},
   295  
   296  	// Race detector/MSan uses cgo.
   297  	"runtime/race": {"C"},
   298  	"runtime/msan": {"C"},
   299  
   300  	// Plan 9 alone needs io/ioutil and os.
   301  	"os/user": {"L4", "CGO", "io/ioutil", "os", "syscall"},
   302  
   303  	// Internal package used only for testing.
   304  	"os/signal/internal/pty": {"CGO", "fmt", "os", "syscall"},
   305  
   306  	// Basic networking.
   307  	// Because net must be used by any package that wants to
   308  	// do networking portably, it must have a small dependency set: just L0+basic os.
   309  	"net": {
   310  		"L0", "CGO",
   311  		"context", "math/rand", "os", "reflect", "sort", "syscall", "time",
   312  		"internal/nettrace", "internal/poll",
   313  		"internal/syscall/windows", "internal/singleflight", "internal/race",
   314  		"golang_org/x/net/lif", "golang_org/x/net/route",
   315  	},
   316  
   317  	// NET enables use of basic network-related packages.
   318  	"NET": {
   319  		"net",
   320  		"mime",
   321  		"net/textproto",
   322  		"net/url",
   323  	},
   324  
   325  	// Uses of networking.
   326  	"log/syslog":    {"L4", "OS", "net"},
   327  	"net/mail":      {"L4", "NET", "OS", "mime"},
   328  	"net/textproto": {"L4", "OS", "net"},
   329  
   330  	// Core crypto.
   331  	"crypto/aes":    {"L3"},
   332  	"crypto/des":    {"L3"},
   333  	"crypto/hmac":   {"L3"},
   334  	"crypto/md5":    {"L3"},
   335  	"crypto/rc4":    {"L3"},
   336  	"crypto/sha1":   {"L3"},
   337  	"crypto/sha256": {"L3"},
   338  	"crypto/sha512": {"L3"},
   339  
   340  	"CRYPTO": {
   341  		"crypto/aes",
   342  		"crypto/des",
   343  		"crypto/hmac",
   344  		"crypto/md5",
   345  		"crypto/rc4",
   346  		"crypto/sha1",
   347  		"crypto/sha256",
   348  		"crypto/sha512",
   349  		"golang_org/x/crypto/chacha20poly1305",
   350  		"golang_org/x/crypto/curve25519",
   351  		"golang_org/x/crypto/poly1305",
   352  	},
   353  
   354  	// Random byte, number generation.
   355  	// This would be part of core crypto except that it imports
   356  	// math/big, which imports fmt.
   357  	"crypto/rand": {"L4", "CRYPTO", "OS", "math/big", "syscall", "internal/syscall/unix"},
   358  
   359  	// Mathematical crypto: dependencies on fmt (L4) and math/big.
   360  	// We could avoid some of the fmt, but math/big imports fmt anyway.
   361  	"crypto/dsa":      {"L4", "CRYPTO", "math/big"},
   362  	"crypto/ecdsa":    {"L4", "CRYPTO", "crypto/elliptic", "math/big", "encoding/asn1"},
   363  	"crypto/elliptic": {"L4", "CRYPTO", "math/big"},
   364  	"crypto/rsa":      {"L4", "CRYPTO", "crypto/rand", "math/big"},
   365  
   366  	"CRYPTO-MATH": {
   367  		"CRYPTO",
   368  		"crypto/dsa",
   369  		"crypto/ecdsa",
   370  		"crypto/elliptic",
   371  		"crypto/rand",
   372  		"crypto/rsa",
   373  		"encoding/asn1",
   374  		"math/big",
   375  	},
   376  
   377  	// SSL/TLS.
   378  	"crypto/tls": {
   379  		"L4", "CRYPTO-MATH", "OS",
   380  		"container/list", "crypto/x509", "encoding/pem", "net", "syscall",
   381  	},
   382  	"crypto/x509": {
   383  		"L4", "CRYPTO-MATH", "OS", "CGO",
   384  		"crypto/x509/pkix", "encoding/pem", "encoding/hex", "net", "os/user", "syscall", "net/url",
   385  		"golang_org/x/crypto/cryptobyte", "golang_org/x/crypto/cryptobyte/asn1",
   386  	},
   387  	"crypto/x509/pkix": {"L4", "CRYPTO-MATH", "encoding/hex"},
   388  
   389  	// Simple net+crypto-aware packages.
   390  	"mime/multipart": {"L4", "OS", "mime", "crypto/rand", "net/textproto", "mime/quotedprintable"},
   391  	"net/smtp":       {"L4", "CRYPTO", "NET", "crypto/tls"},
   392  
   393  	// HTTP, kingpin of dependencies.
   394  	"net/http": {
   395  		"L4", "NET", "OS",
   396  		"compress/gzip",
   397  		"container/list",
   398  		"context",
   399  		"crypto/rand",
   400  		"crypto/tls",
   401  		"golang_org/x/net/http2/hpack",
   402  		"golang_org/x/net/idna",
   403  		"golang_org/x/net/lex/httplex",
   404  		"golang_org/x/net/proxy",
   405  		"golang_org/x/text/unicode/norm",
   406  		"golang_org/x/text/width",
   407  		"internal/nettrace",
   408  		"mime/multipart",
   409  		"net/http/httptrace",
   410  		"net/http/internal",
   411  		"runtime/debug",
   412  	},
   413  	"net/http/internal":  {"L4"},
   414  	"net/http/httptrace": {"context", "crypto/tls", "internal/nettrace", "net", "reflect", "time"},
   415  
   416  	// HTTP-using packages.
   417  	"expvar":             {"L4", "OS", "encoding/json", "net/http"},
   418  	"net/http/cgi":       {"L4", "NET", "OS", "crypto/tls", "net/http", "regexp"},
   419  	"net/http/cookiejar": {"L4", "NET", "net/http"},
   420  	"net/http/fcgi":      {"L4", "NET", "OS", "context", "net/http", "net/http/cgi"},
   421  	"net/http/httptest":  {"L4", "NET", "OS", "crypto/tls", "flag", "net/http", "net/http/internal", "crypto/x509"},
   422  	"net/http/httputil":  {"L4", "NET", "OS", "context", "net/http", "net/http/internal"},
   423  	"net/http/pprof":     {"L4", "OS", "html/template", "net/http", "runtime/pprof", "runtime/trace"},
   424  	"net/rpc":            {"L4", "NET", "encoding/gob", "html/template", "net/http"},
   425  	"net/rpc/jsonrpc":    {"L4", "NET", "encoding/json", "net/rpc"},
   426  }
   427  
   428  // isMacro reports whether p is a package dependency macro
   429  // (uppercase name).
   430  func isMacro(p string) bool {
   431  	return 'A' <= p[0] && p[0] <= 'Z'
   432  }
   433  
   434  func allowed(pkg string) map[string]bool {
   435  	m := map[string]bool{}
   436  	var allow func(string)
   437  	allow = func(p string) {
   438  		if m[p] {
   439  			return
   440  		}
   441  		m[p] = true // set even for macros, to avoid loop on cycle
   442  
   443  		// Upper-case names are macro-expanded.
   444  		if isMacro(p) {
   445  			for _, pp := range pkgDeps[p] {
   446  				allow(pp)
   447  			}
   448  		}
   449  	}
   450  	for _, pp := range pkgDeps[pkg] {
   451  		allow(pp)
   452  	}
   453  	return m
   454  }
   455  
   456  // listStdPkgs returns the same list of packages as "go list std".
   457  func listStdPkgs(goroot string) ([]string, error) {
   458  	// Based on cmd/go's matchPackages function.
   459  	var pkgs []string
   460  
   461  	src := filepath.Join(goroot, "src") + string(filepath.Separator)
   462  	walkFn := func(path string, fi os.FileInfo, err error) error {
   463  		if err != nil || !fi.IsDir() || path == src {
   464  			return nil
   465  		}
   466  
   467  		base := filepath.Base(path)
   468  		if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
   469  			return filepath.SkipDir
   470  		}
   471  
   472  		name := filepath.ToSlash(path[len(src):])
   473  		if name == "builtin" || name == "cmd" || strings.Contains(name, "golang_org") {
   474  			return filepath.SkipDir
   475  		}
   476  
   477  		pkgs = append(pkgs, name)
   478  		return nil
   479  	}
   480  	if err := filepath.Walk(src, walkFn); err != nil {
   481  		return nil, err
   482  	}
   483  	return pkgs, nil
   484  }
   485  
   486  func TestDependencies(t *testing.T) {
   487  	iOS := runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64")
   488  	if runtime.GOOS == "nacl" || iOS {
   489  		// Tests run in a limited file system and we do not
   490  		// provide access to every source file.
   491  		t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
   492  	}
   493  
   494  	ctxt := Default
   495  	all, err := listStdPkgs(ctxt.GOROOT)
   496  	if err != nil {
   497  		t.Fatal(err)
   498  	}
   499  	sort.Strings(all)
   500  
   501  	for _, pkg := range all {
   502  		imports, err := findImports(pkg)
   503  		if err != nil {
   504  			t.Error(err)
   505  			continue
   506  		}
   507  		ok := allowed(pkg)
   508  		var bad []string
   509  		for _, imp := range imports {
   510  			if !ok[imp] {
   511  				bad = append(bad, imp)
   512  			}
   513  		}
   514  		if bad != nil {
   515  			t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
   516  		}
   517  	}
   518  }
   519  
   520  var buildIgnore = []byte("\n// +build ignore")
   521  
   522  func findImports(pkg string) ([]string, error) {
   523  	dir := filepath.Join(Default.GOROOT, "src", pkg)
   524  	files, err := ioutil.ReadDir(dir)
   525  	if err != nil {
   526  		return nil, err
   527  	}
   528  	var imports []string
   529  	var haveImport = map[string]bool{}
   530  	for _, file := range files {
   531  		name := file.Name()
   532  		if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
   533  			continue
   534  		}
   535  		f, err := os.Open(filepath.Join(dir, name))
   536  		if err != nil {
   537  			return nil, err
   538  		}
   539  		var imp []string
   540  		data, err := readImports(f, false, &imp)
   541  		f.Close()
   542  		if err != nil {
   543  			return nil, fmt.Errorf("reading %v: %v", name, err)
   544  		}
   545  		if bytes.Contains(data, buildIgnore) {
   546  			continue
   547  		}
   548  		for _, quoted := range imp {
   549  			path, err := strconv.Unquote(quoted)
   550  			if err != nil {
   551  				continue
   552  			}
   553  			if !haveImport[path] {
   554  				haveImport[path] = true
   555  				imports = append(imports, path)
   556  			}
   557  		}
   558  	}
   559  	sort.Strings(imports)
   560  	return imports, nil
   561  }
   562  

View as plain text