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 < constraints, container/list, container/ring,
43 internal/cfg, internal/coverage, internal/coverage/rtcov,
44 internal/coverage/uleb128, internal/coverage/calloc,
45 internal/cpu, internal/goarch,
46 internal/goexperiment, internal/goos,
47 internal/goversion, internal/nettrace, internal/platform,
49 maps, slices, unicode/utf8, unicode/utf16, unicode,
52 # These packages depend only on internal/goarch and unsafe.
53 internal/goarch, unsafe
56 # RUNTIME is the core runtime group of packages, all of them very light-weight.
57 internal/abi, internal/cpu, internal/goarch,
58 internal/coverage/rtcov, internal/goexperiment,
62 < internal/unsafeheader
63 < runtime/internal/sys
64 < runtime/internal/syscall
65 < runtime/internal/atomic
66 < runtime/internal/math
72 < internal/reflectlite
74 < internal/oserror, math/bits
95 # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
96 RUNTIME, unicode/utf8, unicode/utf16
97 < internal/syscall/windows/sysdll, syscall/js
99 < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
100 < internal/syscall/execenv
103 # TIME is SYSCALL plus the core packages about time, including context.
113 # MATH is RUNTIME plus the basic math packages.
134 # STR is basic string and buffer manipulation.
135 RUNTIME, io, unicode/utf8, unicode/utf16, unicode
142 # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
143 # OS includes string routines, but those must be layered above package os.
144 # OS does not include reflection.
148 < internal/safefilepath
155 unicode, fmt !< net, os, os/signal;
161 path/filepath, internal/godebug < os/exec;
163 io/ioutil, os/exec, os/signal
169 < golang.org/x/sys/cpu;
171 # FMT is OS (which includes string routines) plus reflect and fmt.
172 # It does not include package log, which should be avoided in core packages.
173 arena, strconv, unicode
185 # Misc packages needing only FMT.
190 internal/types/errors,
191 mime/quotedprintable,
192 net/internal/socktest,
202 # core ones do not use fmt.
208 < encoding/base32, encoding/base64;
210 FMT, encoding < flag;
212 fmt !< encoding/base32, encoding/base64;
214 FMT, encoding/base32, encoding/base64, internal/saferio
215 < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
216 encoding/json, encoding/pem, encoding/xml, mime;
221 < hash/adler32, hash/crc32, hash/crc64, hash/fnv;
224 FMT, encoding/binary, math/rand
228 FMT, encoding/binary, hash/adler32, hash/crc32
229 < compress/bzip2, compress/flate, compress/lzw
230 < archive/zip, compress/gzip, compress/zlib;
234 < text/template/parse;
236 net/url, text/template/parse
238 < internal/lazytemplate;
240 encoding/json, html, text/template
247 < internal/lazyregexp;
250 encoding/binary, regexp
254 FMT, encoding/binary, compress/zlib, internal/saferio
257 < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
261 # go parser and friends.
266 < go/internal/typeparams
270 < go/build/constraint, go/doc/comment;
272 go/build/constraint, go/doc/comment, go/parser, text/tabwriter
276 go/doc/comment, go/parser, internal/lazyregexp, text/template
282 container/heap, go/constant, go/parser, internal/types/errors, internal/lazyregexp
285 FMT, internal/goexperiment
288 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion
293 < database/sql/internal
294 < database/sql/driver
298 FMT, compress/lzw, compress/zlib
300 < image, image/color/palette
301 < image/internal/imageutil
303 < image/gif, image/jpeg, image/png;
305 # cgo, delayed as long as possible.
306 # If you add a dependency on CGO, you must add the package
307 # to cgoPackages in cmd/dist/test.go as well.
312 < runtime/msan, runtime/asan;
315 NONE < runtime/race/internal/amd64v1;
316 NONE < runtime/race/internal/amd64v3;
317 CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
319 # Bulk of the standard library must not use cgo.
320 # The prohibition stops at net and os/user.
321 C !< fmt, go/types, CRYPTO-MATH, log/slog;
331 < internal/singleflight;
334 < golang.org/x/net/dns/dnsmessage,
335 golang.org/x/net/lif,
336 golang.org/x/net/route;
338 os, runtime, strconv, sync, unsafe,
342 internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
345 # net is unavoidable when doing any networking,
346 # so large dependencies must be kept out.
347 # This is a long-looking list but most of these
348 # are small with few dependencies.
350 golang.org/x/net/dns/dnsmessage,
351 golang.org/x/net/lif,
352 golang.org/x/net/route,
356 internal/singleflight,
363 math/rand !< net; # net uses runtime instead
365 # NET is net plus net-helper packages.
369 mime, net/textproto, net/url
372 # logging - most packages should not import; http and up is allowed
376 log, log/slog !< crypto/tls, database/sql, go/importer, testing;
382 < log/slog/internal, log/slog/internal/buffer;
385 encoding, encoding/json,
387 log/slog/internal, log/slog/internal/buffer,
390 < log/slog/internal/slogtest, log/slog/internal/benchmarks;
395 NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
396 sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
397 crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
399 # CRYPTO is core crypto algorithms - no cgo, fmt, net.
400 # Unfortunately, stuck with reflect via encoding/binary.
401 crypto/internal/boring/sig,
402 crypto/internal/boring/syso,
404 golang.org/x/sys/cpu,
408 < crypto/internal/alias
412 crypto/internal/boring/bcache
413 < crypto/internal/boring
416 crypto/internal/alias
417 < crypto/internal/randutil
418 < crypto/internal/nistec/fiat
419 < crypto/internal/nistec
420 < crypto/internal/edwards25519/field
421 < crypto/internal/edwards25519;
424 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
425 crypto/sha1, crypto/sha256, crypto/sha512;
427 crypto/boring, crypto/internal/edwards25519/field
434 crypto/internal/edwards25519,
442 CGO, fmt, net !< CRYPTO;
444 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
445 CRYPTO, FMT, math/big
446 < crypto/internal/boring/bbig
450 < golang.org/x/crypto/cryptobyte/asn1
451 < golang.org/x/crypto/cryptobyte
452 < crypto/internal/bigmod
453 < crypto/dsa, crypto/elliptic, crypto/rsa
457 CGO, net !< CRYPTO-MATH;
459 # TLS, Prince of Dependencies.
460 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
461 < golang.org/x/crypto/internal/alias
462 < golang.org/x/crypto/internal/subtle
463 < golang.org/x/crypto/chacha20
464 < golang.org/x/crypto/internal/poly1305
465 < golang.org/x/crypto/chacha20poly1305
466 < golang.org/x/crypto/hkdf
467 < crypto/x509/internal/macos
470 crypto/internal/boring/fipstls, crypto/x509/pkix
474 # crypto-aware packages
476 DEBUG, go/build, go/types, text/scanner, crypto/md5
478 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
481 NET, crypto/rand, mime/quotedprintable
488 < hash/maphash; # for purego implementation
490 # HTTP, King of Dependencies.
493 < golang.org/x/net/http2/hpack
494 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
496 FMT, NET, container/list, encoding/binary, log
497 < golang.org/x/text/transform
498 < golang.org/x/text/unicode/norm
499 < golang.org/x/text/unicode/bidi
500 < golang.org/x/text/secure/bidirule
501 < golang.org/x/net/idna
502 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
505 < net/http/httptrace;
508 golang.org/x/net/http/httpguts,
509 golang.org/x/net/http/httpproxy,
510 golang.org/x/net/http2/hpack,
512 net/http/internal/ascii,
513 net/http/internal/testcert,
519 # HTTP-aware packages
521 encoding/json, net/http
524 net/http, net/http/internal/ascii
525 < net/http/cookiejar, net/http/httputil;
535 FMT, compress/gzip, encoding/binary, text/tabwriter
538 OS, compress/gzip, internal/lazyregexp
541 html, internal/profile, net/http, runtime/pprof, runtime/trace
545 encoding/gob, encoding/json, go/token, html/template, net/http
561 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
564 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
565 internal/godebug, math/rand, encoding/hex, crypto/sha256
568 internal/fuzz, internal/testlog, runtime/pprof, regexp
569 < testing/internal/testdeps;
571 OS, flag, testing, internal/cfg, internal/platform, internal/goroot
575 < internal/obscuretestdata;
580 NET, testing, math/rand
581 < golang.org/x/net/nettest;
584 < os/exec/internal/fdtest;
586 FMT, container/heap, math/rand
590 < internal/diff, internal/txtar;
592 FMT, crypto/md5, encoding/binary, regexp, sort, text/tabwriter, unsafe,
593 internal/coverage, internal/coverage/uleb128
594 < internal/coverage/cmerge,
595 internal/coverage/pods,
596 internal/coverage/slicereader,
597 internal/coverage/slicewriter;
599 internal/coverage/slicereader, internal/coverage/slicewriter
600 < internal/coverage/stringtab
601 < internal/coverage/decodecounter, internal/coverage/decodemeta,
602 internal/coverage/encodecounter, internal/coverage/encodemeta;
604 internal/coverage/cmerge
605 < internal/coverage/cformat;
608 internal/coverage/calloc,
609 internal/coverage/cformat,
610 internal/coverage/decodecounter, internal/coverage/decodemeta,
611 internal/coverage/encodecounter, internal/coverage/encodemeta,
612 internal/coverage/pods
616 // listStdPkgs returns the same list of packages as "go list std".
617 func listStdPkgs(goroot string) ([]string, error) {
618 // Based on cmd/go's matchPackages function.
621 src := filepath.Join(goroot, "src") + string(filepath.Separator)
622 walkFn := func(path string, d fs.DirEntry, err error) error {
623 if err != nil || !d.IsDir() || path == src {
627 base := filepath.Base(path)
628 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
629 return filepath.SkipDir
632 name := filepath.ToSlash(path[len(src):])
633 if name == "builtin" || name == "cmd" {
634 return filepath.SkipDir
637 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
640 if err := filepath.WalkDir(src, walkFn); err != nil {
646 func TestDependencies(t *testing.T) {
647 if !testenv.HasSrc() {
648 // Tests run in a limited file system and we do not
649 // provide access to every source file.
650 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
654 all, err := listStdPkgs(ctxt.GOROOT)
660 sawImport := map[string]map[string]bool{} // from package => to package => true
661 policy := depsPolicy(t)
663 for _, pkg := range all {
664 imports, err := findImports(pkg)
669 if sawImport[pkg] == nil {
670 sawImport[pkg] = map[string]bool{}
673 for _, imp := range imports {
674 sawImport[pkg][imp] = true
675 if !policy.HasEdge(pkg, imp) {
676 bad = append(bad, imp)
680 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
685 var buildIgnore = []byte("\n//go:build ignore")
687 func findImports(pkg string) ([]string, error) {
689 if strings.HasPrefix(pkg, "golang.org") {
690 vpkg = "vendor/" + pkg
692 dir := filepath.Join(Default.GOROOT, "src", vpkg)
693 files, err := os.ReadDir(dir)
698 var haveImport = map[string]bool{}
699 if pkg == "crypto/internal/boring" {
700 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
702 fset := token.NewFileSet()
703 for _, file := range files {
705 if name == "slice_go14.go" || name == "slice_go18.go" {
706 // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
709 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
713 name: filepath.Join(dir, name),
716 f, err := os.Open(info.name)
720 err = readGoInfo(f, &info)
723 return nil, fmt.Errorf("reading %v: %v", name, err)
725 if info.parsed.Name.Name == "main" {
728 if bytes.Contains(info.header, buildIgnore) {
731 for _, imp := range info.imports {
733 if !haveImport[path] {
734 haveImport[path] = true
735 imports = append(imports, path)
739 sort.Strings(imports)
743 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
744 func depsPolicy(t *testing.T) *dag.Graph {
745 g, err := dag.Parse(depsRules)
752 // TestStdlibLowercase tests that all standard library package names are
753 // lowercase. See Issue 40065.
754 func TestStdlibLowercase(t *testing.T) {
755 if !testenv.HasSrc() {
756 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
760 all, err := listStdPkgs(ctxt.GOROOT)
765 for _, pkgname := range all {
766 if strings.ToLower(pkgname) != pkgname {
767 t.Errorf("package %q should not use upper-case path", pkgname)
772 // TestFindImports tests that findImports works. See #43249.
773 func TestFindImports(t *testing.T) {
774 imports, err := findImports("go/build")
778 t.Logf("go/build imports %q", imports)
779 want := []string{"bytes", "os", "path/filepath", "strings"}
781 for _, w := range want {
782 for _, imp := range imports {
787 t.Errorf("expected to find %q in import list", w)