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