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 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, internal/goos, unsafe
60 < internal/unsafeheader
61 < runtime/internal/sys
62 < runtime/internal/syscall
63 < runtime/internal/atomic
64 < runtime/internal/math
69 < internal/reflectlite
71 < internal/oserror, math/bits
92 # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
93 RUNTIME, unicode/utf8, unicode/utf16
94 < internal/syscall/windows/sysdll, syscall/js
96 < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
97 < internal/syscall/execenv
100 # TIME is SYSCALL plus the core packages about time, including context.
110 # MATH is RUNTIME plus the basic math packages.
131 # STR is basic string and buffer manipulation.
132 RUNTIME, io, unicode/utf8, unicode/utf16, unicode
139 # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
140 # OS includes string routines, but those must be layered above package os.
141 # OS does not include reflection.
151 unicode, fmt !< net, os, os/signal;
157 os < internal/godebug;
159 path/filepath, internal/godebug < os/exec;
161 io/ioutil, os/exec, os/signal
167 < golang.org/x/sys/cpu;
169 # FMT is OS (which includes string routines) plus reflect and fmt.
170 # It does not include package log, which should be avoided in core packages.
171 arena, strconv, unicode
183 # Misc packages needing only FMT.
188 internal/types/errors,
189 mime/quotedprintable,
190 net/internal/socktest,
200 # core ones do not use fmt.
206 < encoding/base32, encoding/base64;
208 FMT, encoding < flag;
210 fmt !< encoding/base32, encoding/base64;
212 FMT, encoding/base32, encoding/base64, internal/saferio
213 < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
214 encoding/json, encoding/pem, encoding/xml, mime;
219 < hash/adler32, hash/crc32, hash/crc64, hash/fnv, hash/maphash;
222 FMT, encoding/binary, math/rand
226 FMT, encoding/binary, hash/adler32, hash/crc32
227 < compress/bzip2, compress/flate, compress/lzw
228 < archive/zip, compress/gzip, compress/zlib;
232 < text/template/parse;
234 net/url, text/template/parse
236 < internal/lazytemplate;
238 encoding/json, html, text/template
245 < internal/lazyregexp;
248 encoding/binary, regexp
252 FMT, encoding/binary, compress/zlib, internal/saferio
255 < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
259 # go parser and friends.
264 < go/internal/typeparams
268 < go/build/constraint, go/doc/comment;
270 go/build/constraint, go/doc/comment, go/parser, text/tabwriter
274 go/doc/comment, go/parser, internal/lazyregexp, text/template
280 container/heap, go/constant, go/parser, internal/types/errors, regexp
283 FMT, internal/goexperiment
286 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion
291 < database/sql/internal
292 < database/sql/driver
296 FMT, compress/lzw, compress/zlib
298 < image, image/color/palette
299 < image/internal/imageutil
301 < image/gif, image/jpeg, image/png;
303 # cgo, delayed as long as possible.
304 # If you add a dependency on CGO, you must add the package
305 # to cgoPackages in cmd/dist/test.go as well.
310 < runtime/msan, runtime/asan;
313 NONE < runtime/race/internal/amd64v1;
314 NONE < runtime/race/internal/amd64v3;
315 CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
317 # Bulk of the standard library must not use cgo.
318 # The prohibition stops at net and os/user.
319 C !< fmt, go/types, CRYPTO-MATH;
329 < internal/singleflight;
332 < golang.org/x/net/dns/dnsmessage,
333 golang.org/x/net/lif,
334 golang.org/x/net/route;
336 os, runtime, strconv, sync, unsafe,
340 internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
343 # net is unavoidable when doing any networking,
344 # so large dependencies must be kept out.
345 # This is a long-looking list but most of these
346 # are small with few dependencies.
348 golang.org/x/net/dns/dnsmessage,
349 golang.org/x/net/lif,
350 golang.org/x/net/route,
354 internal/singleflight,
361 math/rand !< net; # net uses runtime instead
363 # NET is net plus net-helper packages.
367 mime, net/textproto, net/url
370 # logging - most packages should not import; http and up is allowed
374 log !< crypto/tls, database/sql, go/importer, testing;
382 NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
383 sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
384 crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
386 # CRYPTO is core crypto algorithms - no cgo, fmt, net.
387 # Unfortunately, stuck with reflect via encoding/binary.
388 crypto/internal/boring/sig,
389 crypto/internal/boring/syso,
391 golang.org/x/sys/cpu,
395 < crypto/internal/alias
396 < crypto/internal/randutil
397 < crypto/internal/nistec/fiat
398 < crypto/internal/nistec
399 < crypto/internal/edwards25519/field
400 < crypto/internal/edwards25519, crypto/ecdh
404 crypto/internal/boring/bcache
405 < crypto/internal/boring
407 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
408 crypto/sha1, crypto/sha256, crypto/sha512
411 CGO, fmt, net !< CRYPTO;
413 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
414 CRYPTO, FMT, math/big
415 < crypto/internal/boring/bbig
419 < golang.org/x/crypto/cryptobyte/asn1
420 < golang.org/x/crypto/cryptobyte
421 < crypto/dsa, crypto/elliptic, crypto/rsa
425 CGO, net !< CRYPTO-MATH;
427 # TLS, Prince of Dependencies.
428 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
429 < golang.org/x/crypto/internal/alias
430 < golang.org/x/crypto/internal/subtle
431 < golang.org/x/crypto/chacha20
432 < golang.org/x/crypto/internal/poly1305
433 < golang.org/x/crypto/chacha20poly1305
434 < golang.org/x/crypto/hkdf
435 < crypto/x509/internal/macos
438 crypto/internal/boring/fipstls, crypto/x509/pkix
442 # crypto-aware packages
444 DEBUG, go/build, go/types, text/scanner, crypto/md5
446 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
449 NET, crypto/rand, mime/quotedprintable
455 # HTTP, King of Dependencies.
458 < golang.org/x/net/http2/hpack
459 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
461 FMT, NET, container/list, encoding/binary, log
462 < golang.org/x/text/transform
463 < golang.org/x/text/unicode/norm
464 < golang.org/x/text/unicode/bidi
465 < golang.org/x/text/secure/bidirule
466 < golang.org/x/net/idna
467 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
470 < net/http/httptrace;
473 golang.org/x/net/http/httpguts,
474 golang.org/x/net/http/httpproxy,
475 golang.org/x/net/http2/hpack,
477 net/http/internal/ascii,
478 net/http/internal/testcert,
484 # HTTP-aware packages
486 encoding/json, net/http
489 net/http, net/http/internal/ascii
490 < net/http/cookiejar, net/http/httputil;
500 FMT, compress/gzip, encoding/binary, text/tabwriter
503 OS, compress/gzip, regexp
506 html, internal/profile, net/http, runtime/pprof, runtime/trace
510 encoding/gob, encoding/json, go/token, html/template, net/http
526 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
529 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
530 internal/godebug, math/rand, encoding/hex, crypto/sha256
533 internal/fuzz, internal/testlog, runtime/pprof, regexp
534 < testing/internal/testdeps;
536 OS, flag, testing, internal/cfg, internal/platform
540 < internal/obscuretestdata;
543 < os/signal/internal/pty;
545 NET, testing, math/rand
546 < golang.org/x/net/nettest;
549 < os/exec/internal/fdtest;
551 FMT, container/heap, math/rand
555 < internal/diff, internal/txtar;
558 < internal/coverage/slicewriter;
560 encoding/binary, internal/unsafeheader, unsafe
561 < internal/coverage/slicereader;
563 FMT, math, internal/coverage
564 < internal/coverage/cmerge;
566 FMT, math, internal/coverage, internal/coverage/cmerge, text/tabwriter
567 < internal/coverage/cformat;
569 FMT, io, internal/coverage/slicereader, internal/coverage/uleb128
570 < internal/coverage/stringtab;
572 FMT, encoding/binary, internal/coverage, internal/coverage/stringtab,
573 io, os, bufio, crypto/md5
574 < internal/coverage/encodemeta;
576 FMT, bufio, encoding/binary, internal/coverage,
577 internal/coverage/stringtab, internal/coverage/slicewriter, os, unsafe
578 < internal/coverage/encodecounter;
580 FMT, encoding/binary, internal/coverage, io, os,
581 internal/coverage/slicereader, internal/coverage/stringtab
582 < internal/coverage/decodecounter;
584 FMT, encoding/binary, internal/coverage, io, os,
585 crypto/md5, internal/coverage/stringtab
586 < internal/coverage/decodemeta;
588 FMT, internal/coverage, os,
589 path/filepath, regexp, sort, strconv
590 < internal/coverage/pods;
592 FMT, bufio, crypto/md5, encoding/binary, runtime/debug,
593 internal/coverage, internal/coverage/cmerge,
594 internal/coverage/cformat, internal/coverage/calloc,
595 internal/coverage/decodecounter, internal/coverage/decodemeta,
596 internal/coverage/encodecounter, internal/coverage/encodemeta,
597 internal/coverage/pods, os, path/filepath, reflect, time, unsafe
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)