{"record":{"id":"63f966bfed10f911","repo":"karatelabs/karate","slug":"def-requires-assignment-text","errorCode":null,"errorMessage":"def requires '=' assignment: <text>","messagePattern":"def requires '=' assignment: <text>","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/StepExecutor.java","lineNumber":356,"sourceCode":"            result.setCallResults(stepCallResults);\n        }\n        // Wipe AFTER attaching so the next step starts with a clean buffer.\n        stepCallResults = null;\n    }\n\n    // ========== Expression Execution ==========\n\n    private void executeExpression(Step step) {\n        runtime.eval(step.getText(), step);\n    }\n\n    // ========== Variable Assignment ==========\n\n    private void executeDef(Step step) {\n        String text = step.getText();\n        int eqIndex = StepUtils.findAssignmentOperator(text);\n        if (eqIndex < 0) {\n            throw new RuntimeException(\"def requires '=' assignment: \" + text);\n        }\n        String name = text.substring(0, eqIndex).trim();\n        validateVariableName(name);\n        String expr = text.substring(eqIndex + 1).trim();\n        String docString = step.getDocString();\n\n        // Handle docstring if expression is empty (docstring IS the RHS expression).\n        // Null it afterwards so it isn't ALSO treated as a separate call argument below.\n        if (expr.isEmpty() && docString != null) {\n            expr = docString;\n            docString = null;\n        }\n\n        // Check if RHS is a special karate expression (not standard JS)\n        if (expr.startsWith(\"call \")) {\n            String callExpr = appendDocStringCallArg(expr.substring(5).trim(), docString);\n            executeCallWithResult(callExpr, name);\n        } else if (expr.startsWith(\"callonce \")) {","sourceCodeStart":338,"sourceCodeEnd":374,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/StepExecutor.java#L338-L374","documentation":"The 'def' keyword in Karate assigns a variable from an expression and therefore requires an '=' assignment operator, e.g. 'def foo = call read(...)'. If StepUtils.findAssignmentOperator finds no '=' in the step text, Karate throws this RuntimeException naming the offending text.","triggerScenarios":"Writing a def step without '=', such as 'def myVar call read(\"other.feature\")' or 'def result someFunction()' — the '=' between the variable name and the expression is missing.","commonSituations":"Translating from other DSLs where assignment is implicit; forgetting '=' after a rename refactor; multi-line def steps where the '=' got lost; confusing 'def' (assignment) with 'call' (invoke-only) steps.","solutions":["Add '=' after the variable name: 'def <name> = <expression>'","If the step is meant to only invoke something without assigning, drop 'def' and use 'call' or 'eval' directly","Verify no comment or line-wrap swallowed the '=' in multi-line steps"],"exampleFix":"// before (feature)\ndef result call read('helper.feature')\n// after\ndef result = call read('helper.feature')","handlingStrategy":"validation","validationCode":"// ensure def lines carry an assignment operator before running\nstatic boolean isWellFormedDef(String stepText) {\n    String t = stepText.trim();\n    if (!t.startsWith(\"def \")) return true;\n    int i = t.indexOf('=', 4);\n    return i > 4 && !t.substring(4, i).trim().contains(\" \");\n}","typeGuard":null,"tryCatchPattern":"try { executor.execute(step); } catch (RuntimeException e) { if (e.getMessage().startsWith(\"def requires '=' assignment\")) { throw new IllegalStateException(\"add '=' to def step: \" + e.getMessage()); } throw e; }","preventionTips":["Always write 'def name = expression'","Do not confuse def (assign) with call (invoke)","Check multi-line steps did not lose the '='"],"tags":["karate","feature-file","syntax","def"],"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"}