{"record":{"id":"159ccc48241ae8bb","repo":"alibaba/spring-ai-alibaba","slug":"failed-to-execute-code","errorCode":null,"errorMessage":"Failed to execute code","messagePattern":"Failed to execute code","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"spring-boot-starters/spring-ai-alibaba-starter-builtin-nodes/src/main/java/com/alibaba/cloud/ai/graph/node/code/LocalCommandlineCodeExecutor.java","lineNumber":164,"sourceCode":"\t\tByteArrayOutputStream outputStream = new ByteArrayOutputStream();\n\t\tByteArrayOutputStream errorStream = new ByteArrayOutputStream();\n\t\texecutor.setStreamHandler(new PumpStreamHandler(outputStream, errorStream));\n\n\t\t// Set timeout\n\t\texecutor.setWatchdog(new ExecuteWatchdog(TimeUnit.SECONDS.toMillis(config.getTimeout())));\n\n\t\ttry {\n\t\t\texecutor.execute(commandLine);\n\t\t\treturn new CodeExecutionResult(0, outputStream.toString().trim());\n\t\t}\n\t\tcatch (ExecuteException e) {\n\t\t\tString errorOutput = errorStream.toString()\n\t\t\t\t.replace(Path.of(workDir).toAbsolutePath() + File.separator, \"\")\n\t\t\t\t.trim();\n\t\t\treturn new CodeExecutionResult(e.getExitValue(), errorOutput);\n\t\t}\n\t\tcatch (IOException e) {\n\t\t\tthrow new Exception(\"Failed to execute code\", e);\n\t\t}\n\t\tfinally {\n\t\t\t// Cleanup Java class files\n\t\t\tif (\"java\".equals(language)) {\n\t\t\t\tFileUtils.deleteFile(workDir, filename.replace(\".java\", \".class\"));\n\t\t\t}\n\t\t}\n\t}\n\n}\n","sourceCodeStart":146,"sourceCodeEnd":175,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-boot-starters/spring-ai-alibaba-starter-builtin-nodes/src/main/java/com/alibaba/cloud/ai/graph/node/code/LocalCommandlineCodeExecutor.java#L146-L175","documentation":"executeCodeLocally wraps IOException thrown while spawning the language interpreter process into a plain Exception with message 'Failed to execute code'. Non-zero exit codes are handled separately (returned as CodeExecutionResult), so this error specifically means the process could not be started or I/O with it failed.","triggerScenarios":"Running a code block locally when the interpreter executable (python/node/java/sh) is not on PATH, the work directory does not exist or is unreadable, or reading the process streams fails mid-execution.","commonSituations":"Python 3 installed as 'python3' but configured environment uses a minimal container without it; Node not installed; workDir points to a deleted/nonexistent directory; wrong PATH in the service runtime vs. the developer shell.","solutions":["Check the cause: 'Cannot run program ...' means the interpreter is missing — install it or add it to PATH.","Verify the configured workDir (CodeExecutionConfig.getWorkDir()) exists and is writable.","Test manually that '<interpreter> --version' works in the same environment/JVM process.","Use DockerCodeExecutor for hermetic environments instead of relying on host tooling."],"exampleFix":"// before\nnew LocalCommandlineCodeExecutor().executeCodeBlocks(blocks, config); // 'Failed to execute code'\n// after\nFile workDirFile = new File(config.getWorkDir());\nif (!workDirFile.exists()) workDirFile.mkdirs(); // and ensure python/node on PATH before executing","handlingStrategy":"validation","validationCode":"String exe = CodeUtils.getExecutableForLanguage(language); // throws if unsupported\nFile wd = new File(config.getWorkDir());\nif (!wd.isDirectory()) throw new IllegalStateException(\"workDir missing: \" + wd);\nif (new ProcessBuilder(exe, \"--version\").start().waitFor() != 0) throw new IllegalStateException(exe + \" not runnable\");","typeGuard":null,"tryCatchPattern":"try {\n    return executor.executeCodeBlocks(blocks, config);\n} catch (Exception e) {\n    if (e.getCause() instanceof IOException ioe && ioe.getMessage().contains(\"Cannot run program\")) {\n        throw new IllegalStateException(\"Interpreter not installed or not on PATH\", e);\n    }\n    throw e;\n}","preventionTips":["Install and PATH-verify python3/node/java in the deployment image","Create the workDir at startup rather than assuming it exists","Prefer DockerCodeExecutor for reproducible environments","Smoke-test each supported language at boot"],"tags":["process-execution","ioexception","command-not-found","code-execution"],"backgroundTag":"command-not-found","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}