{"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/956246301e03d757539f7b76dd5a91f298637563/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/extension/TempDirectory.java#L260-L296","documentation":"Thrown by CloseablePath's constructor when the TempDirFactory returns a Path whose FileSystem is not the default filesystem, but the injection target type is java.io.File. java.io.File can only represent paths on the default filesystem, so converting a non-default Path to File would be lossy/incorrect; the constructor rejects it as a PreconditionViolationException after closing the factory.","triggerScenarios":"Using a custom @TempDir(factory = ...) that returns a Path from a custom FileSystem (e.g. Jimfs in-memory filesystem, zip filesystem), while declaring the @TempDir field/parameter as java.io.File rather than java.nio.file.Path.","commonSituations":"In-memory filesystem testing libraries (Jimfs) wired as a TempDirFactory, with a test field accidentally typed as File; refactoring a Path-based test to File while keeping a custom factory that produces non-default-fs paths.","solutions":["Change the @TempDir field/parameter type from java.io.File to java.nio.file.Path so a non-default filesystem is supported.","Or make the custom TempDirFactory create the directory on the default filesystem (FileSystems.getDefault()) if a File target is required."],"exampleFix":"// before\n@TempDir(factory = JimfsFactory.class)\nFile tmp; // Jimfs path is non-default fs -> PreconditionViolationException\n\n// after\n@TempDir(factory = JimfsFactory.class)\njava.nio.file.Path tmp; // Path supports any FileSystem","handlingStrategy":"type-guard","validationCode":"// If using a non-default-fs factory, force the target type to Path\nif (usesCustomFileSystem(factory) && targetType == File.class) {\n    throw new IllegalStateException(\"Use Path, not File, with a non-default filesystem factory\");\n}","typeGuard":"static boolean targetMatchesFileSystem(Class<?> targetType, Path sample) {\n    return targetType == java.nio.file.Path.class\n        || java.nio.file.FileSystems.getDefault().equals(sample.getFileSystem());\n}","tryCatchPattern":null,"preventionTips":["When using Jimfs or zip-filesystem factories, declare @TempDir as Path, never File.","If a File target is mandatory, ensure the factory creates dirs on the default filesystem.","Document the factory's filesystem expectation on its Javadoc."],"tags":["junit-jupiter","tempdir","extension","filesystem","type-mismatch"],"analyzedSha":"956246301e03d757539f7b76dd5a91f298637563","analyzedAt":"2026-08-04T19:25:17.049Z","schemaVersion":2}