Source file src/internal/analysis/gotestchecker/main.go
1 // Copyright 2026 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 // Package gotestchecker is the driver for analysis in go test. 6 // 7 // The go command synthesizes a main package, then builds an 8 // executable called the "analysis tool" or "tool". It is exempt from 9 // the usual rules on importing internal packages. 10 // 11 // The analysis tool's command-line interface, described below, is 12 // tightly coupled to the go command; it is unstable and may change at 13 // any time. 14 // 15 // The generated main file calls [Main] with the values of the 16 // variables referenced by its analysis configuration (analyzers and 17 // suites, such as the vet suite of [internal/analysis/suite/vet]), 18 // then executes the tool in one of two modes: 19 // 20 // tool -describe=FILE # write analyzer metadata (JSON) to FILE 21 // tool REQUEST # analyze the package described by REQUEST (JSON) 22 // 23 // The result of the -describe query lets the go command plan the 24 // analysis part of the build: from the metadata it learns the 25 // Requires graph of the analyzers, and which of them use facts, so 26 // that it materializes facts only for the analyzers that need them, 27 // and only once per (package, analyzer), regardless of how many root 28 // analyzers require that analyzer. 29 // 30 // Within the tool, analyzers are identified by pointer. In the 31 // protocol between go command and the tool (analyze requests, 32 // metadata and pass files), they are identified by their index in the 33 // metadata (see [protocol.Metadata]), which is deterministic for a 34 // given tool. 35 // 36 // The sequence of events is: 37 // 38 // 1. The tool is linked with the variables of the references that 39 // survive the go command's static stage: positive references, 40 // after textual negation of whole variables, and negations by 41 // reference (whose variables are needed to negate by pointer). 42 // 43 // 2. The -describe query expands each suite into its members, 44 // giving the index of each member and of each analyzer in its 45 // Requires closure. The go command then performs the dynamic 46 // stage: it selects suite members, applies negations by pointer 47 // (=index) and by Analyzer.Name, and plans which analyzers each 48 // package needs. So a REQUEST never mentions references, suites, 49 // or negations: it names analyzers only by index, in 50 // Outputs (the analyzers whose results the go command lacks), 51 // Report (those whose diagnostics are reported), and 52 // Facts (the fact-using analyzers in the Requires closure of Outputs). 53 // 54 // 3. The go command executes the tool for each package, like a build. 55 // The tool runs the Requires graph of all needed analyzers 56 // and writes a "pass" file for needed result specified by Outputs. 57 // 58 // The protocol message types (analyze requests, metadata, and pass 59 // files) are defined by package 60 // [internal/analysis/gotestchecker/protocol], which the go command 61 // also imports. 62 package gotestchecker 63 64 import ( 65 "flag" 66 "fmt" 67 "log" 68 "os" 69 ) 70 71 // A Var is an analyzer or suite variable linked into the tool: its 72 // symbol, the qualified name "pkgpath.Var" (never with a member list), 73 // and its value, which should be an analyzer (*analysis.Analyzer) or 74 // a suite ([]*analysis.Analyzer). Values of other types are reported 75 // as errors by the metadata query. 76 type Var struct { 77 Symbol string 78 Value any 79 } 80 81 // Main is the main function of a go test analysis tool. 82 // The go command passes the variables in order of Symbol, with no 83 // duplicates. 84 func Main(vars []Var) { 85 log.SetFlags(0) 86 log.SetPrefix("gotestchecker: ") 87 88 // We use a private flag set, so that the flags 89 // of analyzer packages cannot interfere. 90 flags := flag.NewFlagSet(os.Args[0], flag.ExitOnError) 91 describe := flags.String("describe", "", "write analyzer metadata (JSON) to the named file") 92 flags.Usage = func() { 93 fmt.Fprintf(os.Stderr, "usage: %[1]s -describe=FILE | %[1]s REQUEST\n", os.Args[0]) 94 os.Exit(2) 95 } 96 flags.Parse(os.Args[1:]) 97 98 if *describe != "" { 99 if flags.NArg() != 0 { 100 flags.Usage() 101 } 102 if err := writeMetadata(*describe, vars); err != nil { 103 log.Fatal(err) 104 } 105 return 106 } 107 108 if flags.NArg() != 1 { 109 flags.Usage() 110 } 111 if err := run(flags.Arg(0), vars); err != nil { 112 log.Fatal(err) 113 } 114 } 115