{"record":{"id":"9a29bc69b3eb3652","repo":"karatelabs/karate","slug":"fork-needs-at-least-one-argument","errorCode":null,"errorMessage":"fork() needs at least one argument","messagePattern":"fork\\(\\) needs at least one argument","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJs.java","lineNumber":855,"sourceCode":"\n    /**\n     * karate.fork() - Asynchronous process execution.\n     * Returns ProcessHandle for async control.\n     * Usage:\n     * var proc = karate.fork('ping google.com')\n     * var proc = karate.fork({ args: ['node', 'server.js'], listener: fn })\n     * var proc = karate.fork({ args: [...], start: false })  // deferred start\n     * proc.onStdOut(fn).start()\n     * proc.waitSync()\n     * proc.stdOut\n     * proc.exitCode\n     * proc.close()\n     */\n    @SuppressWarnings(\"unchecked\")\n    private JavaInvokable fork() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"fork() needs at least one argument\");\n            }\n            ProcessBuilder builder = ProcessBuilder.create();\n            Consumer<String> listener = null;\n            Consumer<String> errorListener = null;\n            boolean autoStart = true;\n\n            Object arg = args[0];\n            if (arg instanceof String) {\n                builder.line((String) arg);\n            } else if (arg instanceof List) {\n                builder.args((List<String>) arg);\n            } else if (arg instanceof Map) {\n                Map<String, Object> options = (Map<String, Object>) arg;\n                builder = ProcessBuilder.fromMap(options);\n\n                // Extract listener function (receives line string directly)\n                Object listenerObj = options.get(\"listener\");\n                if (listenerObj instanceof JavaCallable jsListener) {","sourceCodeStart":837,"sourceCodeEnd":873,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L837-L873","documentation":"Karate's JS `karate.fork()` spawns a background OS process and needs the command to run. Called with zero arguments there is nothing to fork, so the library throws immediately instead of returning a useless ProcessHandle.","triggerScenarios":"Calling karate.fork() with no arguments at all, or with only trailing/optional-style arguments that all evaluate such that args.length == 0 (e.g. karate.fork.apply([]) style invocation or a spread of an empty array).","commonSituations":"Building the argument dynamically from a variable that is empty/undefined so the call site effectively passes nothing; refactor that dropped the argument; assuming fork() with no args starts a default shell like some other tools do.","solutions":["Pass at least one argument: karate.fork('server.js') or karate.fork(['node', 'server.js'])","If building args dynamically, validate the array is non-empty before calling: if (args.length) karate.fork(args)","Pass an options Map with the command: karate.fork({ args: [...], listen: true })","Check for typos where the command variable resolves to empty at call time"],"exampleFix":"// before\nvar cmd = []; // built dynamically, ended up empty\nkarate.fork() // throws\n// after\nif (cmd.length === 0) karate.fail('no command to fork');\nkarate.fork(cmd);","handlingStrategy":"validation","validationCode":"// JS\nif (!cmd || (Array.isArray(cmd) && cmd.length === 0)) throw new Error('fork command missing');\nkarate.fork(cmd);","typeGuard":null,"tryCatchPattern":"try { var h = karate.fork(cmd); } catch (e) { if (('' + e).indexOf('fork() needs at least one argument') !== -1) { karate.log('fork called with no command'); } throw e; }","preventionTips":["Never call fork() without a command argument","Validate dynamically built command arrays are non-empty","Remember fork() has no default command"],"tags":["javascript","process-execution","missing-argument"],"backgroundTag":"missing-required-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}