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;
269 < go/build/constraint, go/doc/comment;
271 go/internal/typeparams, go/build/constraint
274 go/doc/comment, go/parser, text/tabwriter
281 container/heap, go/constant, go/parser, internal/types/errors
284 # The vast majority of standard library packages should not be resorting to regexp.
285 # go/types is a good chokepoint. It shouldn't use regexp, nor should anything
286 # that is low-enough level to be used by go/types.
289 go/doc/comment, go/parser, internal/lazyregexp, text/template
292 FMT, internal/goexperiment
295 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion
300 < database/sql/internal
301 < database/sql/driver
305 FMT, compress/lzw, compress/zlib
307 < image, image/color/palette
308 < image/internal/imageutil
310 < image/gif, image/jpeg, image/png;
312 # cgo, delayed as long as possible.
313 # If you add a dependency on CGO, you must add the package
314 # to cgoPackages in cmd/dist/test.go as well.
319 < runtime/msan, runtime/asan;
322 NONE < runtime/race/internal/amd64v1;
323 NONE < runtime/race/internal/amd64v3;
324 CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
326 # Bulk of the standard library must not use cgo.
327 # The prohibition stops at net and os/user.
328 C !< fmt, go/types, CRYPTO-MATH, log/slog;
338 < internal/singleflight;
341 < golang.org/x/net/dns/dnsmessage,
342 golang.org/x/net/lif,
343 golang.org/x/net/route;
345 os, runtime, strconv, sync, unsafe,
349 internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
352 # net is unavoidable when doing any networking,
353 # so large dependencies must be kept out.
354 # This is a long-looking list but most of these
355 # are small with few dependencies.
357 golang.org/x/net/dns/dnsmessage,
358 golang.org/x/net/lif,
359 golang.org/x/net/route,
363 internal/singleflight,
370 math/rand !< net; # net uses runtime instead
372 # NET is net plus net-helper packages.
376 mime, net/textproto, net/url
379 # logging - most packages should not import; http and up is allowed
383 log, log/slog !< crypto/tls, database/sql, go/importer, testing;
389 < log/slog/internal, log/slog/internal/buffer;
392 encoding, encoding/json,
394 log/slog/internal, log/slog/internal/buffer,
397 < log/slog/internal/slogtest, log/slog/internal/benchmarks;
402 NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
403 sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
404 crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
406 # CRYPTO is core crypto algorithms - no cgo, fmt, net.
407 # Unfortunately, stuck with reflect via encoding/binary.
408 crypto/internal/boring/sig,
409 crypto/internal/boring/syso,
411 golang.org/x/sys/cpu,
415 < crypto/internal/alias
419 crypto/internal/boring/bcache
420 < crypto/internal/boring
423 crypto/internal/alias
424 < crypto/internal/randutil
425 < crypto/internal/nistec/fiat
426 < crypto/internal/nistec
427 < crypto/internal/edwards25519/field
428 < crypto/internal/edwards25519;
431 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
432 crypto/sha1, crypto/sha256, crypto/sha512;
434 crypto/boring, crypto/internal/edwards25519/field
441 crypto/internal/edwards25519,
449 CGO, fmt, net !< CRYPTO;
451 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
452 CRYPTO, FMT, math/big
453 < crypto/internal/boring/bbig
457 < golang.org/x/crypto/cryptobyte/asn1
458 < golang.org/x/crypto/cryptobyte
459 < crypto/internal/bigmod
460 < crypto/dsa, crypto/elliptic, crypto/rsa
464 CGO, net !< CRYPTO-MATH;
466 # TLS, Prince of Dependencies.
467 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
468 < golang.org/x/crypto/internal/alias
469 < golang.org/x/crypto/internal/subtle
470 < golang.org/x/crypto/chacha20
471 < golang.org/x/crypto/internal/poly1305
472 < golang.org/x/crypto/chacha20poly1305
473 < golang.org/x/crypto/hkdf
474 < crypto/x509/internal/macos
477 crypto/internal/boring/fipstls, crypto/x509/pkix
481 # crypto-aware packages
483 DEBUG, go/build, go/types, text/scanner, crypto/md5
485 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
488 NET, crypto/rand, mime/quotedprintable
495 < hash/maphash; # for purego implementation
497 # HTTP, King of Dependencies.
500 < golang.org/x/net/http2/hpack
501 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
503 FMT, NET, container/list, encoding/binary, log
504 < golang.org/x/text/transform
505 < golang.org/x/text/unicode/norm
506 < golang.org/x/text/unicode/bidi
507 < golang.org/x/text/secure/bidirule
508 < golang.org/x/net/idna
509 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
512 < net/http/httptrace;
515 golang.org/x/net/http/httpguts,
516 golang.org/x/net/http/httpproxy,
517 golang.org/x/net/http2/hpack,
519 net/http/internal/ascii,
520 net/http/internal/testcert,
526 # HTTP-aware packages
528 encoding/json, net/http
531 net/http, net/http/internal/ascii
532 < net/http/cookiejar, net/http/httputil;
542 FMT, compress/gzip, encoding/binary, text/tabwriter
545 OS, compress/gzip, internal/lazyregexp
548 html, internal/profile, net/http, runtime/pprof, runtime/trace
552 encoding/gob, encoding/json, go/token, html/template, net/http
568 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
571 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
572 internal/godebug, math/rand, encoding/hex, crypto/sha256
575 internal/fuzz, internal/testlog, runtime/pprof, regexp
576 < testing/internal/testdeps;
578 OS, flag, testing, internal/cfg, internal/platform, internal/goroot
582 < internal/obscuretestdata;
587 NET, testing, math/rand
588 < golang.org/x/net/nettest;
591 < os/exec/internal/fdtest;
593 FMT, container/heap, math/rand
597 < internal/diff, internal/txtar;
599 FMT, crypto/md5, encoding/binary, regexp, sort, text/tabwriter, unsafe,
600 internal/coverage, internal/coverage/uleb128
601 < internal/coverage/cmerge,
602 internal/coverage/pods,
603 internal/coverage/slicereader,
604 internal/coverage/slicewriter;
606 internal/coverage/slicereader, internal/coverage/slicewriter
607 < internal/coverage/stringtab
608 < internal/coverage/decodecounter, internal/coverage/decodemeta,
609 internal/coverage/encodecounter, internal/coverage/encodemeta;
611 internal/coverage/cmerge
612 < internal/coverage/cformat;
615 internal/coverage/calloc,
616 internal/coverage/cformat,
617 internal/coverage/decodecounter, internal/coverage/decodemeta,
618 internal/coverage/encodecounter, internal/coverage/encodemeta,
619 internal/coverage/pods
623 // listStdPkgs returns the same list of packages as "go list std".
624 func listStdPkgs(goroot string) ([]string, error) {
625 // Based on cmd/go's matchPackages function.
628 src := filepath.Join(goroot, "src") + string(filepath.Separator)
629 walkFn := func(path string, d fs.DirEntry, err error) error {
630 if err != nil || !d.IsDir() || path == src {
634 base := filepath.Base(path)
635 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
636 return filepath.SkipDir
639 name := filepath.ToSlash(path[len(src):])
640 if name == "builtin" || name == "cmd" {
641 return filepath.SkipDir
644 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
647 if err := filepath.WalkDir(src, walkFn); err != nil {
653 func TestDependencies(t *testing.T) {
654 if !testenv.HasSrc() {
655 // Tests run in a limited file system and we do not
656 // provide access to every source file.
657 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
661 all, err := listStdPkgs(ctxt.GOROOT)
667 sawImport := map[string]map[string]bool{} // from package => to package => true
668 policy := depsPolicy(t)
670 for _, pkg := range all {
671 imports, err := findImports(pkg)
676 if sawImport[pkg] == nil {
677 sawImport[pkg] = map[string]bool{}
680 for _, imp := range imports {
681 sawImport[pkg][imp] = true
682 if !policy.HasEdge(pkg, imp) {
683 bad = append(bad, imp)
687 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
692 var buildIgnore = []byte("\n//go:build ignore")
694 func findImports(pkg string) ([]string, error) {
696 if strings.HasPrefix(pkg, "golang.org") {
697 vpkg = "vendor/" + pkg
699 dir := filepath.Join(Default.GOROOT, "src", vpkg)
700 files, err := os.ReadDir(dir)
705 var haveImport = map[string]bool{}
706 if pkg == "crypto/internal/boring" {
707 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
709 fset := token.NewFileSet()
710 for _, file := range files {
712 if name == "slice_go14.go" || name == "slice_go18.go" {
713 // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
716 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
720 name: filepath.Join(dir, name),
723 f, err := os.Open(info.name)
727 err = readGoInfo(f, &info)
730 return nil, fmt.Errorf("reading %v: %v", name, err)
732 if info.parsed.Name.Name == "main" {
735 if bytes.Contains(info.header, buildIgnore) {
738 for _, imp := range info.imports {
740 if !haveImport[path] {
741 haveImport[path] = true
742 imports = append(imports, path)
746 sort.Strings(imports)
750 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
751 func depsPolicy(t *testing.T) *dag.Graph {
752 g, err := dag.Parse(depsRules)
759 // TestStdlibLowercase tests that all standard library package names are
760 // lowercase. See Issue 40065.
761 func TestStdlibLowercase(t *testing.T) {
762 if !testenv.HasSrc() {
763 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
767 all, err := listStdPkgs(ctxt.GOROOT)
772 for _, pkgname := range all {
773 if strings.ToLower(pkgname) != pkgname {
774 t.Errorf("package %q should not use upper-case path", pkgname)
779 // TestFindImports tests that findImports works. See #43249.
780 func TestFindImports(t *testing.T) {
781 imports, err := findImports("go/build")
785 t.Logf("go/build imports %q", imports)
786 want := []string{"bytes", "os", "path/filepath", "strings"}
788 for _, w := range want {
789 for _, imp := range imports {
794 t.Errorf("expected to find %q in import list", w)