bazelbuild/bazel · error · OptionsParsingException

Not a valid regular expression: " + e.getMessage()

Error message

Not a valid regular expression: " + e.getMessage()

What it means

Thrown by Converters.RegexPatternConverter.convert when Pattern.compile() rejects the input (after StringEncoding.internalToUnicode unescaping) with a PatternSyntaxException. The converter exists to fail fast at option-parsing time so an invalid regular expression never reaches the code that uses it; the message embeds the underlying Java regex error and position.

Source

Thrown at src/main/java/com/google/devtools/common/options/Converters.java:447

      throw new OptionsParsingException("Not one of " + values);
    }

    @Override
    public String getTypeDescription() {
      return joinEnglishList(values);
    }
  }

  /** Checks whether a string is a valid regex pattern and compiles it. */
  public static class RegexPatternConverter extends Converter.Contextless<RegexPatternOption> {

    @Override
    public RegexPatternOption convert(String input) throws OptionsParsingException {
      try {
        return RegexPatternOption.create(
            Pattern.compile(StringEncoding.internalToUnicode(input), Pattern.DOTALL));
      } catch (PatternSyntaxException e) {
        throw new OptionsParsingException("Not a valid regular expression: " + e.getMessage());
      }
    }

    @Override
    public String getTypeDescription() {
      return "a valid Java regular expression";
    }
  }

  /** Checks whether an integer is in the given range. */
  public static class RangeConverter extends Converter.Contextless<Integer> {
    final int minValue;
    final int maxValue;

    public RangeConverter(int minValue, int maxValue) {
      this.minValue = minValue;
      this.maxValue = maxValue;
    }

View on GitHub (pinned to e6e199d060)

Solutions

  1. Fix the regex per the PatternSyntaxException detail in the message (it names the index of the problem).
  2. Escape literal metacharacters: MyTest\.test\[1\].
  3. Test the pattern in isolation first: jshell> Pattern.compile(args[0]).
  4. Mind shell quoting: use single quotes so backslashes survive to Java.

Example fix

# before
bazel test --test_filter='MyTest.test[1]' //...
# after
bazel test --test_filter='MyTest\.test\[1\]' //...
Defensive patterns

Strategy: validation

Validate before calling

boolean isParsableRegex(String s) {
  if (s == null) return false;
  try { Pattern.compile(s, Pattern.DOTALL); return true; }
  catch (PatternSyntaxException e) { return false; }
}

Type guard

boolean isValidRegexFlagValue(String s) {
  if (s == null || s.isEmpty()) return false;
  try { java.util.regex.Pattern.compile(s, java.util.regex.Pattern.DOTALL); return true; }
  catch (java.util.regex.PatternSyntaxException e) { return false; }
}

Try / catch

try {
  RegexPatternOption p = new Converters.RegexPatternConverter().convert(input);
} catch (OptionsParsingException e) {
  // e.getMessage() carries the PatternSyntaxException detail; surface it to the user verbatim
}

Prevention

When it happens

Trigger: Passing --test_filter=Foo( to an option using RegexPatternConverter; unmatched brackets/parens, dangling backslashes, invalid escapes like \y, or malformed repetition like a{2,1}.

Common situations: Method filters with unescaped regex metacharacters (MyTest.test[1]); backslash handling lost through shell quoting; patterns written for a different regex dialect (Perl/POSIX) using unsupported syntax.

Related errors


AI-assisted analysis of bazelbuild/bazel@e6e199d060 (2026-08-14). Data as JSON: /api/errors/74b2c2f8e52fd21e. Report an issue: GitHub.