Source file
src/go/build/deps_test.go
1
2
3
4
5
6
7
8 package build
9
10 import (
11 "bytes"
12 "fmt"
13 "go/token"
14 "internal/dag"
15 "internal/testenv"
16 "io/fs"
17 "os"
18 "path/filepath"
19 "runtime"
20 "slices"
21 "strings"
22 "testing"
23 )
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 var depsRules = `
40 # No dependencies allowed for any of these packages.
41 NONE
42 < unsafe
43 < cmp,
44 container/list,
45 container/ring,
46 internal/byteorder,
47 internal/cfg,
48 internal/coverage,
49 internal/coverage/rtcov,
50 internal/coverage/uleb128,
51 internal/coverage/calloc,
52 internal/cpu,
53 internal/goarch,
54 internal/godebugs,
55 internal/goexperiment,
56 internal/goos,
57 internal/goversion,
58 internal/nettrace,
59 internal/platform,
60 internal/profilerecord,
61 internal/trace/traceviewer/format,
62 log/internal,
63 math/bits,
64 unicode,
65 unicode/utf8,
66 unicode/utf16;
67
68 internal/goarch < internal/abi;
69 internal/byteorder, internal/goarch < internal/chacha8rand;
70
71 # RUNTIME is the core runtime group of packages, all of them very light-weight.
72 internal/abi,
73 internal/chacha8rand,
74 internal/coverage/rtcov,
75 internal/cpu,
76 internal/goarch,
77 internal/godebugs,
78 internal/goexperiment,
79 internal/goos,
80 internal/profilerecord,
81 math/bits
82 < internal/bytealg
83 < internal/stringslite
84 < internal/itoa
85 < internal/unsafeheader
86 < runtime/internal/sys
87 < internal/runtime/syscall
88 < internal/runtime/atomic
89 < internal/runtime/exithook
90 < runtime/internal/math
91 < runtime
92 < sync/atomic
93 < internal/race
94 < internal/msan
95 < internal/asan
96 < internal/weak
97 < sync
98 < internal/bisect
99 < internal/godebug
100 < internal/reflectlite
101 < errors
102 < internal/oserror;
103
104 cmp, internal/race, math/bits
105 < iter
106 < maps, slices;
107
108 internal/oserror, maps, slices
109 < RUNTIME;
110
111 RUNTIME
112 < sort
113 < container/heap;
114
115 RUNTIME
116 < io;
117
118 RUNTIME
119 < arena;
120
121 syscall !< io;
122 reflect !< sort;
123
124 RUNTIME, unicode/utf8
125 < path;
126
127 unicode !< path;
128
129 # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
130 RUNTIME, unicode/utf8, unicode/utf16
131 < internal/syscall/windows/sysdll, syscall/js
132 < syscall
133 < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
134 < internal/syscall/execenv
135 < SYSCALL;
136
137 # TIME is SYSCALL plus the core packages about time, including context.
138 SYSCALL
139 < time/tzdata
140 < time
141 < context
142 < TIME;
143
144 TIME, io, path, slices
145 < io/fs;
146
147 # MATH is RUNTIME plus the basic math packages.
148 RUNTIME
149 < math
150 < MATH;
151
152 unicode !< math;
153
154 MATH
155 < math/cmplx;
156
157 MATH
158 < math/rand, math/rand/v2;
159
160 MATH
161 < runtime/metrics;
162
163 RUNTIME, math/rand/v2
164 < internal/concurrent;
165
166 MATH, unicode/utf8
167 < strconv;
168
169 unicode !< strconv;
170
171 # STR is basic string and buffer manipulation.
172 RUNTIME, io, unicode/utf8, unicode/utf16, unicode
173 < bytes, strings
174 < bufio;
175
176 bufio, path, strconv
177 < STR;
178
179 RUNTIME, internal/concurrent
180 < unique;
181
182 # OS is basic OS access, including helpers (path/filepath, os/exec, etc).
183 # OS includes string routines, but those must be layered above package os.
184 # OS does not include reflection.
185 io/fs
186 < internal/testlog
187 < internal/poll
188 < internal/filepathlite
189 < os
190 < os/signal;
191
192 io/fs
193 < embed;
194
195 unicode, fmt !< net, os, os/signal;
196
197 os/signal, internal/filepathlite, STR
198 < path/filepath
199 < io/ioutil;
200
201 path/filepath, internal/godebug < os/exec;
202
203 io/ioutil, os/exec, os/signal
204 < OS;
205
206 reflect !< OS;
207
208 OS
209 < golang.org/x/sys/cpu;
210
211 # FMT is OS (which includes string routines) plus reflect and fmt.
212 # It does not include package log, which should be avoided in core packages.
213 arena, strconv, unicode
214 < reflect;
215
216 os, reflect
217 < internal/fmtsort
218 < fmt;
219
220 OS, fmt
221 < FMT;
222
223 log !< FMT;
224
225 # Misc packages needing only FMT.
226 FMT
227 < html,
228 internal/dag,
229 internal/goroot,
230 internal/types/errors,
231 mime/quotedprintable,
232 net/internal/socktest,
233 net/url,
234 runtime/trace,
235 text/scanner,
236 text/tabwriter;
237
238 io, reflect
239 < internal/saferio;
240
241 # encodings
242 # core ones do not use fmt.
243 io, strconv, slices
244 < encoding;
245
246 encoding, reflect
247 < encoding/binary
248 < encoding/base32, encoding/base64;
249
250 FMT, encoding < flag;
251
252 fmt !< encoding/base32, encoding/base64;
253
254 FMT, encoding/base32, encoding/base64, internal/saferio
255 < encoding/ascii85, encoding/csv, encoding/gob, encoding/hex,
256 encoding/json, encoding/pem, encoding/xml, mime;
257
258 # hashes
259 io
260 < hash
261 < hash/adler32, hash/crc32, hash/crc64, hash/fnv;
262
263 # math/big
264 FMT, math/rand
265 < math/big;
266
267 # compression
268 FMT, encoding/binary, hash/adler32, hash/crc32, sort
269 < compress/bzip2, compress/flate, compress/lzw, internal/zstd
270 < archive/zip, compress/gzip, compress/zlib;
271
272 # templates
273 FMT
274 < text/template/parse;
275
276 net/url, text/template/parse
277 < text/template
278 < internal/lazytemplate;
279
280 # regexp
281 FMT, sort
282 < regexp/syntax
283 < regexp
284 < internal/lazyregexp;
285
286 encoding/json, html, text/template, regexp
287 < html/template;
288
289 # suffix array
290 encoding/binary, regexp
291 < index/suffixarray;
292
293 # executable parsing
294 FMT, encoding/binary, compress/zlib, internal/saferio, internal/zstd, sort
295 < runtime/debug
296 < debug/dwarf
297 < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
298 < debug/buildinfo
299 < DEBUG;
300
301 # go parser and friends.
302 FMT, sort
303 < internal/gover
304 < go/version
305 < go/token
306 < go/scanner
307 < go/ast
308 < go/internal/typeparams;
309
310 FMT
311 < go/build/constraint;
312
313 FMT, sort
314 < go/doc/comment;
315
316 go/internal/typeparams, go/build/constraint
317 < go/parser;
318
319 go/doc/comment, go/parser, text/tabwriter
320 < go/printer
321 < go/format;
322
323 math/big, go/token
324 < go/constant;
325
326 FMT, internal/goexperiment
327 < internal/buildcfg;
328
329 container/heap, go/constant, go/parser, internal/buildcfg, internal/goversion, internal/types/errors
330 < go/types;
331
332 # The vast majority of standard library packages should not be resorting to regexp.
333 # go/types is a good chokepoint. It shouldn't use regexp, nor should anything
334 # that is low-enough level to be used by go/types.
335 regexp !< go/types;
336
337 go/doc/comment, go/parser, internal/lazyregexp, text/template
338 < go/doc;
339
340 go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion, internal/platform
341 < go/build;
342
343 # databases
344 FMT
345 < database/sql/internal
346 < database/sql/driver;
347
348 database/sql/driver, math/rand/v2 < database/sql;
349
350 # images
351 FMT, compress/lzw, compress/zlib
352 < image/color
353 < image, image/color/palette
354 < image/internal/imageutil
355 < image/draw
356 < image/gif, image/jpeg, image/png;
357
358 # cgo, delayed as long as possible.
359 # If you add a dependency on CGO, you must add the package
360 # to cgoPackages in cmd/dist/test.go as well.
361 RUNTIME
362 < C
363 < runtime/cgo
364 < CGO
365 < runtime/msan, runtime/asan;
366
367 # runtime/race
368 NONE < runtime/race/internal/amd64v1;
369 NONE < runtime/race/internal/amd64v3;
370 CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
371
372 # Bulk of the standard library must not use cgo.
373 # The prohibition stops at net and os/user.
374 C !< fmt, go/types, CRYPTO-MATH, log/slog;
375
376 CGO, OS
377 < plugin;
378
379 CGO, FMT
380 < os/user
381 < archive/tar;
382
383 sync
384 < internal/singleflight;
385
386 os
387 < golang.org/x/net/dns/dnsmessage,
388 golang.org/x/net/lif,
389 golang.org/x/net/route;
390
391 internal/bytealg, internal/itoa, math/bits, slices, strconv, unique
392 < net/netip;
393
394 # net is unavoidable when doing any networking,
395 # so large dependencies must be kept out.
396 # This is a long-looking list but most of these
397 # are small with few dependencies.
398 CGO,
399 golang.org/x/net/dns/dnsmessage,
400 golang.org/x/net/lif,
401 golang.org/x/net/route,
402 internal/godebug,
403 internal/nettrace,
404 internal/poll,
405 internal/singleflight,
406 net/netip,
407 os,
408 sort
409 < net;
410
411 fmt, unicode !< net;
412 math/rand !< net; # net uses runtime instead
413
414 # NET is net plus net-helper packages.
415 FMT, net
416 < net/textproto;
417
418 mime, net/textproto, net/url
419 < NET;
420
421 # logging - most packages should not import; http and up is allowed
422 FMT, log/internal
423 < log;
424
425 log, log/slog !< crypto/tls, database/sql, go/importer, testing;
426
427 FMT, log, net
428 < log/syslog;
429
430 RUNTIME
431 < log/slog/internal, log/slog/internal/buffer;
432
433 FMT,
434 encoding, encoding/json,
435 log, log/internal,
436 log/slog/internal, log/slog/internal/buffer,
437 slices
438 < log/slog
439 < log/slog/internal/slogtest, log/slog/internal/benchmarks;
440
441 NET, log
442 < net/mail;
443
444 NONE < crypto/internal/boring/sig, crypto/internal/boring/syso;
445 sync/atomic < crypto/internal/boring/bcache, crypto/internal/boring/fipstls;
446 crypto/internal/boring/sig, crypto/internal/boring/fipstls < crypto/tls/fipsonly;
447
448 # CRYPTO is core crypto algorithms - no cgo, fmt, net.
449 crypto/internal/boring/sig,
450 crypto/internal/boring/syso,
451 golang.org/x/sys/cpu,
452 hash, embed
453 < crypto
454 < crypto/subtle
455 < crypto/internal/alias
456 < crypto/cipher;
457
458 crypto/cipher,
459 crypto/internal/boring/bcache
460 < crypto/internal/boring
461 < crypto/boring;
462
463 crypto/internal/alias
464 < crypto/internal/randutil
465 < crypto/internal/nistec/fiat
466 < crypto/internal/nistec
467 < crypto/internal/edwards25519/field
468 < crypto/internal/edwards25519;
469
470 crypto/boring
471 < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
472 crypto/sha1, crypto/sha256, crypto/sha512;
473
474 crypto/boring, crypto/internal/edwards25519/field
475 < crypto/ecdh;
476
477 # Unfortunately, stuck with reflect via encoding/binary.
478 encoding/binary, crypto/boring < golang.org/x/crypto/sha3;
479
480 crypto/aes,
481 crypto/des,
482 crypto/ecdh,
483 crypto/hmac,
484 crypto/internal/edwards25519,
485 crypto/md5,
486 crypto/rc4,
487 crypto/sha1,
488 crypto/sha256,
489 crypto/sha512,
490 golang.org/x/crypto/sha3
491 < CRYPTO;
492
493 CGO, fmt, net !< CRYPTO;
494
495 # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
496 CRYPTO, FMT, math/big
497 < crypto/internal/boring/bbig
498 < crypto/rand
499 < crypto/internal/mlkem768
500 < crypto/ed25519
501 < encoding/asn1
502 < golang.org/x/crypto/cryptobyte/asn1
503 < golang.org/x/crypto/cryptobyte
504 < crypto/internal/bigmod
505 < crypto/dsa, crypto/elliptic, crypto/rsa
506 < crypto/ecdsa
507 < CRYPTO-MATH;
508
509 CGO, net !< CRYPTO-MATH;
510
511 # TLS, Prince of Dependencies.
512 CRYPTO-MATH, NET, container/list, encoding/hex, encoding/pem
513 < golang.org/x/crypto/internal/alias
514 < golang.org/x/crypto/internal/subtle
515 < golang.org/x/crypto/chacha20
516 < golang.org/x/crypto/internal/poly1305
517 < golang.org/x/crypto/chacha20poly1305
518 < golang.org/x/crypto/hkdf
519 < crypto/internal/hpke
520 < crypto/x509/internal/macos
521 < crypto/x509/pkix;
522
523 crypto/internal/boring/fipstls, crypto/x509/pkix
524 < crypto/x509
525 < crypto/tls;
526
527 # crypto-aware packages
528
529 DEBUG, go/build, go/types, text/scanner, crypto/md5
530 < internal/pkgbits
531 < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
532 < go/importer;
533
534 NET, crypto/rand, mime/quotedprintable
535 < mime/multipart;
536
537 crypto/tls
538 < net/smtp;
539
540 crypto/rand
541 < hash/maphash; # for purego implementation
542
543 # HTTP, King of Dependencies.
544
545 FMT
546 < golang.org/x/net/http2/hpack
547 < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
548
549 FMT, NET, container/list, encoding/binary, log
550 < golang.org/x/text/transform
551 < golang.org/x/text/unicode/norm
552 < golang.org/x/text/unicode/bidi
553 < golang.org/x/text/secure/bidirule
554 < golang.org/x/net/idna
555 < golang.org/x/net/http/httpguts, golang.org/x/net/http/httpproxy;
556
557 NET, crypto/tls
558 < net/http/httptrace;
559
560 compress/gzip,
561 golang.org/x/net/http/httpguts,
562 golang.org/x/net/http/httpproxy,
563 golang.org/x/net/http2/hpack,
564 net/http/internal,
565 net/http/internal/ascii,
566 net/http/internal/testcert,
567 net/http/httptrace,
568 mime/multipart,
569 log
570 < net/http;
571
572 # HTTP-aware packages
573
574 encoding/json, net/http
575 < expvar;
576
577 net/http, net/http/internal/ascii
578 < net/http/cookiejar, net/http/httputil;
579
580 net/http, flag
581 < net/http/httptest;
582
583 net/http, regexp
584 < net/http/cgi
585 < net/http/fcgi;
586
587 # Profiling
588 FMT, compress/gzip, encoding/binary, sort, text/tabwriter
589 < runtime/pprof;
590
591 OS, compress/gzip, internal/lazyregexp
592 < internal/profile;
593
594 html, internal/profile, net/http, runtime/pprof, runtime/trace
595 < net/http/pprof;
596
597 # RPC
598 encoding/gob, encoding/json, go/token, html/template, net/http
599 < net/rpc
600 < net/rpc/jsonrpc;
601
602 # System Information
603 bufio, bytes, internal/cpu, io, os, strings, sync
604 < internal/sysinfo;
605
606 # Test-only
607 log
608 < testing/iotest
609 < testing/fstest;
610
611 FMT, flag, math/rand
612 < testing/quick;
613
614 FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
615 < testing;
616
617 log/slog, testing
618 < testing/slogtest;
619
620 FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
621 internal/godebug, math/rand, encoding/hex, crypto/sha256
622 < internal/fuzz;
623
624 OS, flag, testing, internal/cfg, internal/platform, internal/goroot
625 < internal/testenv;
626
627 OS, encoding/base64
628 < internal/obscuretestdata;
629
630 CGO, OS, fmt
631 < internal/testpty;
632
633 NET, testing, math/rand
634 < golang.org/x/net/nettest;
635
636 syscall
637 < os/exec/internal/fdtest;
638
639 FMT, sort
640 < internal/diff;
641
642 FMT
643 < internal/txtar;
644
645 CRYPTO-MATH, testing
646 < crypto/internal/cryptotest;
647
648 # v2 execution trace parser.
649 FMT
650 < internal/trace/event;
651
652 internal/trace/event
653 < internal/trace/event/go122;
654
655 FMT, io, internal/trace/event/go122
656 < internal/trace/version;
657
658 FMT, encoding/binary, internal/trace/version
659 < internal/trace/raw;
660
661 FMT, internal/trace/event, internal/trace/version, io, sort, encoding/binary
662 < internal/trace/internal/oldtrace;
663
664 FMT, encoding/binary, internal/trace/version, internal/trace/internal/oldtrace, container/heap, math/rand
665 < internal/trace;
666
667 regexp, internal/trace, internal/trace/raw, internal/txtar
668 < internal/trace/testtrace;
669
670 regexp, internal/txtar, internal/trace, internal/trace/raw
671 < internal/trace/internal/testgen/go122;
672
673 # cmd/trace dependencies.
674 FMT,
675 embed,
676 encoding/json,
677 html/template,
678 internal/profile,
679 internal/trace,
680 internal/trace/traceviewer/format,
681 net/http
682 < internal/trace/traceviewer;
683
684 # Coverage.
685 FMT, crypto/md5, encoding/binary, regexp, sort, text/tabwriter,
686 internal/coverage, internal/coverage/uleb128
687 < internal/coverage/cmerge,
688 internal/coverage/pods,
689 internal/coverage/slicereader,
690 internal/coverage/slicewriter;
691
692 internal/coverage/slicereader, internal/coverage/slicewriter
693 < internal/coverage/stringtab
694 < internal/coverage/decodecounter, internal/coverage/decodemeta,
695 internal/coverage/encodecounter, internal/coverage/encodemeta;
696
697 internal/coverage/cmerge
698 < internal/coverage/cformat;
699
700 internal/coverage, crypto/sha256, FMT
701 < cmd/internal/cov/covcmd;
702
703 encoding/json,
704 runtime/debug,
705 internal/coverage/calloc,
706 internal/coverage/cformat,
707 internal/coverage/decodecounter, internal/coverage/decodemeta,
708 internal/coverage/encodecounter, internal/coverage/encodemeta,
709 internal/coverage/pods
710 < internal/coverage/cfile
711 < runtime/coverage;
712
713 internal/coverage/cfile, internal/fuzz, internal/testlog, runtime/pprof, regexp
714 < testing/internal/testdeps;
715
716 # Test-only packages can have anything they want
717 CGO, internal/syscall/unix < net/internal/cgotest;
718
719
720 `
721
722
723 func listStdPkgs(goroot string) ([]string, error) {
724
725 var pkgs []string
726
727 src := filepath.Join(goroot, "src") + string(filepath.Separator)
728 walkFn := func(path string, d fs.DirEntry, err error) error {
729 if err != nil || !d.IsDir() || path == src {
730 return nil
731 }
732
733 base := filepath.Base(path)
734 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
735 return filepath.SkipDir
736 }
737
738 name := filepath.ToSlash(path[len(src):])
739 if name == "builtin" || name == "cmd" {
740 return filepath.SkipDir
741 }
742
743 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
744 return nil
745 }
746 if err := filepath.WalkDir(src, walkFn); err != nil {
747 return nil, err
748 }
749 return pkgs, nil
750 }
751
752 func TestDependencies(t *testing.T) {
753 if !testenv.HasSrc() {
754
755
756 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
757 }
758
759 ctxt := Default
760 all, err := listStdPkgs(ctxt.GOROOT)
761 if err != nil {
762 t.Fatal(err)
763 }
764 slices.Sort(all)
765
766 sawImport := map[string]map[string]bool{}
767 policy := depsPolicy(t)
768
769 for _, pkg := range all {
770 imports, err := findImports(pkg)
771 if err != nil {
772 t.Error(err)
773 continue
774 }
775 if sawImport[pkg] == nil {
776 sawImport[pkg] = map[string]bool{}
777 }
778 var bad []string
779 for _, imp := range imports {
780 sawImport[pkg][imp] = true
781 if !policy.HasEdge(pkg, imp) {
782 bad = append(bad, imp)
783 }
784 }
785 if bad != nil {
786 t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
787 }
788 }
789 }
790
791 var buildIgnore = []byte("\n//go:build ignore")
792
793 func findImports(pkg string) ([]string, error) {
794 vpkg := pkg
795 if strings.HasPrefix(pkg, "golang.org") {
796 vpkg = "vendor/" + pkg
797 }
798 dir := filepath.Join(Default.GOROOT, "src", vpkg)
799 files, err := os.ReadDir(dir)
800 if err != nil {
801 return nil, err
802 }
803 var imports []string
804 var haveImport = map[string]bool{}
805 if pkg == "crypto/internal/boring" {
806 haveImport["C"] = true
807 }
808 fset := token.NewFileSet()
809 for _, file := range files {
810 name := file.Name()
811 if name == "slice_go14.go" || name == "slice_go18.go" {
812
813 continue
814 }
815 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
816 continue
817 }
818 info := fileInfo{
819 name: filepath.Join(dir, name),
820 fset: fset,
821 }
822 f, err := os.Open(info.name)
823 if err != nil {
824 return nil, err
825 }
826 err = readGoInfo(f, &info)
827 f.Close()
828 if err != nil {
829 return nil, fmt.Errorf("reading %v: %v", name, err)
830 }
831 if info.parsed.Name.Name == "main" {
832 continue
833 }
834 if bytes.Contains(info.header, buildIgnore) {
835 continue
836 }
837 for _, imp := range info.imports {
838 path := imp.path
839 if !haveImport[path] {
840 haveImport[path] = true
841 imports = append(imports, path)
842 }
843 }
844 }
845 slices.Sort(imports)
846 return imports, nil
847 }
848
849
850 func depsPolicy(t *testing.T) *dag.Graph {
851 g, err := dag.Parse(depsRules)
852 if err != nil {
853 t.Fatal(err)
854 }
855 return g
856 }
857
858
859
860 func TestStdlibLowercase(t *testing.T) {
861 if !testenv.HasSrc() {
862 t.Skipf("skipping on %s/%s, missing full GOROOT", runtime.GOOS, runtime.GOARCH)
863 }
864
865 ctxt := Default
866 all, err := listStdPkgs(ctxt.GOROOT)
867 if err != nil {
868 t.Fatal(err)
869 }
870
871 for _, pkgname := range all {
872 if strings.ToLower(pkgname) != pkgname {
873 t.Errorf("package %q should not use upper-case path", pkgname)
874 }
875 }
876 }
877
878
879 func TestFindImports(t *testing.T) {
880 imports, err := findImports("go/build")
881 if err != nil {
882 t.Fatal(err)
883 }
884 t.Logf("go/build imports %q", imports)
885 want := []string{"bytes", "os", "path/filepath", "strings"}
886 wantLoop:
887 for _, w := range want {
888 for _, imp := range imports {
889 if imp == w {
890 continue wantLoop
891 }
892 }
893 t.Errorf("expected to find %q in import list", w)
894 }
895 }
896
View as plain text