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
98 # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
99 RUNTIME, unicode/utf8, unicode/utf16
100 < internal/syscall/windows/sysdll, syscall/js
102 < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
103 < internal/syscall/execenv
106 # TIME is SYSCALL plus the core packages about time, including context.
116 # MATH is RUNTIME plus the basic math packages.
137 # STR is basic string and buffer manipulation.
138 RUNTIME, io, unicode/utf8, unicode/utf16, unicode
145 # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
146 # OS includes string routines, but those must be layered above package os.
147 # OS does not include reflection.
151 < internal/safefilepath
158 unicode, fmt !< net, os, os/signal;
164 path/filepath, internal/godebug < os/exec;
166 io/ioutil, os/exec, os/signal
172 < golang.org/x/sys/cpu;
174 # FMT is OS (which includes string routines) plus reflect and fmt.
175 # It does not include package log, which should be avoided in core packages.
176 arena, strconv, unicode
188 # Misc packages needing only FMT.
193 internal/types/errors,
194 mime/quotedprintable,
195 net/internal/socktest,
205 # core ones do not use fmt.
211 < encoding/base32, encoding/base64;
213 FMT, encoding < flag;
215 fmt !< encoding/base32, encoding/base64;
217 FMT, encoding/base32, encoding/base64, internal/saferio
218 < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
219 encoding/json, encoding/pem, encoding/xml, mime;
224 < hash/adler32, hash/crc32, hash/crc64, hash/fnv;
226 # slices depends on unsafe for overlapping check, cmp for comparison
227 # semantics, and math/bits for # calculating bitlength of numbers.
228 unsafe, cmp, math/bits
232 FMT, encoding/binary, math/rand
236 FMT, encoding/binary, hash/adler32, hash/crc32
237 < compress/bzip2, compress/flate, compress/lzw, internal/zstd
238 < archive/zip, compress/gzip, compress/zlib;
242 < text/template/parse;
244 net/url, text/template/parse
246 < internal/lazytemplate;
248 encoding/json, html, text/template
255 < internal/lazyregexp;
258 encoding/binary, regexp
262 FMT, encoding/binary, compress/zlib, internal/saferio, internal/zstd
265 < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
269 # go parser and friends.
274 < go/internal/typeparams;
277 < go/build/constraint, go/doc/comment;
279 go/internal/typeparams, go/build/constraint
282 go/doc/comment, go/parser, text/tabwriter
289 container/heap, go/constant, go/parser, internal/types/errors
292 # The vast majority of standard library packages should not be resorting to regexp.
293 # go/types is a good chokepoint. It shouldn't use regexp, nor should anything
294 # that is low-enough level to be used by go/types.
297 go/doc/comment, go/parser, internal/lazyregexp, text/template
300 FMT, internal/goexperiment
303 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion, internal/platform
308 < database/sql/internal
309 < database/sql/driver
313 FMT, compress/lzw, compress/zlib
315 < image, image/color/palette
316 < image/internal/imageutil
318 < image/gif, image/jpeg, image/png;
320 # cgo, delayed as long as possible.
321 # If you add a dependency on CGO, you must add the package
322 # to cgoPackages in cmd/dist/test.go as well.
327 < runtime/msan, runtime/asan;
330 NONE < runtime/race/internal/amd64v1;
331 NONE < runtime/race/internal/amd64v3;
332 CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
334 # Bulk of the standard library must not use cgo.
335 # The prohibition stops at net and os/user.
336 C !< fmt, go/types, CRYPTO-MATH, log/slog;
346 < internal/singleflight;
349 < golang.org/x/net/dns/dnsmessage,
350 golang.org/x/net/lif,
351 golang.org/x/net/route;
353 os, runtime, strconv, sync, unsafe,
357 internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
360 # net is unavoidable when doing any networking,
361 # so large dependencies must be kept out.
362 # This is a long-looking list but most of these
363 # are small with few dependencies.
365 golang.org/x/net/dns/dnsmessage,
366 golang.org/x/net/lif,
367 golang.org/x/net/route,
371 internal/singleflight,
378 math/rand !< net; # net uses runtime instead
380 # NET is net plus net-helper packages.
384 mime, net/textproto, net/url
387 # logging - most packages should not import; http and up is allowed
391 log, log/slog !< crypto/tls, database/sql, go/importer, testing;
397 < log/slog/internal, log/slog/internal/buffer;
400 encoding, encoding/json,
402 log/slog/internal, log/slog/internal/buffer,
405 < log/slog/internal/slogtest, log/slog/internal/benchmarks;
410 NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
411 sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
412 crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
414 # CRYPTO is core crypto algorithms - no cgo, fmt, net.
415 # Unfortunately, stuck with reflect via encoding/binary.
416 crypto/internal/boring/sig,
417 crypto/internal/boring/syso,
419 golang.org/x/sys/cpu,
423 < crypto/internal/alias
427 crypto/internal/boring/bcache
428 < crypto/internal/boring
431 crypto/internal/alias
432 < crypto/internal/randutil
433 < crypto/internal/nistec/fiat
434 < crypto/internal/nistec
435 < crypto/internal/edwards25519/field
436 < crypto/internal/edwards25519;
439 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
440 crypto/sha1, crypto/sha256, crypto/sha512;
442 crypto/boring, crypto/internal/edwards25519/field
449 crypto/internal/edwards25519,
457 CGO, fmt, net !< CRYPTO;
459 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
460 CRYPTO, FMT, math/big
461 < crypto/internal/boring/bbig
465 < golang.org/x/crypto/cryptobyte/asn1
466 < golang.org/x/crypto/cryptobyte
467 < crypto/internal/bigmod
468 < crypto/dsa, crypto/elliptic, crypto/rsa
472 CGO, net !< CRYPTO-MATH;
474 # TLS, Prince of Dependencies.
475 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
476 < golang.org/x/crypto/internal/alias
477 < golang.org/x/crypto/internal/subtle
478 < golang.org/x/crypto/chacha20
479 < golang.org/x/crypto/internal/poly1305
480 < golang.org/x/crypto/chacha20poly1305
481 < golang.org/x/crypto/hkdf
482 < crypto/x509/internal/macos
485 crypto/internal/boring/fipstls, crypto/x509/pkix
489 # crypto-aware packages
491 DEBUG, go/build, go/types, text/scanner, crypto/md5
493 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
496 NET, crypto/rand, mime/quotedprintable
503 < hash/maphash; # for purego implementation
505 # HTTP, King of Dependencies.
508 < golang.org/x/net/http2/hpack
509 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
511 FMT, NET, container/list, encoding/binary, log
512 < golang.org/x/text/transform
513 < golang.org/x/text/unicode/norm
514 < golang.org/x/text/unicode/bidi
515 < golang.org/x/text/secure/bidirule
516 < golang.org/x/net/idna
517 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
520 < net/http/httptrace;
523 golang.org/x/net/http/httpguts,
524 golang.org/x/net/http/httpproxy,
525 golang.org/x/net/http2/hpack,
527 net/http/internal/ascii,
528 net/http/internal/testcert,
534 # HTTP-aware packages
536 encoding/json, net/http
539 net/http, net/http/internal/ascii
540 < net/http/cookiejar, net/http/httputil;
550 FMT, compress/gzip, encoding/binary, text/tabwriter
553 OS, compress/gzip, internal/lazyregexp
556 html, internal/profile, net/http, runtime/pprof, runtime/trace
560 encoding/gob, encoding/json, go/token, html/template, net/http
579 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
582 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
583 internal/godebug, math/rand, encoding/hex, crypto/sha256
586 internal/fuzz, internal/testlog, runtime/pprof, regexp
587 < testing/internal/testdeps;
589 OS, flag, testing, internal/cfg, internal/platform, internal/goroot
593 < internal/obscuretestdata;
598 NET, testing, math/rand
599 < golang.org/x/net/nettest;
602 < os/exec/internal/fdtest;
604 FMT, container/heap, math/rand
608 < internal/diff, internal/txtar;
610 FMT, crypto/md5, encoding/binary, regexp, sort, text/tabwriter, unsafe,
611 internal/coverage, internal/coverage/uleb128
612 < internal/coverage/cmerge,
613 internal/coverage/pods,
614 internal/coverage/slicereader,
615 internal/coverage/slicewriter;
617 internal/coverage/slicereader, internal/coverage/slicewriter
618 < internal/coverage/stringtab
619 < internal/coverage/decodecounter, internal/coverage/decodemeta,
620 internal/coverage/encodecounter, internal/coverage/encodemeta;
622 internal/coverage/cmerge
623 < internal/coverage/cformat;
627 internal/coverage/calloc,
628 internal/coverage/cformat,
629 internal/coverage/decodecounter, internal/coverage/decodemeta,
630 internal/coverage/encodecounter, internal/coverage/encodemeta,
631 internal/coverage/pods
635 // listStdPkgs returns the same list of packages as "go list std".
636 func listStdPkgs(goroot string) ([]string, error) {
637 // Based on cmd/go's matchPackages function.
640 src := filepath.Join(goroot, "src") + string(filepath.Separator)
641 walkFn := func(path string, d fs.DirEntry, err error) error {
642 if err != nil || !d.IsDir() || path == src {
646 base := filepath.Base(path)
647 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
648 return filepath.SkipDir
651 name := filepath.ToSlash(path[len(src):])
652 if name == "builtin" || name == "cmd" {
653 return filepath.SkipDir
656 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
659 if err := filepath.WalkDir(src, walkFn); err != nil {
665 func TestDependencies(t *testing.T) {
666 if !testenv.HasSrc() {
667 // Tests run in a limited file system and we do not
668 // provide access to every source file.
669 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
673 all, err := listStdPkgs(ctxt.GOROOT)
679 sawImport := map[string]map[string]bool{} // from package => to package => true
680 policy := depsPolicy(t)
682 for _, pkg := range all {
683 imports, err := findImports(pkg)
688 if sawImport[pkg] == nil {
689 sawImport[pkg] = map[string]bool{}
692 for _, imp := range imports {
693 sawImport[pkg][imp] = true
694 if !policy.HasEdge(pkg, imp) {
695 bad = append(bad, imp)
699 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
704 var buildIgnore = []byte("\n//go:build ignore")
706 func findImports(pkg string) ([]string, error) {
708 if strings.HasPrefix(pkg, "golang.org") {
709 vpkg = "vendor/" + pkg
711 dir := filepath.Join(Default.GOROOT, "src", vpkg)
712 files, err := os.ReadDir(dir)
717 var haveImport = map[string]bool{}
718 if pkg == "crypto/internal/boring" {
719 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
721 fset := token.NewFileSet()
722 for _, file := range files {
724 if name == "slice_go14.go" || name == "slice_go18.go" {
725 // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
728 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
732 name: filepath.Join(dir, name),
735 f, err := os.Open(info.name)
739 err = readGoInfo(f, &info)
742 return nil, fmt.Errorf("reading %v: %v", name, err)
744 if info.parsed.Name.Name == "main" {
747 if bytes.Contains(info.header, buildIgnore) {
750 for _, imp := range info.imports {
752 if !haveImport[path] {
753 haveImport[path] = true
754 imports = append(imports, path)
758 sort.Strings(imports)
762 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
763 func depsPolicy(t *testing.T) *dag.Graph {
764 g, err := dag.Parse(depsRules)
771 // TestStdlibLowercase tests that all standard library package names are
772 // lowercase. See Issue 40065.
773 func TestStdlibLowercase(t *testing.T) {
774 if !testenv.HasSrc() {
775 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
779 all, err := listStdPkgs(ctxt.GOROOT)
784 for _, pkgname := range all {
785 if strings.ToLower(pkgname) != pkgname {
786 t.Errorf("package %q should not use upper-case path", pkgname)
791 // TestFindImports tests that findImports works. See #43249.
792 func TestFindImports(t *testing.T) {
793 imports, err := findImports("go/build")
797 t.Logf("go/build imports %q", imports)
798 want := []string{"bytes", "os", "path/filepath", "strings"}
800 for _, w := range want {
801 for _, imp := range imports {
806 t.Errorf("expected to find %q in import list", w)