{"record":{"id":"f0566d82f510cf42","repo":"junit-team/junit5","slug":"temp-directory-with-non-default-file-system-cannot","errorCode":null,"errorMessage":"temp directory with non-default file system cannot be injected into java.io.File target","messagePattern":"temp directory with non-default file system cannot be injected into java\\.io\\.File target","errorType":"exception","errorClass":"PreconditionViolationException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java","lineNumber":278,"sourceCode":"\t\tprivate final AnnotatedElementContext elementContext;\n\t\tprivate final ExtensionContext extensionContext;\n\n\t\tprivate CloseablePath(TempDirFactory factory, Cleanup cleanup, Class<?> elementType,\n\t\t\t\tAnnotatedElementContext elementContext, ExtensionContext extensionContext) throws Exception {\n\t\t\tthis.dir = factory.createTempDirectory(elementContext, extensionContext);\n\t\t\tthis.factory = factory;\n\t\t\tthis.cleanup = cleanup;\n\t\t\tthis.elementContext = elementContext;\n\t\t\tthis.extensionContext = extensionContext;\n\n\t\t\tif (this.dir == null || !Files.isDirectory(this.dir)) {\n\t\t\t\tclose();\n\t\t\t\tthrow new PreconditionViolationException(\"temp directory must be a directory\");\n\t\t\t}\n\n\t\t\tif (elementType == File.class && !this.dir.getFileSystem().equals(FileSystems.getDefault())) {\n\t\t\t\tclose();\n\t\t\t\tthrow new PreconditionViolationException(\n\t\t\t\t\t\"temp directory with non-default file system cannot be injected into \" + File.class.getName()\n\t\t\t\t\t\t\t+ \" target\");\n\t\t\t}\n\t\t}\n\n\t\tPath get() {\n\t\t\treturn requireNonNull(this.dir);\n\t\t}\n\n\t\t@Override\n\t\tpublic void close() throws IOException {\n\t\t\ttry {\n\t\t\t\tif (this.dir != null) {\n\t\t\t\t\tthis.cleanup.run(this.dir, this.elementContext, this.extensionContext);\n\t\t\t\t}\n\t\t\t}\n\t\t\tfinally {\n\t\t\t\tthis.factory.close();","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/junit-team/junit5/blob/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java#L260-L296","documentation":"Thrown as PreconditionViolationException inside the CloseablePath constructor when the injected element type is java.io.File but the created temp directory lives on a non-default FileSystem. A Path from a custom FileSystem cannot be losslessly converted to a java.io.File (which is tied to the default file system), so injection is refused and the path is closed first.","triggerScenarios":"CloseablePath constructor: `elementType == File.class && !this.dir.getFileSystem().equals(FileSystems.getDefault())`. Happens when a custom TempDirFactory creates the directory on an in-memory or zip FileSystem (e.g. Jimfs) but the @TempDir target is typed File.","commonSituations":"Using Google Jimfs or a custom FileSystem for test isolation and asking JUnit to inject java.io.File; copy-paste of a Path-based test into a File-based field while keeping the custom in-memory factory; memory file system tests.","solutions":["Change the @TempDir target type from java.io.File to java.nio.file.Path so the non-default FileSystem is supported.","Or configure the custom factory to create the directory on the default FileSystem (FileSystems.getDefault()) when a File target is required.","Adapt your test code to operate on Path rather than File when using in-memory file systems."],"exampleFix":"// before\n@TempDir(factory = JimfsFactory.class)\nFile dir; // File + non-default FS -> rejected\n\n// after - switch to Path\n@TempDir(factory = JimfsFactory.class)\nPath dir;","handlingStrategy":"type-guard","validationCode":"Path dir = factory.createTempDirectory(ctx, ec);\nboolean defaultFs = dir.getFileSystem().equals(FileSystems.getDefault());\nboolean wantsFile = elementType == File.class;\nif (wantsFile && !defaultFs) {\n    throw new IllegalStateException(\"Non-default FS cannot be injected into File; use Path\");\n}","typeGuard":"Class<?> elementType = /* resolved element type */;\nboolean isPathTarget = elementType == Path.class;\nboolean isFileTarget = elementType == File.class;\nboolean defaultFs = Files.getDefaultFileSystem().equals(...);\nif (isFileTarget && !defaultFs) {\n    // must switch to Path\n}","tryCatchPattern":null,"preventionTips":["When using an in-memory or custom FileSystem (e.g. Jimfs), type @TempDir as Path not File.","If you must inject File, configure the factory to use the default FileSystem.","Document the FileSystem assumption in any custom TempDirFactory."],"tags":["tempdir","filesystem","type-mismatch","file","extension"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}