{"record":{"id":"6a832053b5394fba","repo":"bazelbuild/bazel","slug":"cpuprofile-file-flag-needs-an-argument","errorCode":null,"errorMessage":"-cpuprofile <file> flag needs an argument","messagePattern":"-cpuprofile <file> flag needs an argument","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"src/main/java/net/starlark/java/cmd/Main.java","lineNumber":193,"sourceCode":"\n    // parse flags\n    int i;\n    for (i = 0; i < args.length; i++) {\n      if (!args[i].startsWith(\"-\")) {\n        break;\n      }\n      if (args[i].equals(\"--\")) {\n        i++;\n        break;\n      }\n      if (args[i].equals(\"-c\")) {\n        if (i + 1 == args.length) {\n          throw new IOException(\"-c <cmd> flag needs an argument\");\n        }\n        cmd = args[++i];\n      } else if (args[i].equals(\"-cpuprofile\")) {\n        if (i + 1 == args.length) {\n          throw new IOException(\"-cpuprofile <file> flag needs an argument\");\n        }\n        cpuprofile = args[++i];\n      } else {\n        throw new IOException(\"unknown flag: \" + args[i]);\n      }\n    }\n    // positional arguments\n    if (i < args.length) {\n      if (i + 1 < args.length) {\n        throw new IOException(\"too many positional arguments\");\n      }\n      file = args[i];\n    }\n\n    if (cpuprofile != null) {\n      FileOutputStream out = new FileOutputStream(cpuprofile);\n      Starlark.startCpuProfile(out, Duration.ofMillis(10));\n    }","sourceCodeStart":175,"sourceCodeEnd":211,"githubUrl":"https://github.com/bazelbuild/bazel/blob/e6e199d0601a244511b4cf18c8b2828aa73db1fd/src/main/java/net/starlark/java/cmd/Main.java#L175-L211","documentation":"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.","triggerScenarios":"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.","commonSituations":"Profiling scripts where the profile path variable is empty; flag ordering mistakes after copying documentation examples.","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')."],"exampleFix":"# before\nstarlark -cpuprofile\n\n# after\nstarlark -cpuprofile /tmp/cpu.prof script.star","handlingStrategy":"validation","validationCode":"PROFILE=\"${PROFILE:-/tmp/cpu.prof}\"  # default instead of empty\n[ -n \"$PROFILE\" ] && exec starlark -cpuprofile \"$PROFILE\" \"$@\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["starlark","cli","argument-parsing","profiling"],"backgroundTag":null,"analyzedSha":"e6e199d0601a244511b4cf18c8b2828aa73db1fd","analyzedAt":"2026-08-14T10:24:27.848Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}