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