{"record":{"id":"083b4dea25cdf4ea","repo":"golang/go","slug":"found-d-matches-for-q-in-s","errorCode":null,"errorMessage":"found %d matches for %#q in %s","messagePattern":"found (.+?) matches for %#q in (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/internal/script/cmds.go","lineNumber":696,"sourceCode":"\tpattern := `(?m)` + args[0]\n\tre, err := regexp.Compile(pattern)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tif isGrep {\n\t\tname = args[1] // for error messages\n\t\tdata, err := os.ReadFile(s.Path(args[1]))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\ttext = string(data)\n\t}\n\n\tif n > 0 {\n\t\tcount := len(re.FindAllString(text, -1))\n\t\tif count != n {\n\t\t\treturn fmt.Errorf(\"found %d matches for %#q in %s\", count, pattern, name)\n\t\t}\n\t\treturn nil\n\t}\n\n\tif !re.MatchString(text) {\n\t\treturn fmt.Errorf(\"no match for %#q in %s\", pattern, name)\n\t}\n\n\tif !quiet {\n\t\t// Print the lines containing the match.\n\t\tloc := re.FindStringIndex(text)\n\t\tfor loc[0] > 0 && text[loc[0]-1] != '\\n' {\n\t\t\tloc[0]--\n\t\t}\n\t\tfor loc[1] < len(text) && text[loc[1]] != '\\n' {\n\t\t\tloc[1]++\n\t\t}\n\t\tlines := strings.TrimSuffix(text[loc[0]:loc[1]], \"\\n\")","sourceCodeStart":678,"sourceCodeEnd":714,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/internal/script/cmds.go#L678-L714","documentation":"Thrown by the match function (Grep/Stdout/Stderr commands) when -count=N is specified and the actual number of regexp matches differs from N. The %d shows the actual count found, %#q shows the pattern, and %s shows the target name (file for grep, 'stdout'/'stderr' otherwise). This is an assertion that the pattern matches exactly N times.","triggerScenarios":"re.FindAllString(text, -1) returns a slice whose length != n. The test script expected exactly N matches but found a different count.","commonSituations":"Test output changed (more or fewer log lines, warnings, errors) after a code change, breaking the exact-count assertion. Also occurs when the regex is too broad or too narrow, or when platform-specific output varies the match count.","solutions":["Run with -v to see actual output and count matches manually to determine the correct N.","Update -count=N to match the new actual count if the change is intentional.","Tighten or loosen the regex pattern so it matches exactly the intended lines.","Omit -count if you only need to assert at least one match rather than an exact count."],"exampleFix":"// Before: stdout -count=2 'error:'\n// After:  stdout -count=3 'error:'  // if a new expected error line was added","handlingStrategy":"validation","validationCode":"// Pre-count matches in test setup to determine the correct N:\nfunc countMatches(text, pattern string) (int, error) {\n    re, err := regexp.Compile(pattern)\n    if err != nil { return 0, err }\n    return len(re.FindAllString(text, -1)), nil\n}\n// Then use the discovered count as -count=N in the script.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use -v to see actual output and manually verify match counts.","Update -count=N when expected output changes.","Omit -count if an exact match count is not required — default asserts at least one match."],"tags":["script","test-framework","grep","match-count","assertion","go-compiler-internal"],"backgroundTag":null,"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}