Debian Codegraph @

cmd/bisect/main.go

raw symbols SCIP
1 // Copyright 2023 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 // Bisect finds changes responsible for causing a failure.
6 // A typical use is to identify the source locations in a program
7 // that are miscompiled by a given compiler optimization.
8 //
9 // Usage:
10 //
11 // bisect [flags] [var=value...] command [arguments...]
12 //
13 // Bisect operates on a target command line – the target – that can be
14 // run with various changes individually enabled or disabled. With none
15 // of the changes enabled, the target is known to succeed (exit with exit
16 // code zero). With all the changes enabled, the target is known to fail
17 // (exit any other way). Bisect repeats the target with different sets of
18 // changes enabled, using binary search to find (non-overlapping) minimal
19 // change sets that provoke the failure.
20 //
21 // The target must cooperate with bisect by accepting a change pattern
22 // and then enabling and reporting the changes that match that pattern.
23 // The change pattern is passed to the target by substituting it anywhere
24 // the string PATTERN appears in the environment values or the command
25 // arguments. For each change that matches the pattern, the target must
26 // enable that change and also print one or more “match lines”
27 // (to standard output or standard error) describing the change.
28 // The [golang.org/x/tools/internal/bisect] package provides functions to help
29 // targets implement this protocol. We plan to publish that package
30 // in a non-internal location after finalizing its API.
31 //
32 // Bisect starts by running the target with no changes enabled and then
33 // with all changes enabled. It expects the former to succeed and the latter to fail,
34 // and then it will search for the minimal set of changes that must be enabled
35 // to provoke the failure. If the situation is reversed – the target fails with no
36 // changes enabled and succeeds with all changes enabled – then bisect
37 // automatically runs in reverse as well, searching for the minimal set of changes
38 // that must be disabled to provoke the failure.
39 //
40 // Bisect prints tracing logs to standard error and the minimal change sets
41 // to standard output.
42 //
43 // # Command Line Flags
44 //
45 // Bisect supports the following command-line flags:
46 //
47 // -max=M
48 //
49 // Stop after finding M minimal change sets. The default is no maximum, meaning to run until
50 // all changes that provoke a failure have been identified.
51 //
52 // -maxset=S
53 //
54 // Disallow change sets larger than S elements. The default is no maximum.
55 //
56 // -timeout=D
57 //
58 // If the target runs for longer than duration D, stop the target and interpret that as a failure.
59 // The default is no timeout.
60 //
61 // -count=N
62 //
63 // Run each trial N times (default 2), checking for consistency.
64 //
65 // -v
66 //
67 // Print verbose output, showing each run and its match lines.
68 //
69 // In addition to these general flags,
70 // bisect supports a few “shortcut” flags that make it more convenient
71 // to use with specific targets.
72 //
73 // -compile=<rewrite>
74 //
75 // This flag is equivalent to adding an environment variable
76 // “GOCOMPILEDEBUG=<rewrite>hash=PATTERN”,
77 // which, as discussed in more detail in the example below,
78 // allows bisect to identify the specific source locations where the
79 // compiler rewrite causes the target to fail.
80 //
81 // -godebug=<name>=<value>
82 //
83 // This flag is equivalent to adding an environment variable
84 // “GODEBUG=<name>=<value>#PATTERN”,
85 // which allows bisect to identify the specific call stacks where
86 // the changed [GODEBUG setting] value causes the target to fail.
87 //
88 // # Example
89 //
90 // The Go compiler provides support for enabling or disabling certain rewrites
91 // and optimizations to allow bisect to identify specific source locations where
92 // the rewrite causes the program to fail. For example, to bisect a failure caused
93 // by the new loop variable semantics:
94 //
95 // bisect go test -gcflags=all=-d=loopvarhash=PATTERN
96 //
97 // The -gcflags=all= instructs the go command to pass the -d=... to the Go compiler
98 // when compiling all packages. Bisect varies PATTERN to determine the minimal set of changes
99 // needed to reproduce the failure.
100 //
101 // The go command also checks the GOCOMPILEDEBUG environment variable for flags
102 // to pass to the compiler, so the above command is equivalent to:
103 //
104 // bisect GOCOMPILEDEBUG=loopvarhash=PATTERN go test
105 //
106 // Finally, as mentioned earlier, the -compile flag allows shortening this command further:
107 //
108 // bisect -compile=loopvar go test
109 //
110 // # Defeating Build Caches
111 //
112 // Build systems cache build results, to avoid repeating the same compilations
113 // over and over. When using a cached build result, the go command (correctly)
114 // reprints the cached standard output and standard error associated with that
115 // command invocation. (This makes commands like 'go build -gcflags=-S' for
116 // printing an assembly listing work reliably.)
117 //
118 // Unfortunately, most build systems, including Bazel, are not as careful
119 // as the go command about reprinting compiler output. If the compiler is
120 // what prints match lines, a build system that suppresses compiler
121 // output when using cached compiler results will confuse bisect.
122 // To defeat such build caches, bisect replaces the literal text “RANDOM”
123 // in environment values and command arguments with a random 64-bit value
124 // during each invocation. The Go compiler conveniently accepts a
125 // -d=ignore=... debug flag that ignores its argument, so to run the
126 // previous example using Bazel, the invocation is:
127 //
128 // bazel test --define=gc_goopts=-d=loopvarhash=PATTERN,unused=RANDOM //path/to:test
129 //
130 // [GODEBUG setting]: https://tip.golang.org/doc/godebug
131 package mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/
132
133 import (
134 "contextscip-go gomod github.com/golang/go/src go1.25.0 context/"
135 "flagscip-go gomod github.com/golang/go/src go1.25.0 flag/"
136 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
137 "ioscip-go gomod github.com/golang/go/src go1.25.0 io/"
138 "logscip-go gomod github.com/golang/go/src go1.25.0 log/"
139 "math/bitsscip-go gomod github.com/golang/go/src go1.25.0 `math/bits`/"
140 "math/randscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/"
141 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
142 "os/execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/"
143 "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/"
144 "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/"
145 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
146 "timescip-go gomod github.com/golang/go/src go1.25.0 time/"
147
148 "golang.org/x/tools/internal/bisectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/bisect`/"
149 )
150
151 // Preserve import of bisect, to allow [bisect.Match] in the doc comment.
152 var _ bisectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/bisect`/.Matcherscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/bisect`/Matcher#
153
154 func usagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/usage().() {
155 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr., "usage: bisect [flags] [var=value...] command [arguments...]\n")
156 flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.PrintDefaultsscip-go gomod github.com/golang/go/src go1.25.0 flag/PrintDefaults().()
157 osscip-go gomod github.com/golang/go/src go1.25.0 os/.Exitscip-go gomod github.com/golang/go/src go1.25.0 os/Exit().(2)
158 }
159
160 func mainscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/main().() {
161 logscip-go gomod github.com/golang/go/src go1.25.0 log/.SetFlagsscip-go gomod github.com/golang/go/src go1.25.0 log/SetFlags().(0)
162 logscip-go gomod github.com/golang/go/src go1.25.0 log/.SetPrefixscip-go gomod github.com/golang/go/src go1.25.0 log/SetPrefix().("bisect: ")
163
164 var b Bisectscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#
165 b.Stdoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Stdout. = osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stdoutscip-go gomod github.com/golang/go/src go1.25.0 os/Stdout.
166 b.Stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Stderr. = osscip-go gomod github.com/golang/go/src go1.25.0 os/.Stderrscip-go gomod github.com/golang/go/src go1.25.0 os/Stderr.
167 flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.IntVarscip-go gomod github.com/golang/go/src go1.25.0 flag/IntVar().(&b.Maxscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Max., "max", 0, "stop after finding `m` failing change sets")
168 flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.IntVarscip-go gomod github.com/golang/go/src go1.25.0 flag/IntVar().(&b.MaxSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#MaxSet., "maxset", 0, "do not search for change sets larger than `s` elements")
169 flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.DurationVarscip-go gomod github.com/golang/go/src go1.25.0 flag/DurationVar().(&b.Timeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Timeout., "timeout", 0, "stop target and consider failed after duration `d`")
170 flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.IntVarscip-go gomod github.com/golang/go/src go1.25.0 flag/IntVar().(&b.Countscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Count., "count", 2, "run target `n` times for each trial")
171 flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.BoolVarscip-go gomod github.com/golang/go/src go1.25.0 flag/BoolVar().(&b.Verbosescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Verbose., "v", false, "enable verbose output")
172
173 env := ""
174 envFlag := ""
175 flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Funcscip-go gomod github.com/golang/go/src go1.25.0 flag/Func().("compile", "bisect source locations affected by Go compiler `rewrite` (fma, loopvar, ...)", func(value string) error {
176 if envFlag != "" {
177 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("cannot use -%s and -compile", envFlag)
178 }
179 envFlag = "compile"
180 env = "GOCOMPILEDEBUG=" + value + "hash=PATTERN"
181 return nil
182 })
183 flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Funcscip-go gomod github.com/golang/go/src go1.25.0 flag/Func().("godebug", "bisect call stacks affected by GODEBUG setting `name=value`", func(value string) error {
184 if envFlag != "" {
185 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("cannot use -%s and -godebug", envFlag)
186 }
187 envFlag = "godebug"
188 env = "GODEBUG=" + value + "#PATTERN"
189 return nil
190 })
191
192 flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Usagescip-go gomod github.com/golang/go/src go1.25.0 flag/Usage. = usagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/usage().
193 flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Parsescip-go gomod github.com/golang/go/src go1.25.0 flag/Parse().()
194 args := flagscip-go gomod github.com/golang/go/src go1.25.0 flag/.Argsscip-go gomod github.com/golang/go/src go1.25.0 flag/Args().()
195
196 // Split command line into env settings, command name, args.
197 i := 0
198 for i < len(args) && stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(args[i], "=") {
199 i++
200 }
201 if i == len(args) {
202 usagescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/usage().()
203 }
204 b.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Env., b.Cmdscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Cmd., b.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Args. = args[:i], args[i], args[i+1:]
205 if env != "" {
206 b.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Env. = append([]string{env}, b.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Env....)
207 }
208
209 // Check that PATTERN is available for us to vary.
210 found := false
211 for _, e := range b.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Env. {
212 if _, v, _ := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Cutscip-go gomod github.com/golang/go/src go1.25.0 strings/Cut().(e, "="); stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(v, "PATTERN") {
213 found = true
214 }
215 }
216 for _, a := range b.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Args. {
217 if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(a, "PATTERN") {
218 found = true
219 }
220 }
221 if !found {
222 logscip-go gomod github.com/golang/go/src go1.25.0 log/.Fatalfscip-go gomod github.com/golang/go/src go1.25.0 log/Fatalf().("no PATTERN in target environment or args")
223 }
224
225 if !b.Searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Search().() {
226 osscip-go gomod github.com/golang/go/src go1.25.0 os/.Exitscip-go gomod github.com/golang/go/src go1.25.0 os/Exit().(1)
227 }
228 }
229
230 // A Bisect holds the state for a bisect invocation.
231 type Bisectscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect# struct {
232 // Env is the additional environment variables for the command.
233 // PATTERN and RANDOM are substituted in the values, but not the names.
234 Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Env. []string
235
236 // Cmd is the command (program name) to run.
237 // PATTERN and RANDOM are not substituted.
238 Cmdscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Cmd. string
239
240 // Args is the command arguments.
241 // PATTERN and RANDOM are substituted anywhere they appear.
242 Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Args. []string
243
244 // Command-line flags controlling bisect behavior.
245 Maxscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Max. int // maximum number of sets to report (0 = unlimited)
246 MaxSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#MaxSet. int // maximum number of elements in a set (0 = unlimited)
247 Timeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Timeout. timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration# // kill target and assume failed after this duration (0 = unlimited)
248 Countscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Count. int // run target this many times for each trial and give up if flaky (min 1 assumed; default 2 on command line set in main)
249 Verbosescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Verbose. bool // print long output about each trial (only useful for debugging bisect itself)
250
251 // State for running bisect, replaced during testing.
252 // Failing change sets are printed to Stdout; all other output goes to Stderr.
253 Stdoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Stdout. ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Writerscip-go gomod github.com/golang/go/src go1.25.0 io/Writer# // where to write standard output (usually os.Stdout)
254 Stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Stderr. ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Writerscip-go gomod github.com/golang/go/src go1.25.0 io/Writer# // where to write standard error (usually os.Stderr)
255 TestRunscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#TestRun. func(env []string, cmd string, args []string) (out []byte, err error) // if non-nil, used instead of exec.Command
256
257 // State maintained by Search.
258
259 // By default, Search looks for a minimal set of changes that cause a failure when enabled.
260 // If Disable is true, the search is inverted and seeks a minimal set of changes that
261 // cause a failure when disabled. In this case, the search proceeds as normal except that
262 // each pattern starts with a !.
263 Disablescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Disable. bool
264
265 // SkipHexDigits is the number of hex digits to use in skip messages.
266 // If the set of available changes is the same in each run, as it should be,
267 // then this doesn't matter: we'll only exclude suffixes that uniquely identify
268 // a given change. But for some programs, especially bisecting runtime
269 // behaviors, sometimes enabling one change unlocks questions about other
270 // changes. Strictly speaking this is a misuse of bisect, but just to make
271 // bisect more robust, we use the y and n runs to create an estimate of the
272 // number of bits needed for a unique suffix, and then we round it up to
273 // a number of hex digits, with one extra digit for good measure, and then
274 // we always use that many hex digits for skips.
275 SkipHexDigitsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#SkipHexDigits. int
276
277 // Add is a list of suffixes to add to every trial, because they
278 // contain changes that are necessary for a group we are assembling.
279 Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add. []string
280
281 // Skip is a list of suffixes that uniquely identify changes to exclude from every trial,
282 // because they have already been used in failing change sets.
283 // Suffixes later in the list may only be unique after removing
284 // the ones earlier in the list.
285 // Skip applies after Add.
286 Skipscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Skip. []string
287 }
288
289 // A Result holds the result of a single target trial.
290 type Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result# struct {
291 Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success. bool // whether the target succeeded (exited with zero status)
292 Cmdscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Cmd. string // full target command line
293 Outscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Out. string // full target output (stdout and stderr combined)
294
295 Suffixscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Suffix. string // the suffix used for collecting MatchIDs, MatchText, and MatchFull
296 MatchIDsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchIDs. []uint64 // match IDs enabled during this trial
297 MatchTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchText. []string // match reports for the IDs, with match markers removed
298 MatchFullscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchFull. []string // full match lines for the IDs, with match markers kept
299 }
300
301 // &searchFatal is a special panic value to signal that Search failed.
302 // This lets us unwind the search recursion on a fatal error
303 // but have Search return normally.
304 var searchFatalscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/searchFatal. int
305
306 // Search runs a bisect search according to the configuration in b.
307 // It reports whether any failing change sets were found.
308 func (b *Bisectscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#) Searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Search().() bool {
309 defer func() {
310 // Recover from panic(&searchFatal), implicitly returning false from Search.
311 // Re-panic on any other panic.
312 if e := recover(); e != nil && e != &searchFatalscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/searchFatal. {
313 panic(e)
314 }
315 }()
316
317 // Run with no changes and all changes, to figure out which direction we're searching.
318 // The goal is to find the minimal set of changes to toggle
319 // starting with the state where everything works.
320 // If "no changes" succeeds and "all changes" fails,
321 // we're looking for a minimal set of changes to enable to provoke the failure
322 // (broken = runY, b.Negate = false)
323 // If "no changes" fails and "all changes" succeeds,
324 // we're looking for a minimal set of changes to disable to provoke the failure
325 // (broken = runN, b.Negate = true).
326
327 b.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Logf().("checking target with all changes disabled")
328 runN := b.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Run().("n")
329
330 b.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Logf().("checking target with all changes enabled")
331 runY := b.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Run().("y")
332
333 var broken *Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#
334 switch {
335 case runN.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success. && !runY.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success.:
336 b.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Logf().("target succeeds with no changes, fails with all changes")
337 b.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Logf().("searching for minimal set of enabled changes causing failure")
338 broken = runY
339 b.Disablescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Disable. = false
340
341 case !runN.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success. && runY.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success.:
342 b.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Logf().("target fails with no changes, succeeds with all changes")
343 b.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Logf().("searching for minimal set of disabled changes causing failure")
344 broken = runN
345 b.Disablescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Disable. = true
346
347 case runN.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success. && runY.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success.:
348 b.Fatalfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Fatalf().("target succeeds with no changes and all changes")
349
350 case !runN.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success. && !runY.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success.:
351 b.Fatalfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Fatalf().("target fails with no changes and all changes")
352 }
353
354 // Compute minimum number of bits needed to distinguish
355 // all the changes we saw during N and all the changes we saw during Y.
356 b.SkipHexDigitsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#SkipHexDigits. = skipHexDigitsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/skipHexDigits().(runN.MatchIDsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchIDs., runY.MatchIDsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchIDs.)
357
358 // Loop finding and printing change sets, until none remain.
359 found := 0
360 for {
361 // Find set.
362 bad := b.searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#search().(broken)
363 if bad == nil {
364 if found == 0 {
365 b.Fatalfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Fatalf().("cannot find any failing change sets of size ≤ %d", b.MaxSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#MaxSet.)
366 }
367 break
368 }
369
370 // Confirm that set really does fail, to avoid false accusations.
371 // Also asking for user-visible output; earlier runs did not.
372 b.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Logf().("confirming failing change set")
373 b.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add. = append(b.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add.[:0], bad...)
374 broken = b.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Run().("v")
375 if broken.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success. {
376 b.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Logf().("confirmation run succeeded unexpectedly")
377 }
378 b.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add. = b.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add.[:0]
379
380 // Print confirmed change set.
381 found++
382 b.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Logf().("FOUND failing change set")
383 desc := "(enabling changes causes failure)"
384 if b.Disablescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Disable. {
385 desc = "(disabling changes causes failure)"
386 }
387 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(b.Stdoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Stdout., "--- change set #%d %s\n%s\n---\n", found, desc, stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(broken.MatchTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchText., "\n"))
388
389 // Stop if we've found enough change sets.
390 if b.Maxscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Max. > 0 && found >= b.Maxscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Max. {
391 break
392 }
393
394 // If running bisect target | tee bad.txt, prints to stdout and stderr
395 // both appear on the terminal, but the ones to stdout go through tee
396 // and can take a little bit of extra time. Sleep 1 millisecond to give
397 // tee time to catch up, so that its stdout print does not get interlaced
398 // with the stderr print from the next b.Log message.
399 timescip-go gomod github.com/golang/go/src go1.25.0 time/.Sleepscip-go gomod github.com/golang/go/src go1.25.0 time/Sleep().(1 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Millisecondscip-go gomod github.com/golang/go/src go1.25.0 time/Millisecond.)
400
401 // Disable the now-known-bad changes and see if any failures remain.
402 b.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Logf().("checking for more failures")
403 b.Skipscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Skip. = append(bad, b.Skipscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Skip....)
404 broken = b.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Run().("")
405 if broken.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success. {
406 what := "enabled"
407 if b.Disablescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Disable. {
408 what = "disabled"
409 }
410 b.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Logf().("target succeeds with all remaining changes %s", what)
411 break
412 }
413 b.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Logf().("target still fails; searching for more bad changes")
414 }
415 return true
416 }
417
418 // Fatalf prints a message to standard error and then panics,
419 // causing Search to return false.
420 func (b *Bisectscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#) Fatalfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Fatalf().(format string, args ...any) {
421 s := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("bisect: fatal error: "+format, args...)
422 if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(s, "\n") {
423 s += "\n"
424 }
425 b.Stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Stderr..Writescip-go gomod github.com/golang/go/src go1.25.0 io/Writer#Write().([]byte(s))
426 panic(&searchFatalscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/searchFatal.)
427 }
428
429 // Logf prints a message to standard error.
430 func (b *Bisectscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#) Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Logf().(format string, args ...any) {
431 s := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("bisect: "+format, args...)
432 if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(s, "\n") {
433 s += "\n"
434 }
435 b.Stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Stderr..Writescip-go gomod github.com/golang/go/src go1.25.0 io/Writer#Write().([]byte(s))
436 }
437
438 func skipHexDigitsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/skipHexDigits().(idY, idN []uint64) int {
439 var all []uint64
440 seen := make(map[uint64]bool)
441 for _, x := range idY {
442 seen[x] = true
443 all = append(all, x)
444 }
445 for _, x := range idN {
446 if !seen[x] {
447 seen[x] = true
448 all = append(all, x)
449 }
450 }
451 sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Slicescip-go gomod github.com/golang/go/src go1.25.0 sort/Slice().(all, func(i, j int) bool { return bitsscip-go gomod github.com/golang/go/src go1.25.0 `math/bits`/.Reverse64scip-go gomod github.com/golang/go/src go1.25.0 `math/bits`/Reverse64().(all[i]) < bitsscip-go gomod github.com/golang/go/src go1.25.0 `math/bits`/.Reverse64scip-go gomod github.com/golang/go/src go1.25.0 `math/bits`/Reverse64().(all[j]) })
452 digits := sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Searchscip-go gomod github.com/golang/go/src go1.25.0 sort/Search().(64/4, func(digits int) bool {
453 mask := uint64(1)<<(4*digits) - 1
454 for i := 0; i+1 < len(all); i++ {
455 if all[i]&mask == all[i+1]&mask {
456 return false
457 }
458 }
459 return true
460 })
461 if digits < 64/4 {
462 digits++
463 }
464 return digits
465 }
466
467 // search searches for a single locally minimal change set.
468 //
469 // Invariant: r describes the result of r.Suffix + b.Add, which failed.
470 // (There's an implicit -b.Skip everywhere here. b.Skip does not change.)
471 // We want to extend r.Suffix to preserve the failure, working toward
472 // a suffix that identifies a single change.
473 func (b *Bisectscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#) searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#search().(r *Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#) []string {
474 // The caller should be passing in a failure result that we diagnose.
475 if r.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success. {
476 b.Fatalfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Fatalf().("internal error: unexpected success") // mistake by caller
477 }
478
479 // If the failure reported no changes, the target is misbehaving.
480 if len(r.MatchIDsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchIDs.) == 0 {
481 b.Fatalfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Fatalf().("failure with no reported changes:\n\n$ %s\n%s\n", r.Cmdscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Cmd., r.Outscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Out.)
482 }
483
484 // If there's one matching change, that's the one we're looking for.
485 if len(r.MatchIDsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchIDs.) == 1 {
486 return []string{fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("x%0*x", b.SkipHexDigitsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#SkipHexDigits., r.MatchIDsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchIDs.[0]&(1<<(4*b.SkipHexDigitsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#SkipHexDigits.)-1))}
487 }
488
489 // If the suffix we were tracking in the trial is already 64 bits,
490 // either the target is bad or bisect itself is buggy.
491 if len(r.Suffixscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Suffix.) >= 64 {
492 b.Fatalfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Fatalf().("failed to isolate a single change with very long suffix")
493 }
494
495 // We want to split the current matchIDs by left-extending the suffix with 0 and 1.
496 // If all the matches have the same next bit, that won't cause a split, which doesn't
497 // break the algorithm but does waste time. Avoid wasting time by left-extending
498 // the suffix to the longest suffix shared by all the current match IDs
499 // before adding 0 or 1.
500 suffix := commonSuffixscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/commonSuffix().(r.MatchIDsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchIDs.)
501 if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(suffix, r.Suffixscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Suffix.) {
502 b.Fatalfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Fatalf().("internal error: invalid common suffix") // bug in commonSuffix
503 }
504
505 // Run 0suffix and 1suffix. If one fails, chase down the failure in that half.
506 r0 := b.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Run().("0" + suffix)
507 if !r0.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success. {
508 return b.searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#search().(r0)
509 }
510 r1 := b.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Run().("1" + suffix)
511 if !r1.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success. {
512 return b.searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#search().(r1)
513 }
514
515 // suffix failed, but 0suffix and 1suffix succeeded.
516 // Assuming the target isn't flaky, this means we need
517 // at least one change from 0suffix AND at least one from 1suffix.
518 // We are already tracking N = len(b.Add) other changes and are
519 // allowed to build sets of size at least 1+N (or we shouldn't be here at all).
520 // If we aren't allowed to build sets of size 2+N, give up this branch.
521 if b.MaxSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#MaxSet. > 0 && 2+len(b.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add.) > b.MaxSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#MaxSet. {
522 return nil
523 }
524
525 // Adding all matches for 1suffix, recurse to narrow down 0suffix.
526 old := len(b.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add.)
527 b.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add. = append(b.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add., "1"+suffix)
528 r0 = b.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Run().("0" + suffix)
529 if r0.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success. {
530 // 0suffix + b.Add + 1suffix = suffix + b.Add is what r describes, and it failed.
531 b.Fatalfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Fatalf().("target fails inconsistently")
532 }
533 bad0 := b.searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#search().(r0)
534 if bad0 == nil {
535 // Search failed due to MaxSet limit.
536 return nil
537 }
538 b.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add. = b.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add.[:old]
539
540 // Adding the specific match we found in 0suffix, recurse to narrow down 1suffix.
541 b.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add. = append(b.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add.[:old], bad0...)
542 r1 = b.Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Run().("1" + suffix)
543 if r1.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success. {
544 // 1suffix + b.Add + bad0 = bad0 + b.Add + 1suffix is what b.search(r0) reported as a failure.
545 b.Fatalfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Fatalf().("target fails inconsistently")
546 }
547 bad1 := b.searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#search().(r1)
548 if bad1 == nil {
549 // Search failed due to MaxSet limit.
550 return nil
551 }
552 b.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add. = b.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add.[:old]
553
554 // bad0 and bad1 together provoke the failure.
555 return append(bad0, bad1...)
556 }
557
558 // Run runs a set of trials selecting changes with the given suffix,
559 // plus the ones in b.Add and not the ones in b.Skip.
560 // The returned result's MatchIDs, MatchText, and MatchFull
561 // only list the changes that match suffix.
562 // When b.Count > 1, Run runs b.Count trials and requires
563 // that they all succeed or they all fail. If not, it calls b.Fatalf.
564 func (b *Bisectscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#) Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Run().(suffix string) *Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result# {
565 out := b.runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#run().(suffix)
566 for i := 1; i < b.Countscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Count.; i++ {
567 r := b.runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#run().(suffix)
568 if r.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success. != out.Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success. {
569 b.Fatalfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Fatalf().("target fails inconsistently")
570 }
571 }
572 return out
573 }
574
575 // run runs a single trial for Run.
576 func (b *Bisectscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#) runscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#run().(suffix string) *Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result# {
577 random := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprint().(randscip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/.Uint64scip-go gomod github.com/golang/go/src go1.25.0 `math/rand`/Uint64().())
578
579 // Accept suffix == "v" to mean we need user-visible output.
580 visible := ""
581 if suffix == "v" {
582 visible = "v"
583 suffix = ""
584 }
585
586 // Construct change ID pattern.
587 var pattern string
588 if suffix == "y" || suffix == "n" {
589 pattern = suffix
590 suffix = ""
591 } else {
592 var elem []string
593 if suffix != "" {
594 elem = append(elem, "+", suffix)
595 }
596 for _, x := range b.Addscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Add. {
597 elem = append(elem, "+", x)
598 }
599 for _, x := range b.Skipscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Skip. {
600 elem = append(elem, "-", x)
601 }
602 pattern = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(elem, "")
603 if pattern == "" {
604 pattern = "y"
605 }
606 }
607 if b.Disablescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Disable. {
608 pattern = "!" + pattern
609 }
610 pattern = visible + pattern
611
612 // Construct substituted env and args.
613 env := make([]string, len(b.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Env.))
614 for i, x := range b.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Env. {
615 k, v, _ := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Cutscip-go gomod github.com/golang/go/src go1.25.0 strings/Cut().(x, "=")
616 env[i] = k + "=" + replacescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/replace().(v, pattern, random)
617 }
618 args := make([]string, len(b.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Args.))
619 for i, x := range b.Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Args. {
620 args[i] = replacescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/replace().(x, pattern, random)
621 }
622
623 // Construct and log command line.
624 // There is no newline in the log print.
625 // The line will be completed when the command finishes.
626 cmdText := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(append(append(env, b.Cmdscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Cmd.), args...), " ")
627 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(b.Stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Stderr., "bisect: run: %s...", cmdText)
628
629 // Run command with args and env.
630 var out []byte
631 var err error
632 if b.TestRunscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#TestRun. != nil {
633 out, err = b.TestRunscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#TestRun.(env, b.Cmdscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Cmd., args)
634 } else {
635 ctx := contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.0 context/Background().()
636 if b.Timeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Timeout. != 0 {
637 var cancel contextscip-go gomod github.com/golang/go/src go1.25.0 context/.CancelFuncscip-go gomod github.com/golang/go/src go1.25.0 context/CancelFunc#
638 ctx, cancel = contextscip-go gomod github.com/golang/go/src go1.25.0 context/.WithTimeoutscip-go gomod github.com/golang/go/src go1.25.0 context/WithTimeout().(ctx, b.Timeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Timeout.)
639 defer cancel()
640 }
641 cmd := execscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/.CommandContextscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/CommandContext().(ctx, b.Cmdscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Cmd., args...)
642 cmd.Envscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#Env. = append(osscip-go gomod github.com/golang/go/src go1.25.0 os/.Environscip-go gomod github.com/golang/go/src go1.25.0 os/Environ().(), env...)
643 // Set up cmd.Cancel, cmd.WaitDelay on Go 1.20 and later
644 // TODO(rsc): Inline go120.go's cmdInterrupt once we stop supporting Go 1.19.
645 cmdInterruptscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/cmdInterrupt().(cmd)
646 out, err = cmd.CombinedOutputscip-go gomod github.com/golang/go/src go1.25.0 `os/exec`/Cmd#CombinedOutput().()
647 }
648
649 // Parse output to construct result.
650 r := &Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#{
651 Suffixscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Suffix.: suffix,
652 Successscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Success.: err == nil,
653 Cmdscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Cmd.: cmdText,
654 Outscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Out.: string(out),
655 }
656
657 // Calculate bits, mask to identify suffix matches.
658 var bits, mask uint64
659 if suffix != "" && suffix != "y" && suffix != "n" && suffix != "v" {
660 var err error
661 bits, err = strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseUintscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseUint().(suffix, 2, 64)
662 if err != nil {
663 b.Fatalfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Fatalf().("internal error: bad suffix")
664 }
665 mask = uint64(1<<len(suffix)) - 1
666 }
667
668 // Process output, collecting match reports for suffix.
669 have := make(map[uint64]bool)
670 all := r.Outscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Out.
671 for all != "" {
672 var line string
673 line, all, _ = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Cutscip-go gomod github.com/golang/go/src go1.25.0 strings/Cut().(all, "\n")
674 short, id, ok := bisectscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/bisect`/.CutMarkerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/bisect`/CutMarker().(line)
675 if !ok || (id&mask) != bits {
676 continue
677 }
678
679 if !have[id] {
680 have[id] = true
681 r.MatchIDsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchIDs. = append(r.MatchIDsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchIDs., id)
682 }
683 r.MatchTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchText. = append(r.MatchTextscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchText., short)
684 r.MatchFullscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchFull. = append(r.MatchFullscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchFull., line)
685 }
686
687 // Finish log print from above, describing the command's completion.
688 if err == nil {
689 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(b.Stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Stderr., " ok (%d matches)\n", len(r.MatchIDsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchIDs.))
690 } else {
691 fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Fprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Fprintf().(b.Stderrscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Stderr., " FAIL (%d matches)\n", len(r.MatchIDsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchIDs.))
692 }
693
694 if err != nil && len(r.MatchIDsscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchIDs.) == 0 {
695 b.Fatalfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Fatalf().("target failed without printing any matches\n%s", r.Outscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#Out.)
696 }
697
698 // In verbose mode, print extra debugging: all the lines with match markers.
699 if b.Verbosescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Verbose. {
700 b.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Bisect#Logf().("matches:\n%s", stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(r.MatchFullscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/Result#MatchFull., "\n\t"))
701 }
702
703 return r
704 }
705
706 // replace returns x with literal text PATTERN and RANDOM replaced by pattern and random.
707 func replacescip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/replace().(x, pattern, random string) string {
708 x = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.0 strings/ReplaceAll().(x, "PATTERN", pattern)
709 x = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.ReplaceAllscip-go gomod github.com/golang/go/src go1.25.0 strings/ReplaceAll().(x, "RANDOM", random)
710 return x
711 }
712
713 // commonSuffix returns the longest common binary suffix shared by all uint64s in list.
714 // If list is empty, commonSuffix returns an empty string.
715 func commonSuffixscip-go gomod golang.org/x/tools . `golang.org/x/tools/cmd/bisect`/commonSuffix().(list []uint64) string {
716 if len(list) == 0 {
717 return ""
718 }
719 b := list[0]
720 n := 64
721 for _, x := range list {
722 for x&((1<<n)-1) != b {
723 n--
724 b &= (1 << n) - 1
725 }
726 }
727 s := make([]byte, n)
728 for i := n - 1; i >= 0; i-- {
729 s[i] = '0' + byte(b&1)
730 b >>= 1
731 }
732 return string(s[:])
733 }