bazelbuild/bazel · error · NumberFormatException

empty string

Error message

empty string

What it means

StarlarkInt.parse (backing int(s, base)) rejects the empty string immediately with NumberFormatException('empty string'). The host then typically converts this into an EvalException like int(): <input>. The parser cannot infer any value from zero characters, unlike len-based defaults a caller might imagine.

Source

Thrown at src/main/java/net/starlark/java/eval/StarlarkInt.java:108

   * Returns the StarlarkInt value that most closely approximates x.
   *
   * @throws IllegalArgumentException is x is not finite.
   */
  static StarlarkInt ofFiniteDouble(double x) {
    return StarlarkFloat.finiteDoubleToIntExact(x);
  }

  /**
   * Returns the int denoted by a literal string in the specified base, as if by the Starlark
   * expression {@code int(s, base)}.
   *
   * @throws NumberFormatException if the input is invalid.
   */
  public static StarlarkInt parse(String s, int base) {
    String stringForErrors = s;

    if (s.isEmpty()) {
      throw new NumberFormatException("empty string");
    }

    // +/- prefix?
    boolean isNegative = false;
    char c = s.charAt(0);
    if (c == '+') {
      s = s.substring(1);
    } else if (c == '-') {
      s = s.substring(1);
      isNegative = true;
    }

    String digits = s;

    // 0b 0o 0x prefix?
    if (s.length() > 1 && s.charAt(0) == '0') {
      int prefixBase = 0;
      c = s.charAt(1);

View on GitHub (pinned to e6e199d060)

Solutions

  1. Check for empty/blank before converting: if s.strip(): n = int(s) else: n = 0.
  2. Fix upstream producers so numeric fields are never empty (emit "0").
  3. Treat blank as a sentinel explicitly instead of relying on the parser.

Example fix

# before
port = int(raw_port_field)  # field may be ""

# after
port = int(raw_port_field) if raw_port_field.strip() else 0
Defensive patterns

Strategy: validation

Validate before calling

def parse_int(s, base=10, default=None):
    s = s.strip()
    if not s:
        if default == None:
            fail("empty string passed to int()")
        return default
    return int(s, base)

Type guard

def is_parseable_int(s):
    return len(s.strip()) > 0

Prevention

When it happens

Trigger: int(""), int("", 16), or int(s.strip()) where s is whitespace-only or an unset string attribute; parsing empty fields from split lines ("a,,b".split(",")).

Common situations: Parsing CSV/TSV or config lines with optional numeric fields; calling .strip() on user input before int() so " " becomes ""; defaults like "" on string attributes that later must be numeric.

Related errors


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