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.
25 // depsRules defines the expected dependencies between packages in
26 // the Go source tree. It is a statement of policy.
28 // DO NOT CHANGE THIS DATA TO FIX BUILDS.
29 // Existing packages should not have their constraints relaxed
30 // without prior discussion.
31 // Negative assertions should almost never be removed.
33 // "a < b" means package b can import package a.
35 // See `go doc internal/dag' for the full syntax.
37 // All-caps names are pseudo-names for specific points
38 // in the dependency lattice.
40 # No dependencies allowed for any of these packages.
42 < constraints, container/list, container/ring,
43 internal/cfg, internal/cpu, internal/coverage,
44 internal/coverage/uleb128, internal/goarch,
45 internal/goexperiment, internal/goos,
46 internal/goversion, internal/nettrace,
47 unicode/utf8, unicode/utf16, unicode,
50 # These packages depend only on internal/goarch and unsafe.
51 internal/goarch, unsafe
54 # RUNTIME is the core runtime group of packages, all of them very light-weight.
55 internal/abi, internal/cpu, internal/goarch,
56 internal/goexperiment, internal/goos, unsafe
59 < internal/unsafeheader
60 < runtime/internal/sys
61 < runtime/internal/syscall
62 < runtime/internal/atomic
63 < runtime/internal/math
68 < internal/reflectlite
70 < internal/oserror, math/bits
88 # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
89 RUNTIME, unicode/utf8, unicode/utf16
90 < internal/syscall/windows/sysdll, syscall/js
92 < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
93 < internal/syscall/execenv
96 # TIME is SYSCALL plus the core packages about time, including context.
106 # MATH is RUNTIME plus the basic math packages.
127 # STR is basic string and buffer manipulation.
128 RUNTIME, io, unicode/utf8, unicode/utf16, unicode
135 # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
136 # OS includes string routines, but those must be layered above package os.
137 # OS does not include reflection.
147 unicode, fmt !< net, os, os/signal;
153 os < internal/godebug;
155 path/filepath, internal/godebug < os/exec;
157 io/ioutil, os/exec, os/signal
163 < golang.org/x/sys/cpu;
165 # FMT is OS (which includes string routines) plus reflect and fmt.
166 # It does not include package log, which should be avoided in core packages.
179 # Misc packages needing only FMT.
184 mime/quotedprintable,
185 net/internal/socktest,
195 # core ones do not use fmt.
201 < encoding/base32, encoding/base64;
203 FMT, encoding < flag;
205 fmt !< encoding/base32, encoding/base64;
207 FMT, encoding/base32, encoding/base64, internal/saferio
208 < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
209 encoding/json, encoding/pem, encoding/xml, mime;
214 < hash/adler32, hash/crc32, hash/crc64, hash/fnv, hash/maphash;
217 FMT, encoding/binary, math/rand
221 FMT, encoding/binary, hash/adler32, hash/crc32
222 < compress/bzip2, compress/flate, compress/lzw
223 < archive/zip, compress/gzip, compress/zlib;
227 < text/template/parse;
229 net/url, text/template/parse
231 < internal/lazytemplate;
233 encoding/json, html, text/template
240 < internal/lazyregexp;
243 encoding/binary, regexp
247 FMT, encoding/binary, compress/zlib, internal/saferio
250 < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
254 # go parser and friends.
259 < go/internal/typeparams
263 < go/build/constraint, go/doc/comment;
265 go/build/constraint, go/doc/comment, go/parser, text/tabwriter
269 go/doc/comment, go/parser, internal/lazyregexp, text/template
275 container/heap, go/constant, go/parser, regexp
278 FMT, internal/goexperiment
281 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion
286 < database/sql/internal
287 < database/sql/driver
291 FMT, compress/lzw, compress/zlib
293 < image, image/color/palette
294 < image/internal/imageutil
296 < image/gif, image/jpeg, image/png;
298 # cgo, delayed as long as possible.
299 # If you add a dependency on CGO, you must add the package
300 # to cgoPackages in cmd/dist/test.go as well.
305 < runtime/msan, runtime/asan;
308 NONE < runtime/race/internal/amd64v1;
309 NONE < runtime/race/internal/amd64v3;
310 CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
312 # Bulk of the standard library must not use cgo.
313 # The prohibition stops at net and os/user.
314 C !< fmt, go/types, CRYPTO-MATH;
324 < internal/singleflight;
327 < golang.org/x/net/dns/dnsmessage,
328 golang.org/x/net/lif,
329 golang.org/x/net/route;
331 os, runtime, strconv, sync, unsafe,
335 internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
338 # net is unavoidable when doing any networking,
339 # so large dependencies must be kept out.
340 # This is a long-looking list but most of these
341 # are small with few dependencies.
343 golang.org/x/net/dns/dnsmessage,
344 golang.org/x/net/lif,
345 golang.org/x/net/route,
349 internal/singleflight,
356 math/rand !< net; # net uses runtime instead
358 # NET is net plus net-helper packages.
362 mime, net/textproto, net/url
365 # logging - most packages should not import; http and up is allowed
369 log !< crypto/tls, database/sql, go/importer, testing;
377 NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
378 sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
379 crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
381 # CRYPTO is core crypto algorithms - no cgo, fmt, net.
382 # Unfortunately, stuck with reflect via encoding/binary.
383 crypto/internal/boring/sig,
384 crypto/internal/boring/syso,
386 golang.org/x/sys/cpu,
390 < crypto/internal/alias
391 < crypto/internal/randutil
392 < crypto/internal/nistec/fiat
393 < crypto/internal/nistec
394 < crypto/internal/edwards25519/field
395 < crypto/internal/edwards25519, crypto/ecdh
399 crypto/internal/boring/bcache
400 < crypto/internal/boring
402 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
403 crypto/sha1, crypto/sha256, crypto/sha512
406 CGO, fmt, net !< CRYPTO;
408 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
409 CRYPTO, FMT, math/big
410 < crypto/internal/boring/bbig
414 < golang.org/x/crypto/cryptobyte/asn1
415 < golang.org/x/crypto/cryptobyte
416 < crypto/dsa, crypto/elliptic, crypto/rsa
420 CGO, net !< CRYPTO-MATH;
422 # TLS, Prince of Dependencies.
423 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
424 < golang.org/x/crypto/internal/alias
425 < golang.org/x/crypto/internal/subtle
426 < golang.org/x/crypto/chacha20
427 < golang.org/x/crypto/internal/poly1305
428 < golang.org/x/crypto/chacha20poly1305
429 < golang.org/x/crypto/hkdf
430 < crypto/x509/internal/macos
433 crypto/internal/boring/fipstls, crypto/x509/pkix
437 # crypto-aware packages
439 DEBUG, go/build, go/types, text/scanner, crypto/md5
441 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
444 NET, crypto/rand, mime/quotedprintable
450 # HTTP, King of Dependencies.
453 < golang.org/x/net/http2/hpack
454 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
456 FMT, NET, container/list, encoding/binary, log
457 < golang.org/x/text/transform
458 < golang.org/x/text/unicode/norm
459 < golang.org/x/text/unicode/bidi
460 < golang.org/x/text/secure/bidirule
461 < golang.org/x/net/idna
462 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
465 < net/http/httptrace;
468 golang.org/x/net/http/httpguts,
469 golang.org/x/net/http/httpproxy,
470 golang.org/x/net/http2/hpack,
472 net/http/internal/ascii,
473 net/http/internal/testcert,
479 # HTTP-aware packages
481 encoding/json, net/http
484 net/http, net/http/internal/ascii
485 < net/http/cookiejar, net/http/httputil;
495 FMT, compress/gzip, encoding/binary, text/tabwriter
498 OS, compress/gzip, regexp
501 html, internal/profile, net/http, runtime/pprof, runtime/trace
505 encoding/gob, encoding/json, go/token, html/template, net/http
521 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
524 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
525 internal/godebug, math/rand, encoding/hex, crypto/sha256
528 internal/fuzz, internal/testlog, runtime/pprof, regexp
529 < testing/internal/testdeps;
531 OS, flag, testing, internal/cfg
535 < internal/obscuretestdata;
538 < os/signal/internal/pty;
540 NET, testing, math/rand
541 < golang.org/x/net/nettest;
544 < os/exec/internal/fdtest;
546 FMT, container/heap, math/rand
550 < internal/diff, internal/txtar;
553 < internal/coverage/slicewriter;
555 encoding/binary, internal/unsafeheader, unsafe
556 < internal/coverage/slicereader;
558 FMT, io, internal/coverage/slicereader, internal/coverage/uleb128
559 < internal/coverage/stringtab;
561 FMT, encoding/binary, internal/coverage, internal/coverage/stringtab,
562 io, os, bufio, crypto/md5
563 < internal/coverage/encodemeta;
565 FMT, encoding/binary, internal/coverage, io, os,
566 crypto/md5, internal/coverage/stringtab
567 < internal/coverage/decodemeta;
570 // listStdPkgs returns the same list of packages as "go list std".
571 func listStdPkgs(goroot string) ([]string, error) {
572 // Based on cmd/go's matchPackages function.
575 src := filepath.Join(goroot, "src") + string(filepath.Separator)
576 walkFn := func(path string, d fs.DirEntry, err error) error {
577 if err != nil || !d.IsDir() || path == src {
581 base := filepath.Base(path)
582 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
583 return filepath.SkipDir
586 name := filepath.ToSlash(path[len(src):])
587 if name == "builtin" || name == "cmd" {
588 return filepath.SkipDir
591 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
594 if err := filepath.WalkDir(src, walkFn); err != nil {
600 func TestDependencies(t *testing.T) {
601 if !testenv.HasSrc() {
602 // Tests run in a limited file system and we do not
603 // provide access to every source file.
604 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
608 all, err := listStdPkgs(ctxt.GOROOT)
614 sawImport := map[string]map[string]bool{} // from package => to package => true
615 policy := depsPolicy(t)
617 for _, pkg := range all {
618 imports, err := findImports(pkg)
623 if sawImport[pkg] == nil {
624 sawImport[pkg] = map[string]bool{}
627 for _, imp := range imports {
628 sawImport[pkg][imp] = true
629 if !policy.HasEdge(pkg, imp) {
630 bad = append(bad, imp)
634 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
639 var buildIgnore = []byte("\n//go:build ignore")
641 func findImports(pkg string) ([]string, error) {
643 if strings.HasPrefix(pkg, "golang.org") {
644 vpkg = "vendor/" + pkg
646 dir := filepath.Join(Default.GOROOT, "src", vpkg)
647 files, err := os.ReadDir(dir)
652 var haveImport = map[string]bool{}
653 if pkg == "crypto/internal/boring" {
654 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
656 fset := token.NewFileSet()
657 for _, file := range files {
659 if name == "slice_go14.go" || name == "slice_go18.go" {
660 // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
663 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
667 name: filepath.Join(dir, name),
670 f, err := os.Open(info.name)
674 err = readGoInfo(f, &info)
677 return nil, fmt.Errorf("reading %v: %v", name, err)
679 if info.parsed.Name.Name == "main" {
682 if bytes.Contains(info.header, buildIgnore) {
685 for _, imp := range info.imports {
687 if !haveImport[path] {
688 haveImport[path] = true
689 imports = append(imports, path)
693 sort.Strings(imports)
697 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
698 func depsPolicy(t *testing.T) *dag.Graph {
699 g, err := dag.Parse(depsRules)
706 // TestStdlibLowercase tests that all standard library package names are
707 // lowercase. See Issue 40065.
708 func TestStdlibLowercase(t *testing.T) {
709 if !testenv.HasSrc() {
710 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
714 all, err := listStdPkgs(ctxt.GOROOT)
719 for _, pkgname := range all {
720 if strings.ToLower(pkgname) != pkgname {
721 t.Errorf("package %q should not use upper-case path", pkgname)
726 // TestFindImports tests that findImports works. See #43249.
727 func TestFindImports(t *testing.T) {
728 imports, err := findImports("go/build")
732 t.Logf("go/build imports %q", imports)
733 want := []string{"bytes", "os", "path/filepath", "strings"}
735 for _, w := range want {
736 for _, imp := range imports {
741 t.Errorf("expected to find %q in import list", w)