{"record":{"id":"fb838f23c6d3f5ec","repo":"golang/go","slug":"bad-count-must-be-at-least-1","errorCode":null,"errorMessage":"bad -count=: must be at least 1","messagePattern":"bad -count=: must be at least 1","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/internal/script/cmds.go","lineNumber":658,"sourceCode":"\t\t},\n\t\tfunc(s *State, args ...string) (WaitFunc, error) {\n\t\t\treturn nil, match(s, args, \"\", \"grep\")\n\t\t})\n}\n\nconst matchUsage = \"[-count=N] [-q] 'pattern'\"\n\n// match implements the Grep, Stdout, and Stderr commands.\nfunc match(s *State, args []string, text, name string) error {\n\tn := 0\n\tif len(args) >= 1 && strings.HasPrefix(args[0], \"-count=\") {\n\t\tvar err error\n\t\tn, err = strconv.Atoi(args[0][len(\"-count=\"):])\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"bad -count=: %v\", err)\n\t\t}\n\t\tif n < 1 {\n\t\t\treturn fmt.Errorf(\"bad -count=: must be at least 1\")\n\t\t}\n\t\targs = args[1:]\n\t}\n\tquiet := false\n\tif len(args) >= 1 && args[0] == \"-q\" {\n\t\tquiet = true\n\t\targs = args[1:]\n\t}\n\n\tisGrep := name == \"grep\"\n\n\twantArgs := 1\n\tif isGrep {\n\t\twantArgs = 2\n\t}\n\tif len(args) != wantArgs {\n\t\treturn ErrUsage\n\t}","sourceCodeStart":640,"sourceCodeEnd":676,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/internal/script/cmds.go#L640-L676","documentation":"Thrown by the match function when the -count=N flag value parses successfully as an integer but is less than 1. The match function requires at least one expected match; zero or negative counts are meaningless and rejected after the Atoi parse succeeds.","triggerScenarios":"args[0] is like \"-count=0\" or \"-count=-5\" — strconv.Atoi succeeds returning a value < 1, triggering the `n < 1` check.","commonSituations":"Using -count=0 to assert 'no matches', which is incorrect — omit the -count flag and rely on the pattern not matching instead. Or a sign error / off-by-one producing a negative count.","solutions":["Use -count=1 or higher to assert an exact positive match count.","To assert a pattern does NOT appear, simply omit -count and let the default no-match behavior apply (the pattern should not match).","Check for sign errors if a negative value was unexpected."],"exampleFix":"// Before (broken): stdout -count=0 'pattern'   // trying to assert no match\n// After (fixed):  ! stdout 'pattern'                   // negate to assert absence","handlingStrategy":"validation","validationCode":"// Validate -count is at least 1:\nfunc validateCountMin(arg string) error {\n    if !strings.HasPrefix(arg, \"-count=\") { return nil }\n    n, _ := strconv.Atoi(arg[len(\"-count=\"):])\n    if n < 1 {\n        return fmt.Errorf(\"-count must be >= 1; to assert no match, negate the command with !\")\n    }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use -count=N with N >= 1 for exact match assertions.","To assert a pattern does NOT appear, prefix the command with ! (e.g. ! stdout 'pattern').","Do not use -count=0 — it is meaningless and will always error."],"tags":["script","test-framework","grep","count-flag","validation","go-compiler-internal"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}