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.
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"},
46 "internal/cpu": {"runtime"},
52 "runtime/internal/atomic",
59 // L1 adds simple functions and strings processing,
60 // but not Unicode tables.
61 "math": {"internal/cpu", "unsafe"},
63 "math/cmplx": {"math"},
64 "math/rand": {"L0", "math"},
65 "strconv": {"L0", "unicode/utf8", "math"},
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"},
97 // L3 adds reflection and some basic utility packages
98 // and interface definitions, but nothing that makes
100 "crypto": {"L2", "hash"}, // interfaces
101 "crypto/cipher": {"L2", "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"},
117 "crypto/internal/boring": {"L2", "C", "crypto", "crypto/cipher", "crypto/internal/boring/sig", "crypto/subtle", "encoding/asn1", "hash", "math/big"},
118 "crypto/internal/boring/fipstls": {"sync/atomic"},
119 "crypto/internal/cipherhw": {"crypto/internal/boring"},
120 "crypto/tls/fipsonly": {"crypto/internal/boring/fipstls", "crypto/internal/boring/sig"},
126 "crypto/internal/boring",
127 "crypto/internal/boring/fipstls",
128 "crypto/internal/cipherhw",
140 "image/color/palette",
144 // End of linear dependency definitions.
146 // Operating system access.
147 "syscall": {"L0", "internal/race", "internal/syscall/windows/sysdll", "unicode/utf16"},
148 "internal/syscall/unix": {"L0", "syscall"},
149 "internal/syscall/windows": {"L0", "syscall", "internal/syscall/windows/sysdll"},
150 "internal/syscall/windows/registry": {"L0", "syscall", "internal/syscall/windows/sysdll", "unicode/utf16"},
152 // "L0" without the "io" package:
155 "runtime/internal/atomic",
159 // Other time dependencies:
160 "internal/syscall/windows/registry",
164 "internal/poll": {"L0", "internal/race", "syscall", "time", "unicode/utf16", "unicode/utf8", "internal/syscall/windows"},
165 "os": {"L1", "os", "syscall", "time", "internal/poll", "internal/syscall/windows"},
166 "path/filepath": {"L2", "os", "syscall", "internal/syscall/windows"},
167 "io/ioutil": {"L2", "os", "path/filepath", "time"},
168 "os/exec": {"L2", "os", "context", "path/filepath", "syscall"},
169 "os/signal": {"L2", "os", "syscall"},
171 // OS enables basic operating system functionality,
172 // but not direct use of package syscall, nor os/signal.
181 // Formatted I/O: few dependencies (L1) but we must add reflect.
182 "fmt": {"L1", "os", "reflect"},
183 "log": {"L1", "os", "fmt", "time"},
185 // Packages used by testing must be low-level (L2+fmt).
186 "regexp": {"L2", "regexp/syntax"},
187 "regexp/syntax": {"L2"},
188 "runtime/debug": {"L2", "fmt", "io/ioutil", "os", "time"},
189 "runtime/pprof": {"L2", "compress/gzip", "context", "encoding/binary", "fmt", "io/ioutil", "os", "text/tabwriter", "time"},
190 "runtime/trace": {"L0"},
191 "text/tabwriter": {"L2"},
193 "testing": {"L2", "flag", "fmt", "internal/race", "os", "runtime/debug", "runtime/pprof", "runtime/trace", "time"},
194 "testing/iotest": {"L2", "log"},
195 "testing/quick": {"L2", "flag", "fmt", "reflect", "time"},
196 "internal/testenv": {"L2", "OS", "flag", "testing", "syscall"},
198 // L4 is defined as L3+fmt+log+time, because in general once
199 // you're using L3 packages, use of fmt, log, or time is not a big deal.
208 "go/ast": {"L4", "OS", "go/scanner", "go/token"},
209 "go/doc": {"L4", "go/ast", "go/token", "regexp", "text/template"},
210 "go/parser": {"L4", "OS", "go/ast", "go/scanner", "go/token"},
211 "go/printer": {"L4", "OS", "go/ast", "go/scanner", "go/token", "text/tabwriter"},
212 "go/scanner": {"L4", "OS", "go/token"},
224 "go/format": {"L4", "GOPARSER", "internal/format"},
225 "internal/format": {"L4", "GOPARSER"},
228 "go/constant": {"L4", "go/token", "math/big"},
229 "go/importer": {"L4", "go/build", "go/internal/gccgoimporter", "go/internal/gcimporter", "go/internal/srcimporter", "go/token", "go/types"},
230 "go/internal/gcimporter": {"L4", "OS", "go/build", "go/constant", "go/token", "go/types", "text/scanner"},
231 "go/internal/gccgoimporter": {"L4", "OS", "debug/elf", "go/constant", "go/token", "go/types", "text/scanner"},
232 "go/internal/srcimporter": {"L4", "fmt", "go/ast", "go/build", "go/parser", "go/token", "go/types", "path/filepath"},
233 "go/types": {"L4", "GOPARSER", "container/heap", "go/constant"},
236 "archive/tar": {"L4", "OS", "syscall", "os/user"},
237 "archive/zip": {"L4", "OS", "compress/flate"},
238 "container/heap": {"sort"},
239 "compress/bzip2": {"L4"},
240 "compress/flate": {"L4"},
241 "compress/gzip": {"L4", "compress/flate"},
242 "compress/lzw": {"L4"},
243 "compress/zlib": {"L4", "compress/flate"},
244 "context": {"errors", "fmt", "reflect", "sync", "time"},
245 "database/sql": {"L4", "container/list", "context", "database/sql/driver", "database/sql/internal"},
246 "database/sql/driver": {"L4", "context", "time", "database/sql/internal"},
247 "debug/dwarf": {"L4"},
248 "debug/elf": {"L4", "OS", "debug/dwarf", "compress/zlib"},
249 "debug/gosym": {"L4"},
250 "debug/macho": {"L4", "OS", "debug/dwarf"},
251 "debug/pe": {"L4", "OS", "debug/dwarf"},
252 "debug/plan9obj": {"L4", "OS"},
254 "encoding/ascii85": {"L4"},
255 "encoding/asn1": {"L4", "math/big"},
256 "encoding/csv": {"L4"},
257 "encoding/gob": {"L4", "OS", "encoding"},
258 "encoding/hex": {"L4"},
259 "encoding/json": {"L4", "encoding"},
260 "encoding/pem": {"L4"},
261 "encoding/xml": {"L4", "encoding"},
262 "flag": {"L4", "OS"},
263 "go/build": {"L4", "OS", "GOPARSER"},
265 "image/draw": {"L4", "image/internal/imageutil"},
266 "image/gif": {"L4", "compress/lzw", "image/color/palette", "image/draw"},
267 "image/internal/imageutil": {"L4"},
268 "image/jpeg": {"L4", "image/internal/imageutil"},
269 "image/png": {"L4", "compress/zlib"},
270 "index/suffixarray": {"L4", "regexp"},
271 "internal/singleflight": {"sync"},
272 "internal/trace": {"L4", "OS"},
274 "mime": {"L4", "OS", "syscall", "internal/syscall/windows/registry"},
275 "mime/quotedprintable": {"L4"},
276 "net/internal/socktest": {"L4", "OS", "syscall", "internal/syscall/windows"},
278 "plugin": {"L0", "OS", "CGO"},
279 "runtime/pprof/internal/profile": {"L4", "OS", "compress/gzip", "regexp"},
280 "testing/internal/testdeps": {"L4", "runtime/pprof", "regexp"},
281 "text/scanner": {"L4", "OS"},
282 "text/template/parse": {"L4"},
285 "L4", "OS", "encoding/json", "html", "text/template",
286 "text/template/parse",
289 "L4", "OS", "net/url", "text/template/parse",
293 // If you add a dependency on CGO, you must add the package to
294 // cgoPackages in cmd/dist/test.go.
295 "runtime/cgo": {"L0", "C"},
296 "CGO": {"C", "runtime/cgo"},
298 // Fake entry to satisfy the pseudo-import "C"
299 // that shows up in programs that use cgo.
302 // Race detector/MSan uses cgo.
303 "runtime/race": {"C"},
304 "runtime/msan": {"C"},
306 // Plan 9 alone needs io/ioutil and os.
307 "os/user": {"L4", "CGO", "io/ioutil", "os", "syscall"},
309 // Internal package used only for testing.
310 "os/signal/internal/pty": {"CGO", "fmt", "os"},
313 // Because net must be used by any package that wants to
314 // do networking portably, it must have a small dependency set: just L0+basic os.
317 "context", "math/rand", "os", "reflect", "sort", "syscall", "time",
318 "internal/nettrace", "internal/poll",
319 "internal/syscall/windows", "internal/singleflight", "internal/race",
320 "golang_org/x/net/lif", "golang_org/x/net/route",
323 // NET enables use of basic network-related packages.
331 // Uses of networking.
332 "log/syslog": {"L4", "OS", "net"},
333 "net/mail": {"L4", "NET", "OS", "mime"},
334 "net/textproto": {"L4", "OS", "net"},
337 "crypto/aes": {"L3"},
338 "crypto/des": {"L3"},
339 "crypto/hmac": {"L3"},
340 "crypto/md5": {"L3"},
341 "crypto/rc4": {"L3"},
342 "crypto/sha1": {"L3"},
343 "crypto/sha256": {"L3"},
344 "crypto/sha512": {"L3"},
355 "golang_org/x/crypto/chacha20poly1305",
356 "golang_org/x/crypto/curve25519",
357 "golang_org/x/crypto/poly1305",
360 // Random byte, number generation.
361 // This would be part of core crypto except that it imports
362 // math/big, which imports fmt.
363 "crypto/rand": {"L4", "CRYPTO", "OS", "math/big", "syscall", "internal/syscall/unix"},
365 // Mathematical crypto: dependencies on fmt (L4) and math/big.
366 // We could avoid some of the fmt, but math/big imports fmt anyway.
367 "crypto/dsa": {"L4", "CRYPTO", "math/big"},
368 "crypto/ecdsa": {"L4", "CRYPTO", "crypto/elliptic", "math/big", "encoding/asn1"},
369 "crypto/elliptic": {"L4", "CRYPTO", "math/big"},
370 "crypto/rsa": {"L4", "CRYPTO", "crypto/rand", "math/big"},
385 "L4", "CRYPTO-MATH", "OS",
386 "container/list", "crypto/x509", "encoding/pem", "net", "syscall",
389 "L4", "CRYPTO-MATH", "OS", "CGO",
390 "crypto/x509/pkix", "encoding/pem", "encoding/hex", "net", "os/user", "syscall", "net/url",
391 "golang_org/x/crypto/cryptobyte", "golang_org/x/crypto/cryptobyte/asn1",
393 "crypto/x509/pkix": {"L4", "CRYPTO-MATH", "encoding/hex"},
395 // Simple net+crypto-aware packages.
396 "mime/multipart": {"L4", "OS", "mime", "crypto/rand", "net/textproto", "mime/quotedprintable"},
397 "net/smtp": {"L4", "CRYPTO", "NET", "crypto/tls"},
399 // HTTP, kingpin of dependencies.
407 "golang_org/x/net/http2/hpack",
408 "golang_org/x/net/idna",
409 "golang_org/x/net/lex/httplex",
410 "golang_org/x/net/proxy",
411 "golang_org/x/text/unicode/norm",
412 "golang_org/x/text/width",
415 "net/http/httptrace",
419 "net/http/internal": {"L4"},
420 "net/http/httptrace": {"context", "crypto/tls", "internal/nettrace", "net", "reflect", "time"},
422 // HTTP-using packages.
423 "expvar": {"L4", "OS", "encoding/json", "net/http"},
424 "net/http/cgi": {"L4", "NET", "OS", "crypto/tls", "net/http", "regexp"},
425 "net/http/cookiejar": {"L4", "NET", "net/http"},
426 "net/http/fcgi": {"L4", "NET", "OS", "context", "net/http", "net/http/cgi"},
427 "net/http/httptest": {"L4", "NET", "OS", "crypto/tls", "flag", "net/http", "net/http/internal", "crypto/x509"},
428 "net/http/httputil": {"L4", "NET", "OS", "context", "net/http", "net/http/internal"},
429 "net/http/pprof": {"L4", "OS", "html/template", "net/http", "runtime/pprof", "runtime/trace"},
430 "net/rpc": {"L4", "NET", "encoding/gob", "html/template", "net/http"},
431 "net/rpc/jsonrpc": {"L4", "NET", "encoding/json", "net/rpc"},
434 // isMacro reports whether p is a package dependency macro
436 func isMacro(p string) bool {
437 return 'A' <= p[0] && p[0] <= 'Z'
440 func allowed(pkg string) map[string]bool {
441 m := map[string]bool{}
442 var allow func(string)
443 allow = func(p string) {
447 m[p] = true // set even for macros, to avoid loop on cycle
449 // Upper-case names are macro-expanded.
451 for _, pp := range pkgDeps[p] {
456 for _, pp := range pkgDeps[pkg] {
462 // listStdPkgs returns the same list of packages as "go list std".
463 func listStdPkgs(goroot string) ([]string, error) {
464 // Based on cmd/go's matchPackages function.
467 src := filepath.Join(goroot, "src") + string(filepath.Separator)
468 walkFn := func(path string, fi os.FileInfo, err error) error {
469 if err != nil || !fi.IsDir() || path == src {
473 base := filepath.Base(path)
474 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
475 return filepath.SkipDir
478 name := filepath.ToSlash(path[len(src):])
479 if name == "builtin" || name == "cmd" || strings.Contains(name, "golang_org") {
480 return filepath.SkipDir
483 pkgs = append(pkgs, name)
486 if err := filepath.Walk(src, walkFn); err != nil {
492 func TestDependencies(t *testing.T) {
493 iOS := runtime.GOOS == "darwin" && (runtime.GOARCH == "arm" || runtime.GOARCH == "arm64")
494 if runtime.GOOS == "nacl" || iOS {
495 // Tests run in a limited file system and we do not
496 // provide access to every source file.
497 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
501 all, err := listStdPkgs(ctxt.GOROOT)
507 for _, pkg := range all {
508 imports, err := findImports(pkg)
515 for _, imp := range imports {
517 bad = append(bad, imp)
521 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
526 var buildIgnore = []byte("\n// +build ignore")
528 func findImports(pkg string) ([]string, error) {
529 dir := filepath.Join(Default.GOROOT, "src", pkg)
530 files, err := ioutil.ReadDir(dir)
535 var haveImport = map[string]bool{}
536 for _, file := range files {
538 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
541 f, err := os.Open(filepath.Join(dir, name))
546 data, err := readImports(f, false, &imp)
549 return nil, fmt.Errorf("reading %v: %v", name, err)
551 if bytes.Contains(data, buildIgnore) {
554 for _, quoted := range imp {
555 path, err := strconv.Unquote(quoted)
559 if !haveImport[path] {
560 haveImport[path] = true
561 imports = append(imports, path)
565 sort.Strings(imports)