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 # slices depends on unsafe for overlapping check.
56 # These packages depend only on internal/goarch and unsafe.
57 internal/goarch, unsafe
62 # RUNTIME is the core runtime group of packages, all of them very light-weight.
63 internal/abi, internal/cpu, internal/goarch,
64 internal/coverage/rtcov, internal/godebugs, internal/goexperiment,
68 < internal/unsafeheader
69 < runtime/internal/sys
70 < runtime/internal/syscall
71 < runtime/internal/atomic
72 < runtime/internal/math
79 < internal/reflectlite
81 < internal/oserror, math/bits
102 # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
103 RUNTIME, unicode/utf8, unicode/utf16
104 < internal/syscall/windows/sysdll, syscall/js
106 < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
107 < internal/syscall/execenv
110 # TIME is SYSCALL plus the core packages about time, including context.
120 # MATH is RUNTIME plus the basic math packages.
141 # STR is basic string and buffer manipulation.
142 RUNTIME, io, unicode/utf8, unicode/utf16, unicode
149 # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
150 # OS includes string routines, but those must be layered above package os.
151 # OS does not include reflection.
155 < internal/safefilepath
162 unicode, fmt !< net, os, os/signal;
168 path/filepath, internal/godebug < os/exec;
170 io/ioutil, os/exec, os/signal
176 < golang.org/x/sys/cpu;
178 # FMT is OS (which includes string routines) plus reflect and fmt.
179 # It does not include package log, which should be avoided in core packages.
180 arena, strconv, unicode
192 # Misc packages needing only FMT.
197 internal/types/errors,
198 mime/quotedprintable,
199 net/internal/socktest,
209 # core ones do not use fmt.
215 < encoding/base32, encoding/base64;
217 FMT, encoding < flag;
219 fmt !< encoding/base32, encoding/base64;
221 FMT, encoding/base32, encoding/base64, internal/saferio
222 < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
223 encoding/json, encoding/pem, encoding/xml, mime;
228 < hash/adler32, hash/crc32, hash/crc64, hash/fnv;
231 FMT, encoding/binary, math/rand
235 FMT, encoding/binary, hash/adler32, hash/crc32
236 < compress/bzip2, compress/flate, compress/lzw, internal/zstd
237 < archive/zip, compress/gzip, compress/zlib;
241 < text/template/parse;
243 net/url, text/template/parse
245 < internal/lazytemplate;
247 encoding/json, html, text/template
254 < internal/lazyregexp;
257 encoding/binary, regexp
261 FMT, encoding/binary, compress/zlib, internal/saferio, internal/zstd
264 < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
268 # go parser and friends.
273 < go/internal/typeparams;
276 < go/build/constraint, go/doc/comment;
278 go/internal/typeparams, go/build/constraint
281 go/doc/comment, go/parser, text/tabwriter
288 container/heap, go/constant, go/parser, internal/types/errors
291 # The vast majority of standard library packages should not be resorting to regexp.
292 # go/types is a good chokepoint. It shouldn't use regexp, nor should anything
293 # that is low-enough level to be used by go/types.
296 go/doc/comment, go/parser, internal/lazyregexp, text/template
299 FMT, internal/goexperiment
302 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion, internal/platform
307 < database/sql/internal
308 < database/sql/driver
312 FMT, compress/lzw, compress/zlib
314 < image, image/color/palette
315 < image/internal/imageutil
317 < image/gif, image/jpeg, image/png;
319 # cgo, delayed as long as possible.
320 # If you add a dependency on CGO, you must add the package
321 # to cgoPackages in cmd/dist/test.go as well.
326 < runtime/msan, runtime/asan;
329 NONE < runtime/race/internal/amd64v1;
330 NONE < runtime/race/internal/amd64v3;
331 CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
333 # Bulk of the standard library must not use cgo.
334 # The prohibition stops at net and os/user.
335 C !< fmt, go/types, CRYPTO-MATH, log/slog;
345 < internal/singleflight;
348 < golang.org/x/net/dns/dnsmessage,
349 golang.org/x/net/lif,
350 golang.org/x/net/route;
352 os, runtime, strconv, sync, unsafe,
356 internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
359 # net is unavoidable when doing any networking,
360 # so large dependencies must be kept out.
361 # This is a long-looking list but most of these
362 # are small with few dependencies.
364 golang.org/x/net/dns/dnsmessage,
365 golang.org/x/net/lif,
366 golang.org/x/net/route,
370 internal/singleflight,
377 math/rand !< net; # net uses runtime instead
379 # NET is net plus net-helper packages.
383 mime, net/textproto, net/url
386 # logging - most packages should not import; http and up is allowed
390 log, log/slog !< crypto/tls, database/sql, go/importer, testing;
396 < log/slog/internal, log/slog/internal/buffer;
399 encoding, encoding/json,
401 log/slog/internal, log/slog/internal/buffer,
404 < log/slog/internal/slogtest, log/slog/internal/benchmarks;
409 NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
410 sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
411 crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
413 # CRYPTO is core crypto algorithms - no cgo, fmt, net.
414 # Unfortunately, stuck with reflect via encoding/binary.
415 crypto/internal/boring/sig,
416 crypto/internal/boring/syso,
418 golang.org/x/sys/cpu,
422 < crypto/internal/alias
426 crypto/internal/boring/bcache
427 < crypto/internal/boring
430 crypto/internal/alias
431 < crypto/internal/randutil
432 < crypto/internal/nistec/fiat
433 < crypto/internal/nistec
434 < crypto/internal/edwards25519/field
435 < crypto/internal/edwards25519;
438 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
439 crypto/sha1, crypto/sha256, crypto/sha512;
441 crypto/boring, crypto/internal/edwards25519/field
448 crypto/internal/edwards25519,
456 CGO, fmt, net !< CRYPTO;
458 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
459 CRYPTO, FMT, math/big
460 < crypto/internal/boring/bbig
464 < golang.org/x/crypto/cryptobyte/asn1
465 < golang.org/x/crypto/cryptobyte
466 < crypto/internal/bigmod
467 < crypto/dsa, crypto/elliptic, crypto/rsa
471 CGO, net !< CRYPTO-MATH;
473 # TLS, Prince of Dependencies.
474 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
475 < golang.org/x/crypto/internal/alias
476 < golang.org/x/crypto/internal/subtle
477 < golang.org/x/crypto/chacha20
478 < golang.org/x/crypto/internal/poly1305
479 < golang.org/x/crypto/chacha20poly1305
480 < golang.org/x/crypto/hkdf
481 < crypto/x509/internal/macos
484 crypto/internal/boring/fipstls, crypto/x509/pkix
488 # crypto-aware packages
490 DEBUG, go/build, go/types, text/scanner, crypto/md5
492 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
495 NET, crypto/rand, mime/quotedprintable
502 < hash/maphash; # for purego implementation
504 # HTTP, King of Dependencies.
507 < golang.org/x/net/http2/hpack
508 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
510 FMT, NET, container/list, encoding/binary, log
511 < golang.org/x/text/transform
512 < golang.org/x/text/unicode/norm
513 < golang.org/x/text/unicode/bidi
514 < golang.org/x/text/secure/bidirule
515 < golang.org/x/net/idna
516 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
519 < net/http/httptrace;
522 golang.org/x/net/http/httpguts,
523 golang.org/x/net/http/httpproxy,
524 golang.org/x/net/http2/hpack,
526 net/http/internal/ascii,
527 net/http/internal/testcert,
533 # HTTP-aware packages
535 encoding/json, net/http
538 net/http, net/http/internal/ascii
539 < net/http/cookiejar, net/http/httputil;
549 FMT, compress/gzip, encoding/binary, text/tabwriter
552 OS, compress/gzip, internal/lazyregexp
555 html, internal/profile, net/http, runtime/pprof, runtime/trace
559 encoding/gob, encoding/json, go/token, html/template, net/http
578 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
581 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
582 internal/godebug, math/rand, encoding/hex, crypto/sha256
585 internal/fuzz, internal/testlog, runtime/pprof, regexp
586 < testing/internal/testdeps;
588 OS, flag, testing, internal/cfg, internal/platform, internal/goroot
592 < internal/obscuretestdata;
597 NET, testing, math/rand
598 < golang.org/x/net/nettest;
601 < os/exec/internal/fdtest;
603 FMT, container/heap, math/rand
607 < internal/diff, internal/txtar;
609 FMT, crypto/md5, encoding/binary, regexp, sort, text/tabwriter, unsafe,
610 internal/coverage, internal/coverage/uleb128
611 < internal/coverage/cmerge,
612 internal/coverage/pods,
613 internal/coverage/slicereader,
614 internal/coverage/slicewriter;
616 internal/coverage/slicereader, internal/coverage/slicewriter
617 < internal/coverage/stringtab
618 < internal/coverage/decodecounter, internal/coverage/decodemeta,
619 internal/coverage/encodecounter, internal/coverage/encodemeta;
621 internal/coverage/cmerge
622 < internal/coverage/cformat;
626 internal/coverage/calloc,
627 internal/coverage/cformat,
628 internal/coverage/decodecounter, internal/coverage/decodemeta,
629 internal/coverage/encodecounter, internal/coverage/encodemeta,
630 internal/coverage/pods
634 // listStdPkgs returns the same list of packages as "go list std".
635 func listStdPkgs(goroot string) ([]string, error) {
636 // Based on cmd/go's matchPackages function.
639 src := filepath.Join(goroot, "src") + string(filepath.Separator)
640 walkFn := func(path string, d fs.DirEntry, err error) error {
641 if err != nil || !d.IsDir() || path == src {
645 base := filepath.Base(path)
646 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
647 return filepath.SkipDir
650 name := filepath.ToSlash(path[len(src):])
651 if name == "builtin" || name == "cmd" {
652 return filepath.SkipDir
655 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
658 if err := filepath.WalkDir(src, walkFn); err != nil {
664 func TestDependencies(t *testing.T) {
665 if !testenv.HasSrc() {
666 // Tests run in a limited file system and we do not
667 // provide access to every source file.
668 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
672 all, err := listStdPkgs(ctxt.GOROOT)
678 sawImport := map[string]map[string]bool{} // from package => to package => true
679 policy := depsPolicy(t)
681 for _, pkg := range all {
682 imports, err := findImports(pkg)
687 if sawImport[pkg] == nil {
688 sawImport[pkg] = map[string]bool{}
691 for _, imp := range imports {
692 sawImport[pkg][imp] = true
693 if !policy.HasEdge(pkg, imp) {
694 bad = append(bad, imp)
698 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
703 var buildIgnore = []byte("\n//go:build ignore")
705 func findImports(pkg string) ([]string, error) {
707 if strings.HasPrefix(pkg, "golang.org") {
708 vpkg = "vendor/" + pkg
710 dir := filepath.Join(Default.GOROOT, "src", vpkg)
711 files, err := os.ReadDir(dir)
716 var haveImport = map[string]bool{}
717 if pkg == "crypto/internal/boring" {
718 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
720 fset := token.NewFileSet()
721 for _, file := range files {
723 if name == "slice_go14.go" || name == "slice_go18.go" {
724 // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
727 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
731 name: filepath.Join(dir, name),
734 f, err := os.Open(info.name)
738 err = readGoInfo(f, &info)
741 return nil, fmt.Errorf("reading %v: %v", name, err)
743 if info.parsed.Name.Name == "main" {
746 if bytes.Contains(info.header, buildIgnore) {
749 for _, imp := range info.imports {
751 if !haveImport[path] {
752 haveImport[path] = true
753 imports = append(imports, path)
757 sort.Strings(imports)
761 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
762 func depsPolicy(t *testing.T) *dag.Graph {
763 g, err := dag.Parse(depsRules)
770 // TestStdlibLowercase tests that all standard library package names are
771 // lowercase. See Issue 40065.
772 func TestStdlibLowercase(t *testing.T) {
773 if !testenv.HasSrc() {
774 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
778 all, err := listStdPkgs(ctxt.GOROOT)
783 for _, pkgname := range all {
784 if strings.ToLower(pkgname) != pkgname {
785 t.Errorf("package %q should not use upper-case path", pkgname)
790 // TestFindImports tests that findImports works. See #43249.
791 func TestFindImports(t *testing.T) {
792 imports, err := findImports("go/build")
796 t.Logf("go/build imports %q", imports)
797 want := []string{"bytes", "os", "path/filepath", "strings"}
799 for _, w := range want {
800 for _, imp := range imports {
805 t.Errorf("expected to find %q in import list", w)