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,
48 maps, unicode/utf8, unicode/utf16, unicode,
51 # These packages depend only on internal/goarch and unsafe.
52 internal/goarch, unsafe
55 # RUNTIME is the core runtime group of packages, all of them very light-weight.
56 internal/abi, internal/cpu, internal/goarch,
57 internal/coverage/rtcov, internal/goexperiment,
61 < internal/unsafeheader
62 < runtime/internal/sys
63 < runtime/internal/syscall
64 < runtime/internal/atomic
65 < runtime/internal/math
71 < internal/reflectlite
73 < internal/oserror, math/bits
94 # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
95 RUNTIME, unicode/utf8, unicode/utf16
96 < internal/syscall/windows/sysdll, syscall/js
98 < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
99 < internal/syscall/execenv
102 # TIME is SYSCALL plus the core packages about time, including context.
112 # MATH is RUNTIME plus the basic math packages.
133 # STR is basic string and buffer manipulation.
134 RUNTIME, io, unicode/utf8, unicode/utf16, unicode
141 # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
142 # OS includes string routines, but those must be layered above package os.
143 # OS does not include reflection.
147 < internal/safefilepath
154 unicode, fmt !< net, os, os/signal;
160 path/filepath, internal/godebug < os/exec;
162 io/ioutil, os/exec, os/signal
168 < golang.org/x/sys/cpu;
170 # FMT is OS (which includes string routines) plus reflect and fmt.
171 # It does not include package log, which should be avoided in core packages.
172 arena, strconv, unicode
184 # Misc packages needing only FMT.
189 internal/types/errors,
190 mime/quotedprintable,
191 net/internal/socktest,
201 # core ones do not use fmt.
207 < encoding/base32, encoding/base64;
209 FMT, encoding < flag;
211 fmt !< encoding/base32, encoding/base64;
213 FMT, encoding/base32, encoding/base64, internal/saferio
214 < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
215 encoding/json, encoding/pem, encoding/xml, mime;
220 < hash/adler32, hash/crc32, hash/crc64, hash/fnv, hash/maphash;
223 FMT, encoding/binary, math/rand
227 FMT, encoding/binary, hash/adler32, hash/crc32
228 < compress/bzip2, compress/flate, compress/lzw
229 < archive/zip, compress/gzip, compress/zlib;
233 < text/template/parse;
235 net/url, text/template/parse
237 < internal/lazytemplate;
239 encoding/json, html, text/template
246 < internal/lazyregexp;
249 encoding/binary, regexp
253 FMT, encoding/binary, compress/zlib, internal/saferio
256 < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
260 # go parser and friends.
265 < go/internal/typeparams
269 < go/build/constraint, go/doc/comment;
271 go/build/constraint, go/doc/comment, go/parser, text/tabwriter
275 go/doc/comment, go/parser, internal/lazyregexp, text/template
281 container/heap, go/constant, go/parser, internal/types/errors, internal/lazyregexp
284 FMT, internal/goexperiment
287 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion
292 < database/sql/internal
293 < database/sql/driver
297 FMT, compress/lzw, compress/zlib
299 < image, image/color/palette
300 < image/internal/imageutil
302 < image/gif, image/jpeg, image/png;
304 # cgo, delayed as long as possible.
305 # If you add a dependency on CGO, you must add the package
306 # to cgoPackages in cmd/dist/test.go as well.
311 < runtime/msan, runtime/asan;
314 NONE < runtime/race/internal/amd64v1;
315 NONE < runtime/race/internal/amd64v3;
316 CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
318 # Bulk of the standard library must not use cgo.
319 # The prohibition stops at net and os/user.
320 C !< fmt, go/types, CRYPTO-MATH;
330 < internal/singleflight;
333 < golang.org/x/net/dns/dnsmessage,
334 golang.org/x/net/lif,
335 golang.org/x/net/route;
337 os, runtime, strconv, sync, unsafe,
341 internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
344 # net is unavoidable when doing any networking,
345 # so large dependencies must be kept out.
346 # This is a long-looking list but most of these
347 # are small with few dependencies.
349 golang.org/x/net/dns/dnsmessage,
350 golang.org/x/net/lif,
351 golang.org/x/net/route,
355 internal/singleflight,
362 math/rand !< net; # net uses runtime instead
364 # NET is net plus net-helper packages.
368 mime, net/textproto, net/url
371 # logging - most packages should not import; http and up is allowed
375 log !< crypto/tls, database/sql, go/importer, testing;
383 NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
384 sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
385 crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
387 # CRYPTO is core crypto algorithms - no cgo, fmt, net.
388 # Unfortunately, stuck with reflect via encoding/binary.
389 crypto/internal/boring/sig,
390 crypto/internal/boring/syso,
392 golang.org/x/sys/cpu,
396 < crypto/internal/alias
400 crypto/internal/boring/bcache
401 < crypto/internal/boring
404 crypto/internal/alias
405 < crypto/internal/randutil
406 < crypto/internal/nistec/fiat
407 < crypto/internal/nistec
408 < crypto/internal/edwards25519/field
409 < crypto/internal/edwards25519;
412 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
413 crypto/sha1, crypto/sha256, crypto/sha512;
415 crypto/boring, crypto/internal/edwards25519/field
422 crypto/internal/edwards25519,
430 CGO, fmt, net !< CRYPTO;
432 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
433 CRYPTO, FMT, math/big
434 < crypto/internal/boring/bbig
438 < golang.org/x/crypto/cryptobyte/asn1
439 < golang.org/x/crypto/cryptobyte
440 < crypto/internal/bigmod
441 < crypto/dsa, crypto/elliptic, crypto/rsa
445 CGO, net !< CRYPTO-MATH;
447 # TLS, Prince of Dependencies.
448 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
449 < golang.org/x/crypto/internal/alias
450 < golang.org/x/crypto/internal/subtle
451 < golang.org/x/crypto/chacha20
452 < golang.org/x/crypto/internal/poly1305
453 < golang.org/x/crypto/chacha20poly1305
454 < golang.org/x/crypto/hkdf
455 < crypto/x509/internal/macos
458 crypto/internal/boring/fipstls, crypto/x509/pkix
462 # crypto-aware packages
464 DEBUG, go/build, go/types, text/scanner, crypto/md5
466 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
469 NET, crypto/rand, mime/quotedprintable
475 # HTTP, King of Dependencies.
478 < golang.org/x/net/http2/hpack
479 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
481 FMT, NET, container/list, encoding/binary, log
482 < golang.org/x/text/transform
483 < golang.org/x/text/unicode/norm
484 < golang.org/x/text/unicode/bidi
485 < golang.org/x/text/secure/bidirule
486 < golang.org/x/net/idna
487 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
490 < net/http/httptrace;
493 golang.org/x/net/http/httpguts,
494 golang.org/x/net/http/httpproxy,
495 golang.org/x/net/http2/hpack,
497 net/http/internal/ascii,
498 net/http/internal/testcert,
504 # HTTP-aware packages
506 encoding/json, net/http
509 net/http, net/http/internal/ascii
510 < net/http/cookiejar, net/http/httputil;
520 FMT, compress/gzip, encoding/binary, text/tabwriter
523 OS, compress/gzip, internal/lazyregexp
526 html, internal/profile, net/http, runtime/pprof, runtime/trace
530 encoding/gob, encoding/json, go/token, html/template, net/http
546 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
549 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
550 internal/godebug, math/rand, encoding/hex, crypto/sha256
553 internal/fuzz, internal/testlog, runtime/pprof, regexp
554 < testing/internal/testdeps;
556 OS, flag, testing, internal/cfg, internal/platform, internal/goroot
560 < internal/obscuretestdata;
565 NET, testing, math/rand
566 < golang.org/x/net/nettest;
569 < os/exec/internal/fdtest;
571 FMT, container/heap, math/rand
575 < internal/diff, internal/txtar;
577 FMT, crypto/md5, encoding/binary, regexp, sort, text/tabwriter, unsafe,
578 internal/coverage, internal/coverage/uleb128
579 < internal/coverage/cmerge,
580 internal/coverage/pods,
581 internal/coverage/slicereader,
582 internal/coverage/slicewriter;
584 internal/coverage/slicereader, internal/coverage/slicewriter
585 < internal/coverage/stringtab
586 < internal/coverage/decodecounter, internal/coverage/decodemeta,
587 internal/coverage/encodecounter, internal/coverage/encodemeta;
589 internal/coverage/cmerge
590 < internal/coverage/cformat;
593 internal/coverage/calloc,
594 internal/coverage/cformat,
595 internal/coverage/decodecounter, internal/coverage/decodemeta,
596 internal/coverage/encodecounter, internal/coverage/encodemeta,
597 internal/coverage/pods
601 // listStdPkgs returns the same list of packages as "go list std".
602 func listStdPkgs(goroot string) ([]string, error) {
603 // Based on cmd/go's matchPackages function.
606 src := filepath.Join(goroot, "src") + string(filepath.Separator)
607 walkFn := func(path string, d fs.DirEntry, err error) error {
608 if err != nil || !d.IsDir() || path == src {
612 base := filepath.Base(path)
613 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
614 return filepath.SkipDir
617 name := filepath.ToSlash(path[len(src):])
618 if name == "builtin" || name == "cmd" {
619 return filepath.SkipDir
622 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
625 if err := filepath.WalkDir(src, walkFn); err != nil {
631 func TestDependencies(t *testing.T) {
632 if !testenv.HasSrc() {
633 // Tests run in a limited file system and we do not
634 // provide access to every source file.
635 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
639 all, err := listStdPkgs(ctxt.GOROOT)
645 sawImport := map[string]map[string]bool{} // from package => to package => true
646 policy := depsPolicy(t)
648 for _, pkg := range all {
649 imports, err := findImports(pkg)
654 if sawImport[pkg] == nil {
655 sawImport[pkg] = map[string]bool{}
658 for _, imp := range imports {
659 sawImport[pkg][imp] = true
660 if !policy.HasEdge(pkg, imp) {
661 bad = append(bad, imp)
665 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
670 var buildIgnore = []byte("\n//go:build ignore")
672 func findImports(pkg string) ([]string, error) {
674 if strings.HasPrefix(pkg, "golang.org") {
675 vpkg = "vendor/" + pkg
677 dir := filepath.Join(Default.GOROOT, "src", vpkg)
678 files, err := os.ReadDir(dir)
683 var haveImport = map[string]bool{}
684 if pkg == "crypto/internal/boring" {
685 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
687 fset := token.NewFileSet()
688 for _, file := range files {
690 if name == "slice_go14.go" || name == "slice_go18.go" {
691 // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
694 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
698 name: filepath.Join(dir, name),
701 f, err := os.Open(info.name)
705 err = readGoInfo(f, &info)
708 return nil, fmt.Errorf("reading %v: %v", name, err)
710 if info.parsed.Name.Name == "main" {
713 if bytes.Contains(info.header, buildIgnore) {
716 for _, imp := range info.imports {
718 if !haveImport[path] {
719 haveImport[path] = true
720 imports = append(imports, path)
724 sort.Strings(imports)
728 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
729 func depsPolicy(t *testing.T) *dag.Graph {
730 g, err := dag.Parse(depsRules)
737 // TestStdlibLowercase tests that all standard library package names are
738 // lowercase. See Issue 40065.
739 func TestStdlibLowercase(t *testing.T) {
740 if !testenv.HasSrc() {
741 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
745 all, err := listStdPkgs(ctxt.GOROOT)
750 for _, pkgname := range all {
751 if strings.ToLower(pkgname) != pkgname {
752 t.Errorf("package %q should not use upper-case path", pkgname)
757 // TestFindImports tests that findImports works. See #43249.
758 func TestFindImports(t *testing.T) {
759 imports, err := findImports("go/build")
763 t.Logf("go/build imports %q", imports)
764 want := []string{"bytes", "os", "path/filepath", "strings"}
766 for _, w := range want {
767 for _, imp := range imports {
772 t.Errorf("expected to find %q in import list", w)