{"record":{"id":"9ff33031d3ce68e7","repo":"karatelabs/karate","slug":"failed-to-materialize-resource-to-filename","errorCode":null,"errorMessage":"Failed to materialize resource to: {filename}","messagePattern":"Failed to materialize resource to: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/common/MemoryResource.java","lineNumber":227,"sourceCode":"\n    /**\n     * Materializes this in-memory resource to disk at the specified filename.\n     * The file is created within the root directory.\n     *\n     * @param filename the filename to save as\n     * @return PathResource pointing to the saved file\n     */\n    public PathResource materialize(String filename) {\n        try {\n            Path target = root.resolve(filename);\n            // Ensure parent directories exist\n            if (target.getParent() != null) {\n                Files.createDirectories(target.getParent());\n            }\n            Files.write(target, bytes());\n            return new PathResource(target, root);\n        } catch (Exception e) {\n            throw new RuntimeException(\"Failed to materialize resource to: \" + filename, e);\n        }\n    }\n\n    @Override\n    public InputStream getStream() {\n        return new ByteArrayInputStream(bytes());\n    }\n\n    @Override\n    public String toString() {\n        return getPrefixedPath();\n    }\n\n}\n","sourceCodeStart":209,"sourceCodeEnd":242,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/common/MemoryResource.java#L209-L242","documentation":"MemoryResource.materialize() writes the in-memory resource's bytes to a real file on disk (used when an API requires a path-backed resource, e.g. for HTML report output). Any failure creating parent directories or writing the bytes is wrapped in this RuntimeException, keeping the original exception as the cause.","triggerScenarios":"Writing to a read-only directory or a path without write permission; parent directory creation fails (permission denied, path exists as a file); disk full; the filename resolves to an invalid/illegal path on the OS; SecurityManager or sandbox blocks the write.","commonSituations":"Running tests in a read-only CI workspace; output dir owned by a different user; target path colliding with an existing regular file; running in a container with a read-only filesystem or full tmpfs.","solutions":["Check the exception cause for the underlying filesystem error (permission denied vs disk full vs invalid path)","Ensure the target directory exists and is writable by the process user","Free disk space or point output to a writable directory (e.g. mvn surefire reportDirectory config)","Verify the filename/path is valid for the platform (no illegal characters, not inside a file)"],"exampleFix":"// before (read-only dir)\nresource.materialize(\"/usr/share/report.html\")\n// after (writable build dir)\nresource.materialize(\"target/report.html\")","handlingStrategy":"try-catch","validationCode":"// Java: pre-validate the target location is writable\nPath target = Paths.get(\"target/report.html\");\nPath parent = target.toAbsolutePath().getParent();\nif (parent == null || !Files.isDirectory(parent) || !Files.isWritable(parent)) {\n    throw new IllegalStateException(\"cannot materialize into: \" + parent);\n}","typeGuard":null,"tryCatchPattern":"try {\n    Resource r = memResource.materialize(\"target/report.html\");\n} catch (RuntimeException e) {\n    if (e.getMessage().startsWith(\"Failed to materialize resource to:\")) {\n        logger.error(\"materialize failed: {}\", e.getCause());\n    }\n}","preventionTips":["Materialize only into writable build/output directories","Check disk space and filesystem mounts in CI images","Ensure the parent path is not occupied by a regular file","Log the cause chain — the root filesystem error is in getCause()"],"tags":["io","file-write","resource"],"backgroundTag":"file-write-failed","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"}