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