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,
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.
153 unicode, fmt !< net, os, os/signal;
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
399 crypto/internal/boring/bcache
400 < crypto/internal/boring
403 crypto/internal/alias
404 < crypto/internal/randutil
405 < crypto/internal/nistec/fiat
406 < crypto/internal/nistec
407 < crypto/internal/edwards25519/field
408 < crypto/internal/edwards25519;
411 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
412 crypto/sha1, crypto/sha256, crypto/sha512;
414 crypto/boring, crypto/internal/edwards25519/field
421 crypto/internal/edwards25519,
429 CGO, fmt, net !< CRYPTO;
431 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
432 CRYPTO, FMT, math/big
433 < crypto/internal/boring/bbig
437 < golang.org/x/crypto/cryptobyte/asn1
438 < golang.org/x/crypto/cryptobyte
439 < crypto/dsa, crypto/elliptic, crypto/rsa
443 CGO, net !< CRYPTO-MATH;
445 # TLS, Prince of Dependencies.
446 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
447 < golang.org/x/crypto/internal/alias
448 < golang.org/x/crypto/internal/subtle
449 < golang.org/x/crypto/chacha20
450 < golang.org/x/crypto/internal/poly1305
451 < golang.org/x/crypto/chacha20poly1305
452 < golang.org/x/crypto/hkdf
453 < crypto/x509/internal/macos
456 crypto/internal/boring/fipstls, crypto/x509/pkix
460 # crypto-aware packages
462 DEBUG, go/build, go/types, text/scanner, crypto/md5
464 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
467 NET, crypto/rand, mime/quotedprintable
473 # HTTP, King of Dependencies.
476 < golang.org/x/net/http2/hpack
477 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
479 FMT, NET, container/list, encoding/binary, log
480 < golang.org/x/text/transform
481 < golang.org/x/text/unicode/norm
482 < golang.org/x/text/unicode/bidi
483 < golang.org/x/text/secure/bidirule
484 < golang.org/x/net/idna
485 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
488 < net/http/httptrace;
491 golang.org/x/net/http/httpguts,
492 golang.org/x/net/http/httpproxy,
493 golang.org/x/net/http2/hpack,
495 net/http/internal/ascii,
496 net/http/internal/testcert,
502 # HTTP-aware packages
504 encoding/json, net/http
507 net/http, net/http/internal/ascii
508 < net/http/cookiejar, net/http/httputil;
518 FMT, compress/gzip, encoding/binary, text/tabwriter
521 OS, compress/gzip, regexp
524 html, internal/profile, net/http, runtime/pprof, runtime/trace
528 encoding/gob, encoding/json, go/token, html/template, net/http
544 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
547 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
548 internal/godebug, math/rand, encoding/hex, crypto/sha256
551 internal/fuzz, internal/testlog, runtime/pprof, regexp
552 < testing/internal/testdeps;
554 OS, flag, testing, internal/cfg, internal/platform, internal/goroot
558 < internal/obscuretestdata;
563 NET, testing, math/rand
564 < golang.org/x/net/nettest;
567 < os/exec/internal/fdtest;
569 FMT, container/heap, math/rand
573 < internal/diff, internal/txtar;
575 FMT, crypto/md5, encoding/binary, regexp, sort, text/tabwriter, unsafe,
576 internal/coverage, internal/coverage/uleb128
577 < internal/coverage/cmerge,
578 internal/coverage/pods,
579 internal/coverage/slicereader,
580 internal/coverage/slicewriter;
582 internal/coverage/slicereader, internal/coverage/slicewriter
583 < internal/coverage/stringtab
584 < internal/coverage/decodecounter, internal/coverage/decodemeta,
585 internal/coverage/encodecounter, internal/coverage/encodemeta;
587 internal/coverage/cmerge
588 < internal/coverage/cformat;
591 internal/coverage/calloc,
592 internal/coverage/cformat,
593 internal/coverage/decodecounter, internal/coverage/decodemeta,
594 internal/coverage/encodecounter, internal/coverage/encodemeta,
595 internal/coverage/pods
599 // listStdPkgs returns the same list of packages as "go list std".
600 func listStdPkgs(goroot string) ([]string, error) {
601 // Based on cmd/go's matchPackages function.
604 src := filepath.Join(goroot, "src") + string(filepath.Separator)
605 walkFn := func(path string, d fs.DirEntry, err error) error {
606 if err != nil || !d.IsDir() || path == src {
610 base := filepath.Base(path)
611 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
612 return filepath.SkipDir
615 name := filepath.ToSlash(path[len(src):])
616 if name == "builtin" || name == "cmd" {
617 return filepath.SkipDir
620 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
623 if err := filepath.WalkDir(src, walkFn); err != nil {
629 func TestDependencies(t *testing.T) {
630 if !testenv.HasSrc() {
631 // Tests run in a limited file system and we do not
632 // provide access to every source file.
633 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
637 all, err := listStdPkgs(ctxt.GOROOT)
643 sawImport := map[string]map[string]bool{} // from package => to package => true
644 policy := depsPolicy(t)
646 for _, pkg := range all {
647 imports, err := findImports(pkg)
652 if sawImport[pkg] == nil {
653 sawImport[pkg] = map[string]bool{}
656 for _, imp := range imports {
657 sawImport[pkg][imp] = true
658 if !policy.HasEdge(pkg, imp) {
659 bad = append(bad, imp)
663 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
668 var buildIgnore = []byte("\n//go:build ignore")
670 func findImports(pkg string) ([]string, error) {
672 if strings.HasPrefix(pkg, "golang.org") {
673 vpkg = "vendor/" + pkg
675 dir := filepath.Join(Default.GOROOT, "src", vpkg)
676 files, err := os.ReadDir(dir)
681 var haveImport = map[string]bool{}
682 if pkg == "crypto/internal/boring" {
683 haveImport["C"] = true // kludge: prevent C from appearing in crypto/internal/boring imports
685 fset := token.NewFileSet()
686 for _, file := range files {
688 if name == "slice_go14.go" || name == "slice_go18.go" {
689 // These files are for compiler bootstrap with older versions of Go and not built in the standard build.
692 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
696 name: filepath.Join(dir, name),
699 f, err := os.Open(info.name)
703 err = readGoInfo(f, &info)
706 return nil, fmt.Errorf("reading %v: %v", name, err)
708 if info.parsed.Name.Name == "main" {
711 if bytes.Contains(info.header, buildIgnore) {
714 for _, imp := range info.imports {
716 if !haveImport[path] {
717 haveImport[path] = true
718 imports = append(imports, path)
722 sort.Strings(imports)
726 // depsPolicy returns a map m such that m[p][d] == true when p can import d.
727 func depsPolicy(t *testing.T) *dag.Graph {
728 g, err := dag.Parse(depsRules)
735 // TestStdlibLowercase tests that all standard library package names are
736 // lowercase. See Issue 40065.
737 func TestStdlibLowercase(t *testing.T) {
738 if !testenv.HasSrc() {
739 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
743 all, err := listStdPkgs(ctxt.GOROOT)
748 for _, pkgname := range all {
749 if strings.ToLower(pkgname) != pkgname {
750 t.Errorf("package %q should not use upper-case path", pkgname)
755 // TestFindImports tests that findImports works. See #43249.
756 func TestFindImports(t *testing.T) {
757 imports, err := findImports("go/build")
761 t.Logf("go/build imports %q", imports)
762 want := []string{"bytes", "os", "path/filepath", "strings"}
764 for _, w := range want {
765 for _, imp := range imports {
770 t.Errorf("expected to find %q in import list", w)