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, internal/godebugs,
46 internal/goexperiment, internal/goos,
47 internal/goversion, internal/nettrace, internal/platform,
49 maps, 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
60 # RUNTIME is the core runtime group of packages, all of them very light-weight.
61 internal/abi, internal/cpu, internal/goarch,
62 internal/coverage/rtcov, internal/godebugs, internal/goexperiment,
66 < internal/unsafeheader
67 < runtime/internal/sys
68 < runtime/internal/syscall
69 < runtime/internal/atomic
70 < runtime/internal/math
77 < internal/reflectlite
79 < internal/oserror, math/bits
100 # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
101 RUNTIME, unicode/utf8, unicode/utf16
102 < internal/syscall/windows/sysdll, syscall/js
104 < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
105 < internal/syscall/execenv
108 # TIME is SYSCALL plus the core packages about time, including context.
118 # MATH is RUNTIME plus the basic math packages.
139 # STR is basic string and buffer manipulation.
140 RUNTIME, io, unicode/utf8, unicode/utf16, unicode
147 # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
148 # OS includes string routines, but those must be layered above package os.
149 # OS does not include reflection.
153 < internal/safefilepath
160 unicode, fmt !< net, os, os/signal;
166 path/filepath, internal/godebug < os/exec;
168 io/ioutil, os/exec, os/signal
174 < golang.org/x/sys/cpu;
176 # FMT is OS (which includes string routines) plus reflect and fmt.
177 # It does not include package log, which should be avoided in core packages.
178 arena, strconv, unicode
190 # Misc packages needing only FMT.
195 internal/types/errors,
196 mime/quotedprintable,
197 net/internal/socktest,
207 # core ones do not use fmt.
213 < encoding/base32, encoding/base64;
215 FMT, encoding < flag;
217 fmt !< encoding/base32, encoding/base64;
219 FMT, encoding/base32, encoding/base64, internal/saferio
220 < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
221 encoding/json, encoding/pem, encoding/xml, mime;
226 < hash/adler32, hash/crc32, hash/crc64, hash/fnv;
229 FMT, encoding/binary, math/rand
233 FMT, encoding/binary, hash/adler32, hash/crc32
234 < compress/bzip2, compress/flate, compress/lzw, internal/zstd
235 < archive/zip, compress/gzip, compress/zlib;
239 < text/template/parse;
241 net/url, text/template/parse
243 < internal/lazytemplate;
245 encoding/json, html, text/template
252 < internal/lazyregexp;
255 encoding/binary, regexp
259 FMT, encoding/binary, compress/zlib, internal/saferio, internal/zstd
262 < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
266 # go parser and friends.
271 < go/internal/typeparams;
274 < go/build/constraint, go/doc/comment;
276 go/internal/typeparams, go/build/constraint
279 go/doc/comment, go/parser, text/tabwriter
286 container/heap, go/constant, go/parser, internal/types/errors
289 # The vast majority of standard library packages should not be resorting to regexp.
290 # go/types is a good chokepoint. It shouldn't use regexp, nor should anything
291 # that is low-enough level to be used by go/types.
294 go/doc/comment, go/parser, internal/lazyregexp, text/template
297 FMT, internal/goexperiment
300 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion, internal/platform
305 < database/sql/internal
306 < database/sql/driver
310 FMT, compress/lzw, compress/zlib
312 < image, image/color/palette
313 < image/internal/imageutil
315 < image/gif, image/jpeg, image/png;
317 # cgo, delayed as long as possible.
318 # If you add a dependency on CGO, you must add the package
319 # to cgoPackages in cmd/dist/test.go as well.
324 < runtime/msan, runtime/asan;
327 NONE < runtime/race/internal/amd64v1;
328 NONE < runtime/race/internal/amd64v3;
329 CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
331 # Bulk of the standard library must not use cgo.
332 # The prohibition stops at net and os/user.
333 C !< fmt, go/types, CRYPTO-MATH, log/slog;
343 < internal/singleflight;
346 < golang.org/x/net/dns/dnsmessage,
347 golang.org/x/net/lif,
348 golang.org/x/net/route;
350 os, runtime, strconv, sync, unsafe,
354 internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
357 # net is unavoidable when doing any networking,
358 # so large dependencies must be kept out.
359 # This is a long-looking list but most of these
360 # are small with few dependencies.
362 golang.org/x/net/dns/dnsmessage,
363 golang.org/x/net/lif,
364 golang.org/x/net/route,
368 internal/singleflight,
375 math/rand !< net; # net uses runtime instead
377 # NET is net plus net-helper packages.
381 mime, net/textproto, net/url
384 # logging - most packages should not import; http and up is allowed
388 log, log/slog !< crypto/tls, database/sql, go/importer, testing;
394 < log/slog/internal, log/slog/internal/buffer;
397 encoding, encoding/json,
399 log/slog/internal, log/slog/internal/buffer,
402 < log/slog/internal/slogtest, log/slog/internal/benchmarks;
407 NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
408 sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
409 crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
411 # CRYPTO is core crypto algorithms - no cgo, fmt, net.
412 # Unfortunately, stuck with reflect via encoding/binary.
413 crypto/internal/boring/sig,
414 crypto/internal/boring/syso,
416 golang.org/x/sys/cpu,
420 < crypto/internal/alias
424 crypto/internal/boring/bcache
425 < crypto/internal/boring
428 crypto/internal/alias
429 < crypto/internal/randutil
430 < crypto/internal/nistec/fiat
431 < crypto/internal/nistec
432 < crypto/internal/edwards25519/field
433 < crypto/internal/edwards25519;
436 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
437 crypto/sha1, crypto/sha256, crypto/sha512;
439 crypto/boring, crypto/internal/edwards25519/field
446 crypto/internal/edwards25519,
454 CGO, fmt, net !< CRYPTO;
456 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
457 CRYPTO, FMT, math/big
458 < crypto/internal/boring/bbig
462 < golang.org/x/crypto/cryptobyte/asn1
463 < golang.org/x/crypto/cryptobyte
464 < crypto/internal/bigmod
465 < crypto/dsa, crypto/elliptic, crypto/rsa
469 CGO, net !< CRYPTO-MATH;
471 # TLS, Prince of Dependencies.
472 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
473 < golang.org/x/crypto/internal/alias
474 < golang.org/x/crypto/internal/subtle
475 < golang.org/x/crypto/chacha20
476 < golang.org/x/crypto/internal/poly1305
477 < golang.org/x/crypto/chacha20poly1305
478 < golang.org/x/crypto/hkdf
479 < crypto/x509/internal/macos
482 crypto/internal/boring/fipstls, crypto/x509/pkix
486 # crypto-aware packages
488 DEBUG, go/build, go/types, text/scanner, crypto/md5
490 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
493 NET, crypto/rand, mime/quotedprintable
500 < hash/maphash; # for purego implementation
502 # HTTP, King of Dependencies.
505 < golang.org/x/net/http2/hpack
506 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
508 FMT, NET, container/list, encoding/binary, log
509 < golang.org/x/text/transform
510 < golang.org/x/text/unicode/norm
511 < golang.org/x/text/unicode/bidi
512 < golang.org/x/text/secure/bidirule
513 < golang.org/x/net/idna
514 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
517 < net/http/httptrace;
520 golang.org/x/net/http/httpguts,
521 golang.org/x/net/http/httpproxy,
522 golang.org/x/net/http2/hpack,
524 net/http/internal/ascii,
525 net/http/internal/testcert,
531 # HTTP-aware packages
533 encoding/json, net/http
536 net/http, net/http/internal/ascii
537 < net/http/cookiejar, net/http/httputil;
547 FMT, compress/gzip, encoding/binary, text/tabwriter
550 OS, compress/gzip, internal/lazyregexp
553 html, internal/profile, net/http, runtime/pprof, runtime/trace
557 encoding/gob, encoding/json, go/token, html/template, net/http
576 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
579 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
580 internal/godebug, math/rand, encoding/hex, crypto/sha256
583 internal/fuzz, internal/testlog, runtime/pprof, regexp
584 < testing/internal/testdeps;
586 OS, flag, testing, internal/cfg, internal/platform, internal/goroot
590 < internal/obscuretestdata;
595 NET, testing, math/rand
596 < golang.org/x/net/nettest;
599 < os/exec/internal/fdtest;
601 FMT, container/heap, math/rand
605 < internal/diff, internal/txtar;
607 FMT, crypto/md5, encoding/binary, regexp, sort, text/tabwriter, unsafe,
608 internal/coverage, internal/coverage/uleb128
609 < internal/coverage/cmerge,
610 internal/coverage/pods,
611 internal/coverage/slicereader,
612 internal/coverage/slicewriter;
614 internal/coverage/slicereader, internal/coverage/slicewriter
615 < internal/coverage/stringtab
616 < internal/coverage/decodecounter, internal/coverage/decodemeta,
617 internal/coverage/encodecounter, internal/coverage/encodemeta;
619 internal/coverage/cmerge
620 < internal/coverage/cformat;
623 internal/coverage/calloc,
624 internal/coverage/cformat,
625 internal/coverage/decodecounter, internal/coverage/decodemeta,
626 internal/coverage/encodecounter, internal/coverage/encodemeta,
627 internal/coverage/pods
631 // listStdPkgs returns the same list of packages as "go list std".
632 func listStdPkgs(goroot string) ([]string, error) {
633 // Based on cmd/go's matchPackages function.
636 src := filepath.Join(goroot, "src") + string(filepath.Separator)
637 walkFn := func(path string, d fs.DirEntry, err error) error {
638 if err != nil || !d.IsDir() || path == src {
642 base := filepath.Base(path)
643 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
644 return filepath.SkipDir
647 name := filepath.ToSlash(path[len(src):])
648 if name == "builtin" || name == "cmd" {
649 return filepath.SkipDir
652 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
655 if err := filepath.WalkDir(src, walkFn); err != nil {
661 func TestDependencies(t *testing.T) {
662 if !testenv.HasSrc() {
663 // Tests run in a limited file system and we do not
664 // provide access to every source file.
665 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
669 all, err := listStdPkgs(ctxt.GOROOT)
675 sawImport := map[string]map[string]bool{} // from package => to package => true
676 policy := depsPolicy(t)
678 for _, pkg := range all {
679 imports, err := findImports(pkg)
684 if sawImport[pkg] == nil {
685 sawImport[pkg] = map[string]bool{}
688 for _, imp := range imports {
689 sawImport[pkg][imp] = true
690 if !policy.HasEdge(pkg, imp) {
691 bad = append(bad, imp)
695 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
700 var buildIgnore = []byte("\n//go:build ignore")
702 func findImports(pkg string) ([]string, error) {
704 if strings.HasPrefix(pkg, "golang.org") {
705 vpkg = "vendor/" + pkg
707 dir := filepath.Join(Default.GOROOT, "src", vpkg)
708 files, err := os.ReadDir(dir)
713 var haveImport = map[string]bool{}
714 if pkg == "crypto/internal/boring" {
715 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
717 fset := token.NewFileSet()
718 for _, file := range files {
720 if name == "slice_go14.go" || name == "slice_go18.go" {
721 // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
724 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
728 name: filepath.Join(dir, name),
731 f, err := os.Open(info.name)
735 err = readGoInfo(f, &info)
738 return nil, fmt.Errorf("reading %v: %v", name, err)
740 if info.parsed.Name.Name == "main" {
743 if bytes.Contains(info.header, buildIgnore) {
746 for _, imp := range info.imports {
748 if !haveImport[path] {
749 haveImport[path] = true
750 imports = append(imports, path)
754 sort.Strings(imports)
758 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
759 func depsPolicy(t *testing.T) *dag.Graph {
760 g, err := dag.Parse(depsRules)
767 // TestStdlibLowercase tests that all standard library package names are
768 // lowercase. See Issue 40065.
769 func TestStdlibLowercase(t *testing.T) {
770 if !testenv.HasSrc() {
771 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
775 all, err := listStdPkgs(ctxt.GOROOT)
780 for _, pkgname := range all {
781 if strings.ToLower(pkgname) != pkgname {
782 t.Errorf("package %q should not use upper-case path", pkgname)
787 // TestFindImports tests that findImports works. See #43249.
788 func TestFindImports(t *testing.T) {
789 imports, err := findImports("go/build")
793 t.Logf("go/build imports %q", imports)
794 want := []string{"bytes", "os", "path/filepath", "strings"}
796 for _, w := range want {
797 for _, imp := range imports {
802 t.Errorf("expected to find %q in import list", w)