bazelbuild/bazel · error · IOException

-c <cmd> flag needs an argument

Error message

-c <cmd> flag needs an argument

What it means

The standalone Starlark interpreter command (net.starlark.java.cmd.Main) accepts only -c, -cpuprofile and one optional script file. If '-c' is the last argument, no command string follows, so argument parsing fails with this IOException before any script runs.

Source

Thrown at src/main/java/net/starlark/java/cmd/Main.java:188

  public static void main(String[] args) throws IOException {
    String file = null;
    String cmd = null;
    String cpuprofile = null;

    // parse flags
    int i;
    for (i = 0; i < args.length; i++) {
      if (!args[i].startsWith("-")) {
        break;
      }
      if (args[i].equals("--")) {
        i++;
        break;
      }
      if (args[i].equals("-c")) {
        if (i + 1 == args.length) {
          throw new IOException("-c <cmd> flag needs an argument");
        }
        cmd = args[++i];
      } else if (args[i].equals("-cpuprofile")) {
        if (i + 1 == args.length) {
          throw new IOException("-cpuprofile <file> flag needs an argument");
        }
        cpuprofile = args[++i];
      } else {
        throw new IOException("unknown flag: " + args[i]);
      }
    }
    // positional arguments
    if (i < args.length) {
      if (i + 1 < args.length) {
        throw new IOException("too many positional arguments");
      }
      file = args[i];
    }

View on GitHub (pinned to e6e199d060)

Solutions

  1. Supply the command string: `starlark -c 'print(1)'`.
  2. Quote shell expansions so empty variables become explicit empty strings or are checked first.
  3. Validate arguments before invoking the process from Java (check length after '-c').

Example fix

# before
starlark -c

# after
starlark -c 'print("hello")'
Defensive patterns

Strategy: validation

Validate before calling

# Validate argv before launching the CLI
if [ "$#" -lt 2 ] || [ "$1" != "-c" ]; then echo "usage: $0 -c <cmd> [file]" >&2; exit 2; fi
exec starlark "$@"

Prevention

When it happens

Trigger: Running `starlark -c` with nothing after it; a shell script that builds the command line with an unset variable (`starlark -c $CMD` where CMD is empty and unquoted expansion drops it).

Common situations: Shell variable quoting mistakes; CI script passing an empty -c payload; copy-paste truncation of an example command.

Related errors


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