{"record":{"id":"177f10681c914aea","repo":"kovidgoyal/kitty","slug":"failed-to-compile-word-regex-q-w","errorCode":null,"errorMessage":"Failed to compile word_regex %q: %w","messagePattern":"Failed to compile word_regex %q: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kittens/diff/patch.go","lineNumber":408,"sourceCode":"\t\ttitle = strings.TrimSpace(parts[2])\n\t}\n\tleft, right, _ := strings.Cut(linespec, \" \")\n\tls, lc := parse_range(left)\n\trs, rc := parse_range(right)\n\treturn &Hunk{\n\t\ttitle: title, left_start: ls - 1, left_count: lc, right_start: rs - 1, right_count: rc,\n\t\tlargest_line_number: utils.Max(ls-1+lc, rs-1+rc),\n\t}\n}\n\nfunc (self *Patch) compute_centers(left_lines, right_lines []string) error {\n\tword_mode := conf != nil && conf.Word_diff_mode == Word_diff_mode_words\n\tvar re *regexp.Regexp\n\tif word_mode {\n\t\tvar err error\n\t\tre, err = word_regexp()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"Failed to compile word_regex %q: %w\", conf.Word_regex, err)\n\t\t}\n\t}\n\n\ttype pair struct {\n\t\tchunk *Chunk\n\t\tidx   int\n\t}\n\tvar pairs []pair\n\tfor _, hunk := range self.all_hunks {\n\t\tfor _, chunk := range hunk.chunks {\n\t\t\tif !chunk.is_context && chunk.left_count == chunk.right_count {\n\t\t\t\tfor i := 0; i < chunk.left_count; i++ {\n\t\t\t\t\tpairs = append(pairs, pair{chunk, i})\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\tif len(pairs) == 0 {","sourceCodeStart":390,"sourceCodeEnd":426,"githubUrl":"https://github.com/kovidgoyal/kitty/blob/6d5d0c440603ad9bdf6dcd599f73f6dde21acb44/kittens/diff/patch.go#L390-L426","documentation":"In word-diff mode (Word_diff_mode_words), compute_centers compiles a regex before processing chunks. The pattern comes from word_regexp(), which builds from the configured word_regex; if the user's word_regex in kitty.conf is invalid regexp syntax, compilation fails and the formatted error includes the offending pattern and the underlying regexp error.","triggerScenarios":"Setting `word_regex` in kitty.conf to an invalid Go regexp (e.g. unbalanced parenthesis, unsupported look-around like (?=...) or backreferences, trailing backslash) and enabling word diff mode; Go's RE2 engine rejects PCRE-isms such as lookahead and \\1 backreferences.","commonSituations":"Copying a word_regex from git/diff tooling that uses PCRE syntax (lookaheads, backrefs) which Go's regexp does not support; stray quoting/escaping from shell or config formatting; regex fragments valid in Perl but invalid in RE2.","solutions":["Fix or remove the `word_regex` setting in kitty.conf.","Test your pattern with Go semantics: `go run` a one-liner using regexp.Compile, or at minimum avoid (?=, (?!, and \\1-style backreferences.","Quote the value correctly in kitty.conf so backslashes aren't mangled."],"exampleFix":"# before (kitty.conf)\nword_regex (?<=\\s)\\w+|\\w+(?=\\s)\n\n# after\nword_regex \\w+","handlingStrategy":"validation","validationCode":"// Go: validate the configured regex before starting the kitten\nif conf.Word_diff_mode == Word_diff_mode_words {\n    if _, err := regexp.Compile(conf.Word_regex); err != nil {\n        return fmt.Errorf(\"bad word_regex: %w\", err)\n    }\n}","typeGuard":"func validWordRegex(s string) bool {\n    _, err := regexp.Compile(s)\n    return err == nil\n}","tryCatchPattern":"Parse the error text for 'Failed to compile word_regex'; on match, disable word-diff mode or fall back to the default regex, and prompt the user to fix kitty.conf.","preventionTips":["Test word_regex values with Go's regexp (RE2), not PCRE.","Avoid lookarounds and backreferences; RE2 doesn't support them.","Quote regexes literally in kitty.conf to prevent escape mangling."],"tags":["kitty","diff-kitten","regex","configuration","go-regexp"],"backgroundTag":"invalid-regex-pattern","analyzedSha":"6d5d0c440603ad9bdf6dcd599f73f6dde21acb44","analyzedAt":"2026-08-27T14:20:20.142Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}