{"record":{"id":"1a71691411e554c1","repo":"karatelabs/karate","slug":"failed-to-open-stream-for-path","errorCode":null,"errorMessage":"Failed to open stream for: {path}","messagePattern":"Failed to open stream for: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/common/PathResource.java","lineNumber":209,"sourceCode":"            boolean hasExtension = pathStr.matches(\".*\\\\.[a-zA-Z0-9]+$\");\n            base = hasExtension ? path.getParent() : path;\n        }\n        Path resolved = base.resolve(childPath);\n        return new PathResource(resolved, root, classpath, classpathRoot);\n    }\n\n    @Override\n    public Resource getParent() {\n        Path parentPath = path.getParent();\n        return parentPath != null ? new PathResource(parentPath, root, classpath, classpathRoot) : null;\n    }\n\n    @Override\n    public InputStream getStream() {\n        try {\n            return Files.newInputStream(path);\n        } catch (Exception e) {\n            throw new RuntimeException(\"Failed to open stream for: \" + path, e);\n        }\n    }\n\n    @Override\n    public String getRelativePath() {\n        return relativePath;\n    }\n\n    @Override\n    public String getText() {\n        if (text == null) {\n            try {\n                bytes = Files.readAllBytes(path);\n                text = FileUtils.toString(bytes);\n            } catch (Exception e) {\n                throw new RuntimeException(\"Failed to read text from: \" + path, e);\n            }\n        }","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/common/PathResource.java#L191-L227","documentation":"PathResource.getStream() opens the underlying file with Files.newInputStream() and wraps any failure (file deleted between existence check and open, permission denied, is a directory, I/O error) in this RuntimeException with the resource path in the message. Callers receive a stream-backed view of the resource, so a failing open breaks any downstream read.","triggerScenarios":"The file was removed or renamed after the PathResource was created; process lacks read permission on the file or a parent directory; the path points to a directory; the file is on an unmounted/network volume that became unavailable; file is locked by another process with incompatible sharing (Windows).","commonSituations":"Temp files cleaned up mid-run by another process; reading files created by a previous test step that ran in a different working directory; permission differences between local dev and CI user; antivirus/file-lock contention on Windows.","solutions":["Print the resolved path in the message and confirm it exists and is a regular file (Files.isRegularFile)","Check read permissions on the file and its parent directories for the process user","Re-create the resource from a fresh, verified path rather than a cached one","If the file is generated by an earlier step, add synchronization/wait so it exists before opening"],"exampleFix":"// before\nResource r = Resource.path(\"missing-report.html\");\nInputStream is = r.getStream(); // throws if deleted\n// after\nResource r = Resource.path(\"report.html\");\nif (Files.isRegularFile(Paths.get(\"report.html\"))) {\n    InputStream is = r.getStream();\n}","handlingStrategy":"validation","validationCode":"// Java: verify the file is openable before getting the stream\nPath p = Paths.get(\"report.html\");\nif (!Files.isRegularFile(p) || !Files.isReadable(p)) {\n    throw new IllegalStateException(\"not a readable file: \" + p);\n}","typeGuard":null,"tryCatchPattern":"try {\n    InputStream is = pathResource.getStream();\n} catch (RuntimeException e) {\n    if (e.getMessage().startsWith(\"Failed to open stream for:\")) {\n        logger.error(\"open failed for {} cause: {}\", e.getMessage(), e.getCause());\n    }\n}","preventionTips":["Create PathResource from freshly verified paths; avoid caching across steps that clean temp dirs","Check read permissions and that the path is not a directory","Use classpath resources to avoid working-directory drift between environments"],"tags":["io","file-open","resource"],"backgroundTag":"file-open-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"}