1
2
3
4
5 package work
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 import (
82 "bytes"
83 "context"
84 "crypto/sha256"
85 "encoding/json"
86 "errors"
87 "fmt"
88 "internal/analysis/gotestchecker/protocol"
89 "maps"
90 "os"
91 "path/filepath"
92 "runtime"
93 "slices"
94 "strings"
95
96 "cmd/go/internal/cache"
97 "cmd/go/internal/cfg"
98 "cmd/go/internal/load"
99 "cmd/go/internal/modload"
100 "cmd/go/internal/str"
101 )
102
103
104
105 type analysisResult struct {
106 types analysisOutput
107 typeErrs []string
108 passes map[int]analysisOutput
109 }
110
111
112
113 type analysisOutput struct {
114 file string
115 id cache.OutputID
116 factsID cache.OutputID
117 trivial bool
118 failure *protocol.Failure
119 }
120
121
122
123
124 func analysisResultOf(a *Action) *analysisResult {
125 res, _ := a.Provider.(*analysisResult)
126 return res
127 }
128
129
130
131
132
133 type analysisState struct {
134 actions []*Action
135 roots []*Action
136 rootConfig map[*Action]*load.AnalysisConfig
137 tool *Action
138 meta *Action
139
140
141
142 toolPath, toolID string
143
144
145
146 plan *analysisPlan
147 }
148
149
150
151 type analysisPlan struct {
152 analyzers []analyzerInfo
153 driverID cache.ActionID
154 need map[*Action][]int
155 reported map[*Action]map[int]*reportedAnalyzer
156 refErrs map[*Action][]string
157 }
158
159
160
161 type analyzerInfo struct {
162 *protocol.Analyzer
163 reqClosure []int
164 id cache.ActionID
165 }
166
167
168
169
170 type reportedAnalyzer struct {
171 name string
172
173
174 }
175
176
177
178
179
180
181
182
183
184
185
186
187
188 func (b *Builder) AnalysisVetAction(s *modload.Loader, p *load.Package, config *load.AnalysisConfig) *Action {
189 if config == nil {
190 panic("nil config")
191 }
192 a := b.analysisVetAction(s, p)
193 st := &b.analysis
194 if _, ok := st.rootConfig[a]; !ok {
195 st.roots = append(st.roots, a)
196 if st.rootConfig == nil {
197 st.rootConfig = make(map[*Action]*load.AnalysisConfig)
198 }
199 }
200 st.rootConfig[a] = config
201 return &Action{
202 Mode: "analysisreport",
203 Package: p,
204 Deps: []*Action{a},
205 Actor: ActorFunc((*Builder).analysisReport),
206 IgnoreFail: true,
207 }
208 }
209
210
211
212
213
214 func (b *Builder) analysisVetAction(s *modload.Loader, p *load.Package) *Action {
215 return b.cacheAction("analysisvet", p, func() *Action {
216 a1 := b.BuildExportAction(ModeBuild, ModeBuild, p)
217 deps := []*Action{a1}
218 for _, p1 := range p.Internal.Imports {
219 deps = append(deps, b.analysisVetAction(s, p1))
220 }
221 a := &Action{
222 Mode: "analysisvet",
223 Package: p,
224 Deps: deps,
225 Objdir: b.NewObjdir(),
226 VetxOnly: true,
227 IgnoreFail: true,
228 }
229 b.analysis.actions = append(b.analysis.actions, a)
230 if a1.Actor == nil {
231
232 return a
233 }
234 if p.UsesCgo() || p.UsesSwig() {
235 a.Deps = append(a.Deps, b.runCgoAction(p, false))
236 }
237 a.Actor = ActorFunc((*Builder).analysisVet)
238 b.addTransitiveCompileActions(a, a1)
239 return a
240 })
241 }
242
243
244
245
246 func (b *Builder) SetAnalysisTool(link *Action) {
247 st := &b.analysis
248 st.tool = link
249 b.setAnalysisMeta(&Action{
250 Mode: "analysismeta",
251 Deps: []*Action{link},
252 Objdir: b.NewObjdir(),
253 Actor: ActorFunc((*Builder).analysisMeta),
254 })
255 }
256
257
258
259 func (b *Builder) setAnalysisMeta(meta *Action) {
260 st := &b.analysis
261 st.meta = meta
262 for _, a := range st.actions {
263 if a.Actor != nil {
264 a.Deps = append(a.Deps, meta)
265 }
266 }
267 }
268
269
270
271
272
273
274
275
276 func (b *Builder) prioritizeAnalysisTool(all []*Action) {
277 if meta := b.analysis.meta; meta != nil {
278 for _, a := range actionList(meta) {
279
280
281 a.priority -= len(all)
282 }
283 }
284 }
285
286
287 func (b *Builder) HasAnalysisActions() bool { return len(b.analysis.actions) > 0 }
288
289
290
291 func (b *Builder) analysisMeta(ctx context.Context, a *Action) error {
292 st := &b.analysis
293 st.toolPath = st.tool.BuiltTarget()
294 st.toolID = st.tool.buildID
295 data, err := b.analysisMetadata(a)
296 if err != nil || data == nil {
297 return err
298 }
299 md := new(protocol.Metadata)
300 if err := json.Unmarshal(data, md); err != nil {
301 return fmt.Errorf("decoding analysis tool metadata: %v", err)
302 }
303 driverID, ids := b.analyzerIDs(md)
304 b.planAnalysis(md, driverID, ids)
305 return nil
306 }
307
308
309
310
311
312 func (b *Builder) analysisMetadata(a *Action) ([]byte, error) {
313 st := &b.analysis
314 c := cache.Default()
315 h := cache.NewHash("analysis metadata")
316 fmt.Fprintf(h, "tool %s\n", st.toolID)
317 key := h.Sum()
318
319 if !cfg.BuildA && st.toolID != "" {
320 if data, _, err := cache.GetBytes(c, key); err == nil {
321 return data, nil
322 }
323 }
324 sh := b.Shell(a)
325 if err := sh.Mkdir(a.Objdir); err != nil {
326 return nil, err
327 }
328 file := a.Objdir + "metadata.json"
329 if err := sh.run(a.Objdir, "analysis tool", nil, cfg.BuildToolexec, st.toolPath, "-describe="+file); err != nil {
330 if cfg.Goos != runtime.GOOS || cfg.Goarch != runtime.GOARCH {
331
332
333 return nil, fmt.Errorf("running analysis tool built for %s/%s: %v\n(to run the tests on another platform, use -exec; to disable analysis, use -vet=off)", cfg.Goos, cfg.Goarch, err)
334 }
335 return nil, err
336 }
337 if cfg.BuildN {
338 return nil, nil
339 }
340 data, err := os.ReadFile(file)
341 if err != nil {
342 return nil, err
343 }
344 if st.toolID != "" {
345 if err := cache.PutBytes(c, key, data); err != nil {
346 return nil, err
347 }
348 }
349 return data, nil
350 }
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373 func (b *Builder) analyzerIDs(md *protocol.Metadata) (driverID cache.ActionID, ids []cache.ActionID) {
374 st := &b.analysis
375 compiled := make(map[string]*Action)
376 for _, a1 := range st.tool.Deps {
377 if a1.Package != nil {
378 compiled[a1.Package.ImportPath] = a1
379 }
380 }
381
382
383
384
385
386
387
388 depsMemo := make(map[*load.Package]cache.ActionID)
389 var merkle func(p *load.Package) cache.ActionID
390 merkle = func(p *load.Package) cache.ActionID {
391 if id, ok := depsMemo[p]; ok {
392 return id
393 }
394 h := cache.NewHash("analysis deps")
395 fmt.Fprintf(h, "package %q\n", p.ImportPath)
396
397
398 if a1 := compiled[p.ImportPath]; a1 != nil {
399 fmt.Fprintf(h, "content %s\n", a1.BuildContentID())
400 }
401
402
403
404
405
406
407 var imports cache.ActionID
408 for _, imp := range p.Internal.Imports {
409 addActionID(&imports, merkle(imp))
410 }
411 fmt.Fprintf(h, "imports %x\n", imports)
412 id := h.Sum()
413 depsMemo[p] = id
414 return id
415 }
416
417
418 driverID = merkle(compiled[load.AnalysisToolDriver].Package)
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438 varPkgs := make([]map[string]bool, len(md.Analyzers))
439 for _, v := range md.Vars {
440 if v.Error != "" {
441 continue
442 }
443 pkg := v.Symbol[:strings.LastIndexByte(v.Symbol, '.')]
444 var visit func(h int)
445 visit = func(h int) {
446 if varPkgs[h] == nil {
447 varPkgs[h] = make(map[string]bool)
448 }
449 if !varPkgs[h][pkg] {
450 varPkgs[h][pkg] = true
451 for _, req := range md.Analyzers[h].Requires {
452 visit(req)
453 }
454 }
455 }
456 for _, m := range v.Members {
457 if m.Analyzer >= 0 {
458 visit(m.Analyzer)
459 }
460 }
461 }
462
463 ids = make([]cache.ActionID, len(md.Analyzers))
464 for h, an := range md.Analyzers {
465 hash := cache.NewHash("analyzer")
466 fmt.Fprintf(hash, "name %q\nrun %q\ndriver %x\n", an.Name, an.RunFunc, driverID)
467
468
469 var vars cache.ActionID
470 linked := len(varPkgs[h]) > 0
471 for pkg := range varPkgs[h] {
472 if a1 := compiled[pkg]; a1 != nil {
473 addActionID(&vars, merkle(a1.Package))
474 } else {
475 linked = false
476 }
477 }
478 fmt.Fprintf(hash, "vars %x\n", vars)
479 if !linked {
480
481
482
483 fmt.Fprintf(hash, "tool %s\n", st.tool.buildID)
484 }
485 ids[h] = hash.Sum()
486 }
487 return driverID, ids
488 }
489
490
491
492 func addActionID(sum *cache.ActionID, id cache.ActionID) {
493 var carry uint16
494 for i := range sum {
495 s := uint16(sum[i]) + uint16(id[i]) + carry
496 sum[i] = byte(s)
497 carry = s >> 8
498 }
499 }
500
501
502
503
504
505
506 func (b *Builder) planAnalysis(md *protocol.Metadata, driverID cache.ActionID, ids []cache.ActionID) {
507 st := &b.analysis
508
509
510
511
512 plan := &analysisPlan{
513 analyzers: make([]analyzerInfo, len(md.Analyzers)),
514 driverID: driverID,
515 reported: make(map[*Action]map[int]*reportedAnalyzer),
516 refErrs: make(map[*Action][]string),
517 }
518 for i, an := range md.Analyzers {
519 set := map[int]bool{i: true}
520 for _, req := range an.Requires {
521 for _, j := range plan.analyzers[req].reqClosure {
522 set[j] = true
523 }
524 }
525 plan.analyzers[i] = analyzerInfo{
526 Analyzer: an,
527 reqClosure: slices.Sorted(maps.Keys(set)),
528 id: ids[i],
529 }
530 }
531
532
533
534 closure := func(hs []int) []int {
535 set := make(map[int]bool)
536 for _, h := range hs {
537 for _, h2 := range plan.analyzers[h].reqClosure {
538 set[h2] = true
539 }
540 }
541 return slices.Sorted(maps.Keys(set))
542 }
543
544
545
546 for _, a := range st.roots {
547 plan.reported[a], plan.refErrs[a] = resolveAnalyzers(md, st.rootConfig[a])
548 }
549
550
551
552
553
554
555
556
557
558 need := make(map[*Action]map[int]bool)
559
560
561 add := func(a *Action, hs []int) bool {
562 set := need[a]
563 if set == nil {
564 set = make(map[int]bool)
565 need[a] = set
566 }
567 added := false
568 for _, h := range hs {
569 if !set[h] {
570 set[h] = true
571 added = true
572 }
573 }
574 return added
575 }
576
577
578
579
580
581
582
583
584
585 var propagate func(a *Action, hs []int)
586 propagate = func(a *Action, hs []int) {
587 for _, dep := range a.Deps {
588 if dep.Mode == "analysisvet" && add(dep, hs) {
589 propagate(dep, hs)
590 }
591 }
592 }
593 for _, a := range st.roots {
594 run := closure(slices.Collect(maps.Keys(plan.reported[a])))
595 add(a, run)
596 var factUsers []int
597 for _, h := range run {
598 if plan.analyzers[h].Facts {
599 factUsers = append(factUsers, h)
600 }
601 }
602 propagate(a, closure(factUsers))
603 }
604 plan.need = make(map[*Action][]int)
605 for a, set := range need {
606 plan.need[a] = slices.Sorted(maps.Keys(set))
607 }
608 st.plan = plan
609 }
610
611
612
613
614
615
616
617
618
619 func resolveAnalyzers(md *protocol.Metadata, config *load.AnalysisConfig) (map[int]*reportedAnalyzer, []string) {
620 var errs []string
621 errorf := func(annot *load.AnalysisRef, format string, args ...any) {
622 errs = append(errs, annot.Errorf(format, args...).Error())
623 }
624
625
626
627
628 type member struct {
629 index int
630 name string
631 }
632 members := func(annot *load.AnalysisRef) []member {
633 v := md.Var(annot.Var.String())
634 if v == nil {
635 return nil
636 }
637 if v.Error != "" {
638 errorf(annot, "%s", v.Error)
639 return nil
640 }
641 if v.IsSuite != annot.IsSuite() {
642 if !v.IsSuite {
643 errorf(annot, "%s is an analyzer, not a suite (remove the brackets)", annot.Var)
644 } else {
645 errorf(annot, "%[1]s is a suite (use %[1]s[*] or select members by name)", annot.Var)
646 }
647 return nil
648 }
649 var res []member
650 addMember := func(m *protocol.Member) {
651 name := annot.Var.String()
652 if v.IsSuite {
653 name += "[" + m.Name + "]"
654 }
655 if m.Analyzer < 0 {
656 msg := m.Error
657 if v.IsSuite {
658 msg = fmt.Sprintf("member %s: %s", m.Name, msg)
659 }
660 errorf(annot, "%s", msg)
661 return
662 }
663 res = append(res, member{m.Analyzer, name})
664 }
665 if !annot.IsSuite() || annot.AllMembers() {
666 for _, m := range v.Members {
667 addMember(m)
668 }
669 } else {
670 for _, name := range annot.Members {
671 i := slices.IndexFunc(v.Members, func(m *protocol.Member) bool {
672 return m.Name == name
673 })
674 if i < 0 {
675 errorf(annot, "suite %s has no member named %s", annot.Var.String(), name)
676 continue
677 }
678 addMember(v.Members[i])
679 }
680 }
681 return res
682 }
683
684
685
686 enabled := make(map[int]*reportedAnalyzer)
687 for _, annot := range config.Positive {
688 for _, m := range members(annot) {
689 if _, ok := enabled[m.index]; !ok {
690 enabled[m.index] = &reportedAnalyzer{name: m.name}
691 }
692 }
693 }
694
695
696
697 for _, annot := range config.Negative {
698 for h := range enabled {
699 if md.Analyzers[h].Name == annot.Name {
700 delete(enabled, h)
701 }
702 }
703 }
704 return enabled, errs
705 }
706
707
708 func (b *Builder) analysisVet(ctx context.Context, a *Action) error {
709
710
711
712
713
714
715
716
717
718 a.Failed = nil
719
720
721 if a.Deps[0].Failed != nil {
722 return nil
723 }
724 for _, a1 := range a.Deps {
725 if a1.Failed != nil && a1.Mode == "cgo run" {
726 return nil
727 }
728 }
729 st := &b.analysis
730 if cfg.BuildN {
731
732
733
734 if _, ok := st.rootConfig[a]; ok {
735 b.Shell(a).ShowCmd("", "%s %s # analyzers determined by -describe", st.toolPath, a.Objdir+"analysis.cfg")
736 }
737 return nil
738 }
739 plan := st.plan
740 if plan == nil {
741 return nil
742 }
743
744
745 var deps []*Action
746 for _, a1 := range a.Deps {
747 if a1.Mode == "analysisvet" && a1.Actor != nil {
748 if analysisResultOf(a1) == nil {
749 return nil
750 }
751 deps = append(deps, a1)
752 }
753 }
754 slices.SortFunc(deps, func(x, y *Action) int {
755 return strings.Compare(x.Package.ImportPath, y.Package.ImportPath)
756 })
757
758 reported := func(index int) bool {
759 _, ok := plan.reported[a][index]
760 return ok
761 }
762 vcfg0, vetDir := buildVetConfig(a)
763
764
765
766
767 absFiles := func(files []string) []string {
768 res := make([]string, len(files))
769 for i, file := range files {
770 if !filepath.IsAbs(file) {
771 file = vetDir + file
772 }
773 res[i] = file
774 }
775 return res
776 }
777 var (
778 goFiles = absFiles(vcfg0.GoFiles)
779 nonGoFiles = absFiles(vcfg0.NonGoFiles)
780 ignoredFiles = absFiles(vcfg0.IgnoredFiles)
781 )
782
783
784
785
786
787
788
789 pkgID := func() cache.ActionID {
790 h := cache.NewHash("analysis pkg " + a.Package.ImportPath)
791 fmt.Fprintf(h, "id %q\nimportpath %q\ngoversion %q\n", vcfg0.ID, vcfg0.ImportPath, vcfg0.GoVersion)
792
793 fmt.Fprintf(h, "goos %q\ngoarch %q\ntags %q\ntooltags %q\n", cfg.Goos, cfg.Goarch, cfg.BuildContext.BuildTags, cfg.BuildContext.ToolTags)
794 if m := vcfg0.Module; m != nil {
795 fmt.Fprintf(h, "module %q %q %q\n", m.Path, m.Version, m.GoVersion)
796 }
797
798
799
800
801
802
803 fmt.Fprintf(h, "dir %q\n", vcfg0.Dir)
804 for _, files := range [][]string{goFiles, nonGoFiles, ignoredFiles} {
805 for _, file := range files {
806 name := file
807 if str.HasFilePathPrefix(file, b.WorkDir) {
808 name = filepath.Base(file)
809 }
810 fmt.Fprintf(h, "file %q %s\n", name, b.fileHash(file))
811 }
812 fmt.Fprintf(h, "\n")
813 }
814
815
816
817
818 depPaths := map[string]bool{"unsafe": true}
819 for _, a1 := range deps {
820 depPaths[a1.Package.ImportPath] = true
821 }
822 for _, k := range slices.Sorted(maps.Keys(vcfg0.ImportMap)) {
823 if v := vcfg0.ImportMap[k]; depPaths[v] {
824 fmt.Fprintf(h, "importmap %q %q\n", k, v)
825 }
826 }
827 for _, a1 := range deps {
828 fmt.Fprintf(h, "dep %q %x\n", a1.Package.ImportPath, analysisResultOf(a1).types.id)
829 }
830 return h.Sum()
831 }()
832
833
834 typesKey := func() cache.ActionID {
835 h := cache.NewHash("analysis types " + a.Package.ImportPath)
836 fmt.Fprintf(h, "pkg %x\ndriver %x\n", pkgID, plan.driverID)
837 return h.Sum()
838 }()
839 typeErrsKey := func() cache.ActionID {
840 h := cache.NewHash("analysis type errors " + a.Package.ImportPath)
841 fmt.Fprintf(h, "types %x\n", typesKey)
842 return h.Sum()
843 }()
844 passKey := func(index int) cache.ActionID {
845 h := cache.NewHash("analysis pass " + a.Package.ImportPath)
846 fmt.Fprintf(h, "pkg %x\nanalyzer %x\nreport %t\n", pkgID, plan.analyzers[index].id, reported(index))
847 for _, h2 := range plan.analyzers[index].reqClosure {
848 if plan.analyzers[h2].Facts {
849 for _, a1 := range deps {
850 fmt.Fprintf(h, "dep %q %x %x\n", a1.Package.ImportPath, plan.analyzers[h2].id, analysisResultOf(a1).passes[h2].factsID)
851 }
852 }
853 }
854 return h.Sum()
855 }
856
857
858 c := cache.Default()
859 get := func(key cache.ActionID) (analysisOutput, bool) {
860 if !cfg.BuildA {
861 if file, entry, err := cache.GetFile(c, key); err == nil {
862 return analysisOutput{file: file, id: entry.OutputID}, true
863 }
864 }
865 return analysisOutput{}, false
866 }
867 res := &analysisResult{passes: make(map[int]analysisOutput)}
868
869 if out, ok := get(typesKey); ok {
870 if data, _, err := cache.GetBytes(c, typeErrsKey); err == nil && json.Unmarshal(data, &res.typeErrs) == nil {
871 res.types = out
872 }
873 }
874 needTypes := res.types.file == ""
875 var missing []int
876 for _, index := range plan.need[a] {
877 if out, ok := get(passKey(index)); ok && out.readPassInfo() == nil {
878 res.passes[index] = out
879 } else {
880 missing = append(missing, index)
881 }
882 }
883
884
885 if len(missing) > 0 || needTypes {
886 areq := &protocol.AnalyzeRequest{
887 ID: vcfg0.ID,
888 ImportPath: vcfg0.ImportPath,
889 Dir: vcfg0.Dir,
890 GoFiles: goFiles,
891 NonGoFiles: nonGoFiles,
892 IgnoredFiles: ignoredFiles,
893 ImportMap: vcfg0.ImportMap,
894 GoVersion: vcfg0.GoVersion,
895 GOARCH: cfg.Goarch,
896 Module: protocolModule(vcfg0.Module),
897 TypeImports: make(map[string]string),
898 Facts: make(map[int]map[string]string),
899 Outputs: make(map[int]string),
900 Report: []int{},
901 }
902 for _, a1 := range deps {
903 areq.TypeImports[a1.Package.ImportPath] = analysisResultOf(a1).types.file
904 }
905 if needTypes {
906 areq.TypeExport = a.Objdir + "types"
907 areq.TypeErrors = a.Objdir + "typeerrors"
908 }
909 for i, index := range missing {
910 areq.Outputs[index] = fmt.Sprintf("%spass%d", a.Objdir, i)
911 if reported(index) {
912 areq.Report = append(areq.Report, index)
913 }
914 for _, h2 := range plan.analyzers[index].reqClosure {
915 if plan.analyzers[h2].Facts && areq.Facts[h2] == nil {
916 files := make(map[string]string)
917 for _, a1 := range deps {
918
919
920 if out, ok := analysisResultOf(a1).passes[h2]; ok && !out.trivial {
921 files[a1.Package.ImportPath] = out.file
922 }
923 }
924 areq.Facts[h2] = files
925 }
926 }
927 }
928
929 sh := b.Shell(a)
930 js, err := json.Marshal(areq)
931 if err != nil {
932 return fmt.Errorf("internal error marshaling analysis config: %v", err)
933 }
934 if err := sh.Mkdir(a.Objdir); err != nil {
935 return err
936 }
937 if err := sh.writeFile(a.Objdir+"analysis.cfg", js); err != nil {
938 return err
939 }
940 p := a.Package
941 if err := sh.run(p.Dir, p.ImportPath, b.cCompilerEnv(), cfg.BuildToolexec, st.toolPath, a.Objdir+"analysis.cfg"); err != nil {
942 return err
943 }
944
945
946 put := func(key cache.ActionID, file string) (analysisOutput, error) {
947 f, err := os.Open(file)
948 if err != nil {
949 return analysisOutput{}, err
950 }
951 defer f.Close()
952 id, _, err := c.Put(key, f)
953 if err != nil {
954 return analysisOutput{}, err
955 }
956 return analysisOutput{file: c.OutputFile(id), id: id}, nil
957 }
958 if needTypes {
959 data, err := os.ReadFile(areq.TypeErrors)
960 if err != nil {
961 return err
962 }
963 if err := json.Unmarshal(data, &res.typeErrs); err != nil {
964 return fmt.Errorf("decoding type errors of %s: %v", vcfg0.ID, err)
965 }
966 if err := cache.PutBytes(c, typeErrsKey, data); err != nil {
967 return err
968 }
969 res.types, err = put(typesKey, areq.TypeExport)
970 if err != nil {
971 return err
972 }
973 }
974 for _, index := range missing {
975 out, err := put(passKey(index), areq.Outputs[index])
976 if err != nil {
977 return err
978 }
979 if err := out.readPassInfo(); err != nil {
980 return err
981 }
982 res.passes[index] = out
983 }
984 }
985 a.Provider = res
986
987
988
989
990
991 var buf bytes.Buffer
992 for _, msg := range res.typeErrs {
993 fmt.Fprintf(&buf, "%s\n", msg)
994 }
995 for _, index := range plan.need[a] {
996 if f := res.passes[index].failure; f != nil {
997 fmt.Fprintf(&buf, "%s\n", f)
998 }
999 }
1000 if buf.Len() > 0 {
1001 return b.Shell(a).reportCmd("", "", buf.Bytes(), errors.New("analysis failed"))
1002 }
1003 return nil
1004 }
1005
1006
1007 func (b *Builder) analysisReport(ctx context.Context, r *Action) error {
1008 a := r.Deps[0]
1009 if a.Actor == nil || a.Deps[0].Failed != nil {
1010 return nil
1011 }
1012 plan := b.analysis.plan
1013 if plan == nil {
1014 return nil
1015 }
1016 reported := plan.reported[a]
1017
1018 var buf bytes.Buffer
1019 incomplete := false
1020
1021
1022 for _, msg := range plan.refErrs[a] {
1023 fmt.Fprintf(&buf, "%s\n", msg)
1024 }
1025
1026
1027
1028
1029
1030 indexes := slices.SortedFunc(maps.Keys(reported), func(x, y int) int {
1031 return strings.Compare(reported[x].name, reported[y].name)
1032 })
1033 res := analysisResultOf(a)
1034 if res == nil {
1035
1036 incomplete = len(indexes) > 0
1037 } else {
1038 for _, index := range indexes {
1039 en := reported[index]
1040 out, ok := res.passes[index]
1041 if !ok {
1042
1043 return fmt.Errorf("internal error: no pass file for analyzer %s", en.name)
1044 }
1045 pf, err := protocol.ReadPass(out.file)
1046 if err != nil {
1047 return err
1048 }
1049 if pf.Failure != nil || pf.Skipped {
1050 incomplete = true
1051 }
1052 for _, d := range pf.Diagnostics {
1053 fmt.Fprintf(&buf, "%s: %s\n", d.Posn, d.Message)
1054 }
1055 }
1056 }
1057 if buf.Len() > 0 {
1058 return b.Shell(r).reportCmd("", "", buf.Bytes(), errors.New("analysis reported problems"))
1059 }
1060
1061 if incomplete {
1062
1063
1064
1065
1066
1067 r.Failed = a
1068 }
1069
1070 return nil
1071 }
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083 func (out *analysisOutput) readPassInfo() error {
1084 pf, err := protocol.ReadPass(out.file)
1085 if err != nil {
1086 return err
1087 }
1088 h := sha256.New()
1089 fmt.Fprintf(h, "facts %d\n", len(pf.Facts))
1090 h.Write(pf.Facts)
1091 if f := pf.Failure; f != nil {
1092 fmt.Fprintf(h, "failure %q %q %q\n", f.Package, f.Analyzer, f.Message)
1093 }
1094 fmt.Fprintf(h, "skipped %t\n", pf.Skipped)
1095 h.Sum(out.factsID[:0])
1096 out.trivial = len(pf.Facts) == 0 && pf.Failure == nil && !pf.Skipped
1097 out.failure = pf.Failure
1098 return nil
1099 }
1100
1101
1102
1103 func protocolModule(m *vetModule) *protocol.Module {
1104 if m == nil {
1105 return nil
1106 }
1107 pm := &protocol.Module{
1108 Path: m.Path,
1109 Version: m.Version,
1110 Replace: protocolModule(m.Replace),
1111 Time: m.Time,
1112 Main: m.Main,
1113 Indirect: m.Indirect,
1114 Dir: m.Dir,
1115 GoMod: m.GoMod,
1116 GoVersion: m.GoVersion,
1117 }
1118 if m.Error != nil {
1119 pm.Error = &protocol.ModuleError{Err: m.Error.Err}
1120 }
1121 return pm
1122 }
1123
View as plain text