bazelbuild/bazel · error · IOException
-cpuprofile <file> flag needs an argument
Error message
-cpuprofile <file> flag needs an argument
What it means
The Starlark interpreter CLI requires '-cpuprofile' to be immediately followed by a file path. If the flag is the last token on the command line, no profile output file is available and parsing aborts with this IOException.
Source
Thrown at src/main/java/net/starlark/java/cmd/Main.java:193
// 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];
}
if (cpuprofile != null) {
FileOutputStream out = new FileOutputStream(cpuprofile);
Starlark.startCpuProfile(out, Duration.ofMillis(10));
}View on GitHub (pinned to e6e199d060)
Solutions
- Pass a path: `starlark -cpuprofile /tmp/cpu.prof script.star`.
- Check the profile-path variable is non-empty before building the command line.
- Ensure the path does not start with '-' (or use './-name').
Example fix
# before starlark -cpuprofile # after starlark -cpuprofile /tmp/cpu.prof script.star
Defensive patterns
Strategy: validation
Validate before calling
PROFILE="${PROFILE:-/tmp/cpu.prof}" # default instead of empty
[ -n "$PROFILE" ] && exec starlark -cpuprofile "$PROFILE" "$@" Prevention
- Default profile paths in wrapper scripts instead of passing possibly-empty variables.
- Ensure flag arguments never begin with '-'.
- Smoke-test wrapper scripts with 'sh -x' before CI use.
When it happens
Trigger: Running `starlark -cpuprofile` with no filename; a wrapper script that appends -cpuprofile conditionally but drops the path; typo where the path starts with '-' and is mistaken for another flag consuming it.
Common situations: Profiling scripts where the profile path variable is empty; flag ordering mistakes after copying documentation examples.
Related errors
- -c <cmd> flag needs an argument
- unknown flag: %s
- too many positional arguments
- Usage: %s -- command arg1 @args
- \nUsage: %s -- command arg1 @args\n
AI-assisted analysis of bazelbuild/bazel@e6e199d060 (2026-08-14).
Data as JSON: /api/errors/6a832053b5394fba.
Report an issue: GitHub.