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.
5 // This file exercises the import parser but also checks that
6 // some low-level packages do not have new dependencies added.
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.
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.
33 // DO NOT CHANGE THIS DATA TO FIX BUILDS.
35 var pkgDeps = map[string][]string{
36 // L0 is the lowest level, core, nearly unavoidable packages.
37 "errors": {"runtime", "internal/reflectlite"},
38 "io": {"errors", "sync", "sync/atomic"},
39 "runtime": {"unsafe", "runtime/internal/atomic", "runtime/internal/sys", "runtime/internal/math", "internal/cpu", "internal/bytealg"},
40 "runtime/internal/sys": {},
41 "runtime/internal/atomic": {"unsafe", "internal/cpu"},
42 "runtime/internal/math": {"runtime/internal/sys"},
43 "internal/race": {"runtime", "unsafe"},
44 "sync": {"internal/race", "runtime", "sync/atomic", "unsafe"},
45 "sync/atomic": {"unsafe"},
48 "internal/bytealg": {"unsafe", "internal/cpu"},
49 "internal/reflectlite": {"runtime", "unsafe"},
55 "runtime/internal/atomic",
61 "internal/reflectlite",
64 // L1 adds simple functions and strings processing,
65 // but not Unicode tables.
66 "math": {"internal/cpu", "unsafe", "math/bits"},
67 "math/bits": {"unsafe"},
68 "math/cmplx": {"math", "math/bits"},
69 "math/rand": {"L0", "math"},
70 "strconv": {"L0", "unicode/utf8", "math", "math/bits"},
86 // L2 adds Unicode and strings processing.
87 "bufio": {"L0", "unicode/utf8", "bytes"},
88 "bytes": {"L0", "unicode", "unicode/utf8"},
89 "path": {"L0", "unicode/utf8", "strings"},
90 "strings": {"L0", "unicode", "unicode/utf8"},
102 // L3 adds reflection and some basic utility packages
103 // and interface definitions, but nothing that makes
105 "crypto": {"L2", "hash"}, // interfaces
106 "crypto/cipher": {"L2", "crypto/subtle", "crypto/internal/subtle", "encoding/binary"},
107 "crypto/internal/subtle": {"unsafe", "reflect"}, // reflect behind a appengine tag
109 "encoding/base32": {"L2"},
110 "encoding/base64": {"L2", "encoding/binary"},
111 "encoding/binary": {"L2", "reflect"},
112 "hash": {"L2"}, // interfaces
113 "hash/adler32": {"L2", "hash"},
114 "hash/crc32": {"L2", "hash"},
115 "hash/crc64": {"L2", "hash"},
116 "hash/fnv": {"L2", "hash"},
117 "hash/maphash": {"L2", "hash"},
118 "image": {"L2", "image/color"}, // interfaces
119 "image/color": {"L2"}, // interfaces
120 "image/color/palette": {"L2", "image/color"},
121 "internal/fmtsort": {"reflect", "sort"},
123 "sort": {"internal/reflectlite"},
125 "crypto/internal/boring": {"L2", "C", "crypto", "crypto/cipher", "crypto/internal/boring/sig", "crypto/subtle", "encoding/asn1", "hash", "math/big"},
126 "crypto/internal/boring/fipstls": {"sync/atomic"},
127 "crypto/internal/cipherhw": {"crypto/internal/boring"},
128 "crypto/tls/fipsonly": {"crypto/internal/boring/fipstls", "crypto/internal/boring/sig"},
134 "crypto/internal/boring",
135 "crypto/internal/boring/fipstls",
136 "crypto/internal/subtle",
148 "image/color/palette",
154 // End of linear dependency definitions.
156 // Operating system access.
157 "syscall": {"L0", "internal/oserror", "internal/race", "internal/syscall/windows/sysdll", "syscall/js", "unicode/utf16"},
158 "syscall/js": {"L0"},
159 "internal/oserror": {"L0"},
160 "internal/syscall/unix": {"L0", "syscall"},
161 "internal/syscall/windows": {"L0", "syscall", "internal/syscall/windows/sysdll", "unicode/utf16"},
162 "internal/syscall/windows/registry": {"L0", "syscall", "internal/syscall/windows/sysdll", "unicode/utf16"},
163 "internal/syscall/execenv": {"L0", "syscall", "internal/syscall/windows", "unicode/utf16"},
165 // "L0" without the "io" package:
168 "runtime/internal/atomic",
172 // Other time dependencies:
173 "internal/syscall/windows/registry",
178 "internal/cfg": {"L0"},
179 "internal/poll": {"L0", "internal/oserror", "internal/race", "syscall", "time", "unicode/utf16", "unicode/utf8", "internal/syscall/windows", "internal/syscall/unix"},
180 "internal/testlog": {"L0"},
181 "os": {"L1", "os", "syscall", "time", "internal/oserror", "internal/poll", "internal/syscall/windows", "internal/syscall/unix", "internal/syscall/execenv", "internal/testlog"},
182 "path/filepath": {"L2", "os", "syscall", "internal/syscall/windows"},
183 "io/ioutil": {"L2", "os", "path/filepath", "time"},
184 "os/exec": {"L2", "os", "context", "path/filepath", "syscall", "internal/syscall/execenv"},
185 "os/signal": {"L2", "os", "syscall"},
187 // OS enables basic operating system functionality,
188 // but not direct use of package syscall, nor os/signal.
197 // Formatted I/O: few dependencies (L1) but we must add reflect and internal/fmtsort.
198 "fmt": {"L1", "os", "reflect", "internal/fmtsort"},
199 "log": {"L1", "os", "fmt", "time"},
201 // Packages used by testing must be low-level (L2+fmt).
202 "regexp": {"L2", "regexp/syntax"},
203 "regexp/syntax": {"L2"},
204 "runtime/debug": {"L2", "fmt", "io/ioutil", "os", "time"},
205 "runtime/pprof": {"L2", "compress/gzip", "context", "encoding/binary", "fmt", "io/ioutil", "os", "syscall", "text/tabwriter", "time"},
206 "runtime/trace": {"L0", "context", "fmt"},
207 "text/tabwriter": {"L2"},
209 "testing": {"L2", "flag", "fmt", "internal/race", "os", "runtime/debug", "runtime/pprof", "runtime/trace", "time"},
210 "testing/iotest": {"L2", "log"},
211 "testing/quick": {"L2", "flag", "fmt", "reflect", "time"},
212 "internal/obscuretestdata": {"L2", "OS", "encoding/base64"},
213 "internal/testenv": {"L2", "OS", "flag", "testing", "syscall", "internal/cfg"},
214 "internal/lazyregexp": {"L2", "OS", "regexp"},
215 "internal/lazytemplate": {"L2", "OS", "text/template"},
217 // L4 is defined as L3+fmt+log+time, because in general once
218 // you're using L3 packages, use of fmt, log, or time is not a big deal.
227 "go/ast": {"L4", "OS", "go/scanner", "go/token"},
228 "go/doc": {"L4", "OS", "go/ast", "go/token", "regexp", "internal/lazyregexp", "text/template"},
229 "go/parser": {"L4", "OS", "go/ast", "go/scanner", "go/token"},
230 "go/printer": {"L4", "OS", "go/ast", "go/scanner", "go/token", "text/tabwriter"},
231 "go/scanner": {"L4", "OS", "go/token"},
243 "go/format": {"L4", "GOPARSER", "internal/format"},
244 "internal/format": {"L4", "GOPARSER"},
247 "go/constant": {"L4", "go/token", "math/big"},
248 "go/importer": {"L4", "go/build", "go/internal/gccgoimporter", "go/internal/gcimporter", "go/internal/srcimporter", "go/token", "go/types"},
249 "go/internal/gcimporter": {"L4", "OS", "go/build", "go/constant", "go/token", "go/types", "text/scanner"},
250 "go/internal/gccgoimporter": {"L4", "OS", "debug/elf", "go/constant", "go/token", "go/types", "internal/xcoff", "text/scanner"},
251 "go/internal/srcimporter": {"L4", "OS", "fmt", "go/ast", "go/build", "go/parser", "go/token", "go/types", "path/filepath"},
252 "go/types": {"L4", "GOPARSER", "container/heap", "go/constant"},
255 "archive/tar": {"L4", "OS", "syscall", "os/user"},
256 "archive/zip": {"L4", "OS", "compress/flate"},
257 "container/heap": {"sort"},
258 "compress/bzip2": {"L4"},
259 "compress/flate": {"L4"},
260 "compress/gzip": {"L4", "compress/flate"},
261 "compress/lzw": {"L4"},
262 "compress/zlib": {"L4", "compress/flate"},
263 "context": {"errors", "internal/reflectlite", "sync", "sync/atomic", "time"},
264 "database/sql": {"L4", "container/list", "context", "database/sql/driver", "database/sql/internal"},
265 "database/sql/driver": {"L4", "context", "time", "database/sql/internal"},
266 "debug/dwarf": {"L4"},
267 "debug/elf": {"L4", "OS", "debug/dwarf", "compress/zlib"},
268 "debug/gosym": {"L4"},
269 "debug/macho": {"L4", "OS", "debug/dwarf", "compress/zlib"},
270 "debug/pe": {"L4", "OS", "debug/dwarf", "compress/zlib"},
271 "debug/plan9obj": {"L4", "OS"},
273 "encoding/ascii85": {"L4"},
274 "encoding/asn1": {"L4", "math/big"},
275 "encoding/csv": {"L4"},
276 "encoding/gob": {"L4", "OS", "encoding"},
277 "encoding/hex": {"L4"},
278 "encoding/json": {"L4", "encoding"},
279 "encoding/pem": {"L4"},
280 "encoding/xml": {"L4", "encoding"},
281 "flag": {"L4", "OS"},
282 "go/build": {"L4", "OS", "GOPARSER", "internal/goroot", "internal/goversion"},
284 "image/draw": {"L4", "image/internal/imageutil"},
285 "image/gif": {"L4", "compress/lzw", "image/color/palette", "image/draw"},
286 "image/internal/imageutil": {"L4"},
287 "image/jpeg": {"L4", "image/internal/imageutil"},
288 "image/png": {"L4", "compress/zlib"},
289 "index/suffixarray": {"L4", "regexp"},
290 "internal/goroot": {"L4", "OS"},
291 "internal/singleflight": {"sync"},
292 "internal/trace": {"L4", "OS", "container/heap"},
293 "internal/xcoff": {"L4", "OS", "debug/dwarf"},
295 "mime": {"L4", "OS", "syscall", "internal/syscall/windows/registry"},
296 "mime/quotedprintable": {"L4"},
297 "net/internal/socktest": {"L4", "OS", "syscall", "internal/syscall/windows"},
299 "plugin": {"L0", "OS", "CGO"},
300 "runtime/pprof/internal/profile": {"L4", "OS", "compress/gzip", "regexp"},
301 "testing/internal/testdeps": {"L4", "internal/testlog", "runtime/pprof", "regexp"},
302 "text/scanner": {"L4", "OS"},
303 "text/template/parse": {"L4"},
306 "L4", "OS", "encoding/json", "html", "text/template",
307 "text/template/parse",
310 "L4", "OS", "net/url", "text/template/parse",
314 // If you add a dependency on CGO, you must add the package to
315 // cgoPackages in cmd/dist/test.go.
316 "runtime/cgo": {"L0", "C"},
317 "CGO": {"C", "runtime/cgo"},
319 // Fake entry to satisfy the pseudo-import "C"
320 // that shows up in programs that use cgo.
323 // Race detector/MSan uses cgo.
324 "runtime/race": {"C"},
325 "runtime/msan": {"C"},
327 // Plan 9 alone needs io/ioutil and os.
328 "os/user": {"L4", "CGO", "io/ioutil", "os", "syscall", "internal/syscall/windows", "internal/syscall/windows/registry"},
330 // Internal package used only for testing.
331 "os/signal/internal/pty": {"CGO", "fmt", "os", "syscall"},
334 // Because net must be used by any package that wants to
335 // do networking portably, it must have a small dependency set: just L0+basic os.
338 "context", "math/rand", "os", "sort", "syscall", "time",
339 "internal/nettrace", "internal/poll", "internal/syscall/unix",
340 "internal/syscall/windows", "internal/singleflight", "internal/race",
341 "golang.org/x/net/dns/dnsmessage", "golang.org/x/net/lif", "golang.org/x/net/route",
344 // NET enables use of basic network-related packages.
352 // Uses of networking.
353 "log/syslog": {"L4", "OS", "net"},
354 "net/mail": {"L4", "NET", "OS", "mime"},
355 "net/textproto": {"L4", "OS", "net"},
358 "crypto/aes": {"L3"},
359 "crypto/des": {"L3"},
360 "crypto/hmac": {"L3"},
361 "crypto/internal/randutil": {"io", "sync"},
362 "crypto/md5": {"L3"},
363 "crypto/rc4": {"L3"},
364 "crypto/sha1": {"L3"},
365 "crypto/sha256": {"L3"},
366 "crypto/sha512": {"L3"},
372 "crypto/internal/randutil",
378 "golang.org/x/crypto/chacha20poly1305",
379 "golang.org/x/crypto/curve25519",
380 "golang.org/x/crypto/poly1305",
383 // Random byte, number generation.
384 // This would be part of core crypto except that it imports
385 // math/big, which imports fmt.
386 "crypto/rand": {"L4", "CRYPTO", "OS", "math/big", "syscall", "syscall/js", "internal/syscall/unix"},
388 // Not part of CRYPTO because it imports crypto/rand and crypto/sha512.
389 "crypto/ed25519": {"L3", "CRYPTO", "crypto/rand", "crypto/ed25519/internal/edwards25519"},
390 "crypto/ed25519/internal/edwards25519": {"encoding/binary"},
392 // Mathematical crypto: dependencies on fmt (L4) and math/big.
393 // We could avoid some of the fmt, but math/big imports fmt anyway.
394 "crypto/dsa": {"L4", "CRYPTO", "math/big"},
396 "L4", "CRYPTO", "crypto/elliptic", "math/big",
397 "golang.org/x/crypto/cryptobyte", "golang.org/x/crypto/cryptobyte/asn1",
399 "crypto/elliptic": {"L4", "CRYPTO", "math/big"},
400 "crypto/rsa": {"L4", "CRYPTO", "crypto/rand", "math/big"},
415 "L4", "CRYPTO-MATH", "OS", "golang.org/x/crypto/cryptobyte", "golang.org/x/crypto/hkdf",
416 "container/list", "crypto/x509", "encoding/pem", "net", "syscall", "crypto/ed25519",
419 "L4", "CRYPTO-MATH", "OS", "CGO", "crypto/ed25519",
420 "crypto/x509/pkix", "encoding/pem", "encoding/hex", "net", "os/user", "syscall", "net/url",
421 "golang.org/x/crypto/cryptobyte", "golang.org/x/crypto/cryptobyte/asn1",
423 "crypto/x509/pkix": {"L4", "CRYPTO-MATH", "encoding/hex"},
425 // Simple net+crypto-aware packages.
426 "mime/multipart": {"L4", "OS", "mime", "crypto/rand", "net/textproto", "mime/quotedprintable"},
427 "net/smtp": {"L4", "CRYPTO", "NET", "crypto/tls"},
429 // HTTP, kingpin of dependencies.
437 "golang.org/x/net/http/httpguts",
438 "golang.org/x/net/http/httpproxy",
439 "golang.org/x/net/http2/hpack",
440 "golang.org/x/net/idna",
441 "golang.org/x/text/unicode/norm",
442 "golang.org/x/text/width",
445 "net/http/httptrace",
450 "net/http/internal": {"L4"},
451 "net/http/httptrace": {"context", "crypto/tls", "internal/nettrace", "net", "net/textproto", "reflect", "time"},
453 // HTTP-using packages.
454 "expvar": {"L4", "OS", "encoding/json", "net/http"},
455 "net/http/cgi": {"L4", "NET", "OS", "crypto/tls", "net/http", "regexp"},
456 "net/http/cookiejar": {"L4", "NET", "net/http"},
457 "net/http/fcgi": {"L4", "NET", "OS", "context", "net/http", "net/http/cgi"},
458 "net/http/httptest": {
459 "L4", "NET", "OS", "crypto/tls", "flag", "net/http", "net/http/internal", "crypto/x509",
460 "golang.org/x/net/http/httpguts",
462 "net/http/httputil": {"L4", "NET", "OS", "context", "net/http", "net/http/internal", "golang.org/x/net/http/httpguts"},
463 "net/http/pprof": {"L4", "OS", "html/template", "net/http", "runtime/pprof", "runtime/trace"},
464 "net/rpc": {"L4", "NET", "encoding/gob", "html/template", "net/http", "go/token"},
465 "net/rpc/jsonrpc": {"L4", "NET", "encoding/json", "net/rpc"},
468 // isMacro reports whether p is a package dependency macro
470 func isMacro(p string) bool {
471 return 'A' <= p[0] && p[0] <= 'Z'
474 func allowed(pkg string) map[string]bool {
475 m := map[string]bool{}
476 var allow func(string)
477 allow = func(p string) {
481 m[p] = true // set even for macros, to avoid loop on cycle
483 // Upper-case names are macro-expanded.
485 for _, pp := range pkgDeps[p] {
490 for _, pp := range pkgDeps[pkg] {
496 // listStdPkgs returns the same list of packages as "go list std".
497 func listStdPkgs(goroot string) ([]string, error) {
498 // Based on cmd/go's matchPackages function.
501 src := filepath.Join(goroot, "src") + string(filepath.Separator)
502 walkFn := func(path string, fi os.FileInfo, err error) error {
503 if err != nil || !fi.IsDir() || path == src {
507 base := filepath.Base(path)
508 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
509 return filepath.SkipDir
512 name := filepath.ToSlash(path[len(src):])
513 if name == "builtin" || name == "cmd" || strings.Contains(name, "golang.org/x/") {
514 return filepath.SkipDir
517 pkgs = append(pkgs, name)
520 if err := filepath.Walk(src, walkFn); err != nil {
526 func TestDependencies(t *testing.T) {
527 iOS := runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64")
529 // Tests run in a limited file system and we do not
530 // provide access to every source file.
531 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
535 all, err := listStdPkgs(ctxt.GOROOT)
541 sawImport := map[string]map[string]bool{} // from package => to package => true
543 for _, pkg := range all {
544 imports, err := findImports(pkg)
549 if sawImport[pkg] == nil {
550 sawImport[pkg] = map[string]bool{}
554 for _, imp := range imports {
555 sawImport[pkg][imp] = true
557 bad = append(bad, imp)
561 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
565 // depPath returns the path between the given from and to packages.
566 // It returns the empty string if there's no dependency path.
567 var depPath func(string, string) string
568 depPath = func(from, to string) string {
569 if sawImport[from][to] {
570 return from + " => " + to
572 for pkg := range sawImport[from] {
573 if p := depPath(pkg, to); p != "" {
574 return from + " => " + p
580 // Also test some high-level policy goals are being met by not finding
581 // these dependency paths:
582 badPaths := []struct{ from, to string }{
587 for _, path := range badPaths {
588 if how := depPath(path.from, path.to); how != "" {
589 t.Errorf("policy violation: %s", how)
595 var buildIgnore = []byte("\n// +build ignore")
597 func findImports(pkg string) ([]string, error) {
598 dir := filepath.Join(Default.GOROOT, "src", pkg)
599 files, err := ioutil.ReadDir(dir)
604 var haveImport = map[string]bool{}
605 for _, file := range files {
607 if name == "slice_go14.go" || name == "slice_go18.go" {
608 // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
611 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
614 f, err := os.Open(filepath.Join(dir, name))
619 data, err := readImports(f, false, &imp)
622 return nil, fmt.Errorf("reading %v: %v", name, err)
624 if bytes.Contains(data, buildIgnore) {
627 for _, quoted := range imp {
628 path, err := strconv.Unquote(quoted)
632 if !haveImport[path] {
633 haveImport[path] = true
634 imports = append(imports, path)
638 sort.Strings(imports)