{"record":{"id":"9f2f6fe6ffc9bdc2","repo":"elastic/elasticsearch","slug":"failed-to-read-exec-output","errorCode":null,"errorMessage":"Failed to read exec output","messagePattern":"Failed to read exec output","errorType":"exception","errorClass":"GradleException","httpStatus":null,"severity":"error","filePath":"build-tools/src/main/java/org/elasticsearch/gradle/LoggedExec.java","lineNumber":184,"sourceCode":"            }\n            if (getStandardInput().isPresent()) {\n                execSpec.setStandardInput(new ByteArrayInputStream(getStandardInput().get().getBytes(StandardCharsets.UTF_8)));\n            }\n        });\n        int exitValue = execResult.getExitValue();\n\n        if (exitValue == 0 && getCaptureOutput().get()) {\n            output = byteStreamToString(out);\n        }\n        if (getLogger().isInfoEnabled() == false) {\n            if (exitValue != 0) {\n                try {\n                    if (getIndentingConsoleOutput().isPresent() == false) {\n                        getLogger().error(\"Output for \" + getExecutable().get() + \":\");\n                    }\n                    outputLogger.accept(getLogger());\n                } catch (Exception e) {\n                    throw new GradleException(\"Failed to read exec output\", e);\n                }\n                throw new GradleException(\n                    String.format(\"Process '%s %s' finished with non-zero exit value %d\", getExecutable().get(), getArgs().get(), exitValue)\n                );\n            }\n        }\n\n    }\n\n    private String byteStreamToString(OutputStream out) {\n        return ((ByteArrayOutputStream) out).toString(StandardCharsets.UTF_8);\n    }\n\n    public static ExecResult exec(ExecOperations execOperations, Action<ExecSpec> action) {\n        return genericExec(execOperations::exec, action);\n    }\n\n    public static ExecResult javaexec(ExecOperations project, Action<JavaExecSpec> action) {","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/build-tools/src/main/java/org/elasticsearch/gradle/LoggedExec.java#L166-L202","documentation":"LoggedExec.run() wraps any exception thrown while logging the output of a failed process (non-zero exit, logger not at info). After detecting exitValue != 0, it calls outputLogger.accept(getLogger()) which invokes byteStreamToString(out) — a cast of OutputStream to ByteArrayOutputStream. If that cast or string conversion fails, the original failure is masked and rethrown as this GradleException.","triggerScenarios":"An executed process returns non-zero, logging level is above info (so the output-logging branch runs), and the output-logging lambda throws — typically because `out` is not a ByteArrayOutputStream (e.g., a custom OutputStream was injected) or a charset/IO issue occurs during toString.","commonSituations":"Subclassing LoggedExec and overriding the OutputStream handling; corruption of the captured stream; edge cases where the ByteArrayOutputStream cast in byteStreamToString fails. Rare in practice — usually indicates a subclassing bug.","solutions":["Inspect the wrapped cause (GradleException.getCause()) for the real failure — typically a ClassCastException from byteStreamToString.","If subclassing LoggedExec, ensure the `out` OutputStream passed to the logging path remains a ByteArrayOutputStream.","Run with --info to take the early-return branch that bypasses this output-logging code entirely."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { loggedExec.getActions(); } catch (GradleException e) { if (e.getMessage().contains(\"Failed to read exec output\")) { log.error(\"Underlying cause:\", e.getCause()); } throw e; }","preventionTips":["Run with --info to bypass the output-logging branch that wraps this error.","If subclassing LoggedExec, preserve ByteArrayOutputStream as the captured stream type.","Inspect the wrapped cause (getCause()) — the real failure is masked."],"tags":["gradle","build-tools","exec","error-wrapping"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}