1 // Copyright 2022 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 < cmp, container/list, container/ring,
43 internal/cfg, internal/coverage, internal/coverage/rtcov,
44 internal/coverage/uleb128, internal/coverage/calloc,
45 internal/cpu, internal/goarch, internal/godebugs,
46 internal/goexperiment, internal/goos,
47 internal/goversion, internal/nettrace, internal/platform,
49 unicode/utf8, unicode/utf16, unicode,
52 # These packages depend only on internal/goarch and unsafe.
53 internal/goarch, unsafe
58 # RUNTIME is the core runtime group of packages, all of them very light-weight.
59 internal/abi, internal/cpu, internal/goarch,
60 internal/coverage/rtcov, internal/godebugs, internal/goexperiment,
64 < internal/unsafeheader
65 < runtime/internal/sys
66 < runtime/internal/syscall
67 < runtime/internal/atomic
68 < runtime/internal/math
75 < internal/reflectlite
77 < internal/oserror, math/bits
80 # slices depends on unsafe for overlapping check, cmp for comparison
81 # semantics, and math/bits for # calculating bitlength of numbers.
82 unsafe, cmp, math/bits
100 RUNTIME, unicode/utf8
105 # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
106 RUNTIME, unicode/utf8, unicode/utf16
107 < internal/syscall/windows/sysdll, syscall/js
109 < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
110 < internal/syscall/execenv
113 # TIME is SYSCALL plus the core packages about time, including context.
123 # MATH is RUNTIME plus the basic math packages.
134 < math/rand, math/rand/v2;
144 # STR is basic string and buffer manipulation.
145 RUNTIME, io, unicode/utf8, unicode/utf16, unicode
152 # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
153 # OS includes string routines, but those must be layered above package os.
154 # OS does not include reflection.
158 < internal/safefilepath
165 unicode, fmt !< net, os, os/signal;
171 path/filepath, internal/godebug < os/exec;
173 io/ioutil, os/exec, os/signal
179 < golang.org/x/sys/cpu;
181 # FMT is OS (which includes string routines) plus reflect and fmt.
182 # It does not include package log, which should be avoided in core packages.
183 arena, strconv, unicode
195 # Misc packages needing only FMT.
200 internal/types/errors,
201 mime/quotedprintable,
202 net/internal/socktest,
212 # core ones do not use fmt.
218 < encoding/base32, encoding/base64;
220 FMT, encoding < flag;
222 fmt !< encoding/base32, encoding/base64;
224 FMT, encoding/base32, encoding/base64, internal/saferio
225 < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
226 encoding/json, encoding/pem, encoding/xml, mime;
231 < hash/adler32, hash/crc32, hash/crc64, hash/fnv;
234 FMT, encoding/binary, math/rand
238 FMT, encoding/binary, hash/adler32, hash/crc32
239 < compress/bzip2, compress/flate, compress/lzw, internal/zstd
240 < archive/zip, compress/gzip, compress/zlib;
244 < text/template/parse;
246 net/url, text/template/parse
248 < internal/lazytemplate;
254 < internal/lazyregexp;
256 encoding/json, html, text/template, regexp
260 encoding/binary, regexp
264 FMT, encoding/binary, compress/zlib, internal/saferio, internal/zstd
267 < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
271 # go parser and friends.
276 < go/internal/typeparams;
279 < go/build/constraint, go/doc/comment;
281 go/internal/typeparams, go/build/constraint
284 go/doc/comment, go/parser, text/tabwriter
291 FMT, internal/goexperiment
294 container/heap, go/constant, go/parser, internal/buildcfg, internal/goversion, internal/types/errors
297 # The vast majority of standard library packages should not be resorting to regexp.
298 # go/types is a good chokepoint. It shouldn't use regexp, nor should anything
299 # that is low-enough level to be used by go/types.
302 go/doc/comment, go/parser, internal/lazyregexp, text/template
305 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion, internal/platform
310 < database/sql/internal
311 < database/sql/driver
315 FMT, compress/lzw, compress/zlib
317 < image, image/color/palette
318 < image/internal/imageutil
320 < image/gif, image/jpeg, image/png;
322 # cgo, delayed as long as possible.
323 # If you add a dependency on CGO, you must add the package
324 # to cgoPackages in cmd/dist/test.go as well.
329 < runtime/msan, runtime/asan;
332 NONE < runtime/race/internal/amd64v1;
333 NONE < runtime/race/internal/amd64v3;
334 CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
336 # Bulk of the standard library must not use cgo.
337 # The prohibition stops at net and os/user.
338 C !< fmt, go/types, CRYPTO-MATH, log/slog;
348 < internal/singleflight;
351 < golang.org/x/net/dns/dnsmessage,
352 golang.org/x/net/lif,
353 golang.org/x/net/route;
355 os, runtime, strconv, sync, unsafe,
359 internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
362 # net is unavoidable when doing any networking,
363 # so large dependencies must be kept out.
364 # This is a long-looking list but most of these
365 # are small with few dependencies.
367 golang.org/x/net/dns/dnsmessage,
368 golang.org/x/net/lif,
369 golang.org/x/net/route,
373 internal/singleflight,
380 math/rand !< net; # net uses runtime instead
382 # NET is net plus net-helper packages.
386 mime, net/textproto, net/url
389 # logging - most packages should not import; http and up is allowed
393 log, log/slog !< crypto/tls, database/sql, go/importer, testing;
399 < log/slog/internal, log/slog/internal/buffer;
402 encoding, encoding/json,
404 log/slog/internal, log/slog/internal/buffer,
407 < log/slog/internal/slogtest, log/slog/internal/benchmarks;
412 NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
413 sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
414 crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
416 # CRYPTO is core crypto algorithms - no cgo, fmt, net.
417 # Unfortunately, stuck with reflect via encoding/binary.
418 crypto/internal/boring/sig,
419 crypto/internal/boring/syso,
421 golang.org/x/sys/cpu,
425 < crypto/internal/alias
429 crypto/internal/boring/bcache
430 < crypto/internal/boring
433 crypto/internal/alias
434 < crypto/internal/randutil
435 < crypto/internal/nistec/fiat
436 < crypto/internal/nistec
437 < crypto/internal/edwards25519/field
438 < crypto/internal/edwards25519;
441 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
442 crypto/sha1, crypto/sha256, crypto/sha512;
444 crypto/boring, crypto/internal/edwards25519/field
451 crypto/internal/edwards25519,
459 CGO, fmt, net !< CRYPTO;
461 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
462 CRYPTO, FMT, math/big
463 < crypto/internal/boring/bbig
467 < golang.org/x/crypto/cryptobyte/asn1
468 < golang.org/x/crypto/cryptobyte
469 < crypto/internal/bigmod
470 < crypto/dsa, crypto/elliptic, crypto/rsa
474 CGO, net !< CRYPTO-MATH;
476 # TLS, Prince of Dependencies.
477 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
478 < golang.org/x/crypto/internal/alias
479 < golang.org/x/crypto/internal/subtle
480 < golang.org/x/crypto/chacha20
481 < golang.org/x/crypto/internal/poly1305
482 < golang.org/x/crypto/chacha20poly1305
483 < golang.org/x/crypto/hkdf
484 < crypto/x509/internal/macos
487 crypto/internal/boring/fipstls, crypto/x509/pkix
491 # crypto-aware packages
493 DEBUG, go/build, go/types, text/scanner, crypto/md5
495 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
498 NET, crypto/rand, mime/quotedprintable
505 < hash/maphash; # for purego implementation
507 # HTTP, King of Dependencies.
510 < golang.org/x/net/http2/hpack
511 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
513 FMT, NET, container/list, encoding/binary, log
514 < golang.org/x/text/transform
515 < golang.org/x/text/unicode/norm
516 < golang.org/x/text/unicode/bidi
517 < golang.org/x/text/secure/bidirule
518 < golang.org/x/net/idna
519 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
522 < net/http/httptrace;
525 golang.org/x/net/http/httpguts,
526 golang.org/x/net/http/httpproxy,
527 golang.org/x/net/http2/hpack,
529 net/http/internal/ascii,
530 net/http/internal/testcert,
536 # HTTP-aware packages
538 encoding/json, net/http
541 net/http, net/http/internal/ascii
542 < net/http/cookiejar, net/http/httputil;
552 FMT, compress/gzip, encoding/binary, text/tabwriter
555 OS, compress/gzip, internal/lazyregexp
558 html, internal/profile, net/http, runtime/pprof, runtime/trace
562 encoding/gob, encoding/json, go/token, html/template, net/http
567 bufio, bytes, internal/cpu, io, os, strings, sync
578 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
584 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
585 internal/godebug, math/rand, encoding/hex, crypto/sha256
588 internal/fuzz, internal/testlog, runtime/pprof, regexp
589 < testing/internal/testdeps;
591 OS, flag, testing, internal/cfg, internal/platform, internal/goroot
595 < internal/obscuretestdata;
600 NET, testing, math/rand
601 < golang.org/x/net/nettest;
604 < os/exec/internal/fdtest;
606 FMT, container/heap, math/rand
610 < internal/diff, internal/txtar;
612 FMT, crypto/md5, encoding/binary, regexp, sort, text/tabwriter, unsafe,
613 internal/coverage, internal/coverage/uleb128
614 < internal/coverage/cmerge,
615 internal/coverage/pods,
616 internal/coverage/slicereader,
617 internal/coverage/slicewriter;
619 internal/coverage/slicereader, internal/coverage/slicewriter
620 < internal/coverage/stringtab
621 < internal/coverage/decodecounter, internal/coverage/decodemeta,
622 internal/coverage/encodecounter, internal/coverage/encodemeta;
624 internal/coverage/cmerge
625 < internal/coverage/cformat;
627 internal/coverage, crypto/sha256, FMT
628 < cmd/internal/cov/covcmd;
632 internal/coverage/calloc,
633 internal/coverage/cformat,
634 internal/coverage/decodecounter, internal/coverage/decodemeta,
635 internal/coverage/encodecounter, internal/coverage/encodemeta,
636 internal/coverage/pods
640 // listStdPkgs returns the same list of packages as "go list std".
641 func listStdPkgs(goroot string) ([]string, error) {
642 // Based on cmd/go's matchPackages function.
645 src := filepath.Join(goroot, "src") + string(filepath.Separator)
646 walkFn := func(path string, d fs.DirEntry, err error) error {
647 if err != nil || !d.IsDir() || path == src {
651 base := filepath.Base(path)
652 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
653 return filepath.SkipDir
656 name := filepath.ToSlash(path[len(src):])
657 if name == "builtin" || name == "cmd" {
658 return filepath.SkipDir
661 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
664 if err := filepath.WalkDir(src, walkFn); err != nil {
670 func TestDependencies(t *testing.T) {
671 if !testenv.HasSrc() {
672 // Tests run in a limited file system and we do not
673 // provide access to every source file.
674 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
678 all, err := listStdPkgs(ctxt.GOROOT)
684 sawImport := map[string]map[string]bool{} // from package => to package => true
685 policy := depsPolicy(t)
687 for _, pkg := range all {
688 imports, err := findImports(pkg)
693 if sawImport[pkg] == nil {
694 sawImport[pkg] = map[string]bool{}
697 for _, imp := range imports {
698 sawImport[pkg][imp] = true
699 if !policy.HasEdge(pkg, imp) {
700 bad = append(bad, imp)
704 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
709 var buildIgnore = []byte("\n//go:build ignore")
711 func findImports(pkg string) ([]string, error) {
713 if strings.HasPrefix(pkg, "golang.org") {
714 vpkg = "vendor/" + pkg
716 dir := filepath.Join(Default.GOROOT, "src", vpkg)
717 files, err := os.ReadDir(dir)
722 var haveImport = map[string]bool{}
723 if pkg == "crypto/internal/boring" {
724 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
726 fset := token.NewFileSet()
727 for _, file := range files {
729 if name == "slice_go14.go" || name == "slice_go18.go" {
730 // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
733 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
737 name: filepath.Join(dir, name),
740 f, err := os.Open(info.name)
744 err = readGoInfo(f, &info)
747 return nil, fmt.Errorf("reading %v: %v", name, err)
749 if info.parsed.Name.Name == "main" {
752 if bytes.Contains(info.header, buildIgnore) {
755 for _, imp := range info.imports {
757 if !haveImport[path] {
758 haveImport[path] = true
759 imports = append(imports, path)
763 sort.Strings(imports)
767 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
768 func depsPolicy(t *testing.T) *dag.Graph {
769 g, err := dag.Parse(depsRules)
776 // TestStdlibLowercase tests that all standard library package names are
777 // lowercase. See Issue 40065.
778 func TestStdlibLowercase(t *testing.T) {
779 if !testenv.HasSrc() {
780 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
784 all, err := listStdPkgs(ctxt.GOROOT)
789 for _, pkgname := range all {
790 if strings.ToLower(pkgname) != pkgname {
791 t.Errorf("package %q should not use upper-case path", pkgname)
796 // TestFindImports tests that findImports works. See #43249.
797 func TestFindImports(t *testing.T) {
798 imports, err := findImports("go/build")
802 t.Logf("go/build imports %q", imports)
803 want := []string{"bytes", "os", "path/filepath", "strings"}
805 for _, w := range want {
806 for _, imp := range imports {
811 t.Errorf("expected to find %q in import list", w)