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