]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/build/deps_test.go
go/build: use tabs consistently in deps test rules
[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, internal/platform,
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         RUNTIME
82         < arena;
83
84         syscall !< io;
85         reflect !< sort;
86
87         RUNTIME, unicode/utf8
88         < path;
89
90         unicode !< path;
91
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
95         < syscall
96         < internal/syscall/unix, internal/syscall/windows, internal/syscall/windows/registry
97         < internal/syscall/execenv
98         < SYSCALL;
99
100         # TIME is SYSCALL plus the core packages about time, including context.
101         SYSCALL
102         < time/tzdata
103         < time
104         < context
105         < TIME;
106
107         TIME, io, path, sort
108         < io/fs;
109
110         # MATH is RUNTIME plus the basic math packages.
111         RUNTIME
112         < math
113         < MATH;
114
115         unicode !< math;
116
117         MATH
118         < math/cmplx;
119
120         MATH
121         < math/rand;
122
123         MATH
124         < runtime/metrics;
125
126         MATH, unicode/utf8
127         < strconv;
128
129         unicode !< strconv;
130
131         # STR is basic string and buffer manipulation.
132         RUNTIME, io, unicode/utf8, unicode/utf16, unicode
133         < bytes, strings
134         < bufio;
135
136         bufio, path, strconv
137         < STR;
138
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.
142         io/fs
143         < internal/testlog
144         < internal/poll
145         < os
146         < os/signal;
147
148         io/fs
149         < embed;
150
151         unicode, fmt !< net, os, os/signal;
152
153         os/signal, STR
154         < path/filepath
155         < io/ioutil;
156
157         os < internal/godebug;
158
159         path/filepath, internal/godebug < os/exec;
160
161         io/ioutil, os/exec, os/signal
162         < OS;
163
164         reflect !< OS;
165
166         OS
167         < golang.org/x/sys/cpu;
168
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
172         < reflect;
173
174         os, reflect
175         < internal/fmtsort
176         < fmt;
177
178         OS, fmt
179         < FMT;
180
181         log !< FMT;
182
183         # Misc packages needing only FMT.
184         FMT
185         < html,
186           internal/dag,
187           internal/goroot,
188           internal/types/errors,
189           mime/quotedprintable,
190           net/internal/socktest,
191           net/url,
192           runtime/trace,
193           text/scanner,
194           text/tabwriter;
195
196         io, reflect
197         < internal/saferio;
198
199         # encodings
200         # core ones do not use fmt.
201         io, strconv
202         < encoding;
203
204         encoding, reflect
205         < encoding/binary
206         < encoding/base32, encoding/base64;
207
208         FMT, encoding < flag;
209
210         fmt !< encoding/base32, encoding/base64;
211
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;
215
216         # hashes
217         io
218         < hash
219         < hash/adler32, hash/crc32, hash/crc64, hash/fnv, hash/maphash;
220
221         # math/big
222         FMT, encoding/binary, math/rand
223         < math/big;
224
225         # compression
226         FMT, encoding/binary, hash/adler32, hash/crc32
227         < compress/bzip2, compress/flate, compress/lzw
228         < archive/zip, compress/gzip, compress/zlib;
229
230         # templates
231         FMT
232         < text/template/parse;
233
234         net/url, text/template/parse
235         < text/template
236         < internal/lazytemplate;
237
238         encoding/json, html, text/template
239         < html/template;
240
241         # regexp
242         FMT
243         < regexp/syntax
244         < regexp
245         < internal/lazyregexp;
246
247         # suffix array
248         encoding/binary, regexp
249         < index/suffixarray;
250
251         # executable parsing
252         FMT, encoding/binary, compress/zlib, internal/saferio
253         < runtime/debug
254         < debug/dwarf
255         < debug/elf, debug/gosym, debug/macho, debug/pe, debug/plan9obj, internal/xcoff
256         < debug/buildinfo
257         < DEBUG;
258
259         # go parser and friends.
260         FMT
261         < go/token
262         < go/scanner
263         < go/ast
264         < go/internal/typeparams
265         < go/parser;
266
267         FMT
268         < go/build/constraint, go/doc/comment;
269
270         go/build/constraint, go/doc/comment, go/parser, text/tabwriter
271         < go/printer
272         < go/format;
273
274         go/doc/comment, go/parser, internal/lazyregexp, text/template
275         < go/doc;
276
277         math/big, go/token
278         < go/constant;
279
280         container/heap, go/constant, go/parser, internal/types/errors, regexp
281         < go/types;
282
283         FMT, internal/goexperiment
284         < internal/buildcfg;
285
286         go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion
287         < go/build;
288
289         # databases
290         FMT
291         < database/sql/internal
292         < database/sql/driver
293         < database/sql;
294
295         # images
296         FMT, compress/lzw, compress/zlib
297         < image/color
298         < image, image/color/palette
299         < image/internal/imageutil
300         < image/draw
301         < image/gif, image/jpeg, image/png;
302
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.
306         RUNTIME
307         < C
308         < runtime/cgo
309         < CGO
310         < runtime/msan, runtime/asan;
311
312         # runtime/race
313         NONE < runtime/race/internal/amd64v1;
314         NONE < runtime/race/internal/amd64v3;
315         CGO, runtime/race/internal/amd64v1, runtime/race/internal/amd64v3 < runtime/race;
316
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;
320
321         CGO, OS
322         < plugin;
323
324         CGO, FMT
325         < os/user
326         < archive/tar;
327
328         sync
329         < internal/singleflight;
330
331         os
332         < golang.org/x/net/dns/dnsmessage,
333           golang.org/x/net/lif,
334           golang.org/x/net/route;
335
336         os, runtime, strconv, sync, unsafe,
337         internal/godebug
338         < internal/intern;
339
340         internal/bytealg, internal/intern, internal/itoa, math/bits, sort, strconv
341         < net/netip;
342
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.
347         CGO,
348         golang.org/x/net/dns/dnsmessage,
349         golang.org/x/net/lif,
350         golang.org/x/net/route,
351         internal/godebug,
352         internal/nettrace,
353         internal/poll,
354         internal/singleflight,
355         internal/race,
356         net/netip,
357         os
358         < net;
359
360         fmt, unicode !< net;
361         math/rand !< net; # net uses runtime instead
362
363         # NET is net plus net-helper packages.
364         FMT, net
365         < net/textproto;
366
367         mime, net/textproto, net/url
368         < NET;
369
370         # logging - most packages should not import; http and up is allowed
371         FMT
372         < log;
373
374         log !< crypto/tls, database/sql, go/importer, testing;
375
376         FMT, log, net
377         < log/syslog;
378
379         NET, log
380         < net/mail;
381
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;
385
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,
390         encoding/binary,
391         golang.org/x/sys/cpu,
392         hash, embed
393         < crypto
394         < crypto/subtle
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
401         < crypto/cipher;
402
403         crypto/cipher,
404         crypto/internal/boring/bcache
405         < crypto/internal/boring
406         < crypto/boring
407         < crypto/aes, crypto/des, crypto/hmac, crypto/md5, crypto/rc4,
408           crypto/sha1, crypto/sha256, crypto/sha512
409         < CRYPTO;
410
411         CGO, fmt, net !< CRYPTO;
412
413         # CRYPTO-MATH is core bignum-based crypto - no cgo, net; fmt now ok.
414         CRYPTO, FMT, math/big
415         < crypto/internal/boring/bbig
416         < crypto/rand
417         < crypto/ed25519
418         < encoding/asn1
419         < golang.org/x/crypto/cryptobyte/asn1
420         < golang.org/x/crypto/cryptobyte
421         < crypto/dsa, crypto/elliptic, crypto/rsa
422         < crypto/ecdsa
423         < CRYPTO-MATH;
424
425         CGO, net !< CRYPTO-MATH;
426
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
436         < crypto/x509/pkix;
437
438         crypto/internal/boring/fipstls, crypto/x509/pkix
439         < crypto/x509
440         < crypto/tls;
441
442         # crypto-aware packages
443
444         DEBUG, go/build, go/types, text/scanner, crypto/md5
445         < internal/pkgbits
446         < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
447         < go/importer;
448
449         NET, crypto/rand, mime/quotedprintable
450         < mime/multipart;
451
452         crypto/tls
453         < net/smtp;
454
455         # HTTP, King of Dependencies.
456
457         FMT
458         < golang.org/x/net/http2/hpack
459         < net/http/internal, net/http/internal/ascii, net/http/internal/testcert;
460
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;
468
469         NET, crypto/tls
470         < net/http/httptrace;
471
472         compress/gzip,
473         golang.org/x/net/http/httpguts,
474         golang.org/x/net/http/httpproxy,
475         golang.org/x/net/http2/hpack,
476         net/http/internal,
477         net/http/internal/ascii,
478         net/http/internal/testcert,
479         net/http/httptrace,
480         mime/multipart,
481         log
482         < net/http;
483
484         # HTTP-aware packages
485
486         encoding/json, net/http
487         < expvar;
488
489         net/http, net/http/internal/ascii
490         < net/http/cookiejar, net/http/httputil;
491
492         net/http, flag
493         < net/http/httptest;
494
495         net/http, regexp
496         < net/http/cgi
497         < net/http/fcgi;
498
499         # Profiling
500         FMT, compress/gzip, encoding/binary, text/tabwriter
501         < runtime/pprof;
502
503         OS, compress/gzip, regexp
504         < internal/profile;
505
506         html, internal/profile, net/http, runtime/pprof, runtime/trace
507         < net/http/pprof;
508
509         # RPC
510         encoding/gob, encoding/json, go/token, html/template, net/http
511         < net/rpc
512         < net/rpc/jsonrpc;
513
514         # System Information
515         internal/cpu, sync
516         < internal/sysinfo;
517
518         # Test-only
519         log
520         < testing/iotest
521         < testing/fstest;
522
523         FMT, flag, math/rand
524         < testing/quick;
525
526         FMT, DEBUG, flag, runtime/trace, internal/sysinfo, math/rand
527         < testing;
528
529         FMT, crypto/sha256, encoding/json, go/ast, go/parser, go/token,
530         internal/godebug, math/rand, encoding/hex, crypto/sha256
531         < internal/fuzz;
532
533         internal/fuzz, internal/testlog, runtime/pprof, regexp
534         < testing/internal/testdeps;
535
536         OS, flag, testing, internal/cfg, internal/platform
537         < internal/testenv;
538
539         OS, encoding/base64
540         < internal/obscuretestdata;
541
542         CGO, OS, fmt
543         < os/signal/internal/pty;
544
545         NET, testing, math/rand
546         < golang.org/x/net/nettest;
547
548         syscall
549         < os/exec/internal/fdtest;
550
551         FMT, container/heap, math/rand
552         < internal/trace;
553
554         FMT
555         < internal/diff, internal/txtar;
556
557         FMT, os
558         < internal/coverage/slicewriter;
559
560         encoding/binary, internal/unsafeheader, unsafe
561         < internal/coverage/slicereader;
562
563         FMT, math, internal/coverage
564         < internal/coverage/cmerge;
565
566         FMT, math, internal/coverage, internal/coverage/cmerge, text/tabwriter
567         < internal/coverage/cformat;
568
569         FMT, io, internal/coverage/slicereader, internal/coverage/uleb128
570         < internal/coverage/stringtab;
571
572         FMT, encoding/binary, internal/coverage, internal/coverage/stringtab,
573         io, os, bufio, crypto/md5
574         < internal/coverage/encodemeta;
575
576         FMT, bufio, encoding/binary, internal/coverage,
577         internal/coverage/stringtab, internal/coverage/slicewriter, os, unsafe
578         < internal/coverage/encodecounter;
579
580         FMT, encoding/binary, internal/coverage, io, os,
581         internal/coverage/slicereader, internal/coverage/stringtab
582         < internal/coverage/decodecounter;
583
584         FMT, encoding/binary, internal/coverage, io, os,
585         crypto/md5, internal/coverage/stringtab
586         < internal/coverage/decodemeta;
587
588         FMT, internal/coverage, os,
589         path/filepath, regexp, sort, strconv
590         < internal/coverage/pods;
591
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
598         < runtime/coverage;
599 `
600
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.
604         var pkgs []string
605
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 {
609                         return nil
610                 }
611
612                 base := filepath.Base(path)
613                 if strings.HasPrefix(base, ".") || strings.HasPrefix(base, "_") || base == "testdata" {
614                         return filepath.SkipDir
615                 }
616
617                 name := filepath.ToSlash(path[len(src):])
618                 if name == "builtin" || name == "cmd" {
619                         return filepath.SkipDir
620                 }
621
622                 pkgs = append(pkgs, strings.TrimPrefix(name, "vendor/"))
623                 return nil
624         }
625         if err := filepath.WalkDir(src, walkFn); err != nil {
626                 return nil, err
627         }
628         return pkgs, nil
629 }
630
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)
636         }
637
638         ctxt := Default
639         all, err := listStdPkgs(ctxt.GOROOT)
640         if err != nil {
641                 t.Fatal(err)
642         }
643         sort.Strings(all)
644
645         sawImport := map[string]map[string]bool{} // from package => to package => true
646         policy := depsPolicy(t)
647
648         for _, pkg := range all {
649                 imports, err := findImports(pkg)
650                 if err != nil {
651                         t.Error(err)
652                         continue
653                 }
654                 if sawImport[pkg] == nil {
655                         sawImport[pkg] = map[string]bool{}
656                 }
657                 var bad []string
658                 for _, imp := range imports {
659                         sawImport[pkg][imp] = true
660                         if !policy.HasEdge(pkg, imp) {
661                                 bad = append(bad, imp)
662                         }
663                 }
664                 if bad != nil {
665                         t.Errorf("unexpected dependency: %s imports %v", pkg, bad)
666                 }
667         }
668 }
669
670 var buildIgnore = []byte("\n//go:build ignore")
671
672 func findImports(pkg string) ([]string, error) {
673         vpkg := pkg
674         if strings.HasPrefix(pkg, "golang.org") {
675                 vpkg = "vendor/" + pkg
676         }
677         dir := filepath.Join(Default.GOROOT, "src", vpkg)
678         files, err := os.ReadDir(dir)
679         if err != nil {
680                 return nil, err
681         }
682         var imports []string
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
686         }
687         fset := token.NewFileSet()
688         for _, file := range files {
689                 name := file.Name()
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.
692                         continue
693                 }
694                 if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
695                         continue
696                 }
697                 info := fileInfo{
698                         name: filepath.Join(dir, name),
699                         fset: fset,
700                 }
701                 f, err := os.Open(info.name)
702                 if err != nil {
703                         return nil, err
704                 }
705                 err = readGoInfo(f, &info)
706                 f.Close()
707                 if err != nil {
708                         return nil, fmt.Errorf("reading %v: %v", name, err)
709                 }
710                 if info.parsed.Name.Name == "main" {
711                         continue
712                 }
713                 if bytes.Contains(info.header, buildIgnore) {
714                         continue
715                 }
716                 for _, imp := range info.imports {
717                         path := imp.path
718                         if !haveImport[path] {
719                                 haveImport[path] = true
720                                 imports = append(imports, path)
721                         }
722                 }
723         }
724         sort.Strings(imports)
725         return imports, nil
726 }
727
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)
731         if err != nil {
732                 t.Fatal(err)
733         }
734         return g
735 }
736
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)
742         }
743
744         ctxt := Default
745         all, err := listStdPkgs(ctxt.GOROOT)
746         if err != nil {
747                 t.Fatal(err)
748         }
749
750         for _, pkgname := range all {
751                 if strings.ToLower(pkgname) != pkgname {
752                         t.Errorf("package %q should not use upper-case path", pkgname)
753                 }
754         }
755 }
756
757 // TestFindImports tests that findImports works.  See #43249.
758 func TestFindImports(t *testing.T) {
759         imports, err := findImports("go/build")
760         if err != nil {
761                 t.Fatal(err)
762         }
763         t.Logf("go/build imports %q", imports)
764         want := []string{"bytes", "os", "path/filepath", "strings"}
765 wantLoop:
766         for _, w := range want {
767                 for _, imp := range imports {
768                         if imp == w {
769                                 continue wantLoop
770                         }
771                 }
772                 t.Errorf("expected to find %q in import list", w)
773         }
774 }