{"record":{"id":"c556d617bd5aa498","repo":"kestra-io/kestra","slug":"file-already-exist","errorCode":null,"errorMessage":"File {} already exist","messagePattern":"File (.+?) already exist","errorType":"exception","errorClass":"FileAlreadyExistsException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/io/kestra/core/runners/LocalWorkingDir.java","lineNumber":208,"sourceCode":"     **/\n    @Override\n    public Path putFile(Path path, InputStream inputStream, FileExistComportment comportment) throws IOException {\n        if (path == null) {\n            throw new IllegalArgumentException(\"Cannot create a working directory file with a null path\");\n        }\n        if (inputStream == null) {\n            throw new IllegalArgumentException(\"Cannot create a working directory file with an empty inputStream\");\n        }\n        Path newFilePath = this.resolve(path);\n        Files.createDirectories(newFilePath.getParent());\n\n        if (Files.exists(newFilePath)) {\n            switch (comportment) {\n                case OVERWRITE -> {\n                    log.info(\"File {} already exist. It will be overwritten\", newFilePath);\n                    copyFile(inputStream, newFilePath);\n                }\n                case FAIL -> throw new FileAlreadyExistsException(\"File \" + newFilePath + \" already exist\");\n                case WARN -> log.warn(\"File {} already exist. It will be ignore\", newFilePath);\n                case IGNORE -> {\n                }\n            }\n        } else {\n            Files.createFile(newFilePath);\n            copyFile(inputStream, newFilePath);\n        }\n\n        return newFilePath;\n    }\n\n    private static void copyFile(InputStream inputStream, Path path) throws IOException {\n        try (inputStream) {\n            Files.copy(inputStream, path, REPLACE_EXISTING);\n        }\n    }\n","sourceCodeStart":190,"sourceCodeEnd":226,"githubUrl":"https://github.com/kestra-io/kestra/blob/823fada9274c4f9c251ea0a516460a4f7d958032/core/src/main/java/io/kestra/core/runners/LocalWorkingDir.java#L190-L226","documentation":"Thrown by `LocalWorkingDir.putFile` when the target file already exists AND the caller selected `FileExistComportment.FAIL`. The other comportments are OVERWRITE (logs and replaces), WARN (logs and skips), and IGNORE (silent skip). Thrown as `java.nio.file.FileAlreadyExistsException` with the resolved path in the message.","triggerScenarios":"Calling `workingDir.putFile(path, in, FileExistComportment.FAIL)` when `path` already resolves to an existing file — e.g. a task writing the same filename twice in one execution, or a filename collision from a loop iteration.","commonSituations":"A ForEach loop whose iterations write to a fixed filename; re-running logic that re-creates the same file; a download task that overwrites a previously-created file when FAIL was chosen to detect collisions.","solutions":["Use `FileExistComportment.OVERWRITE` if replacement is intended.","Generate a unique filename per write (append an index or `IdUtils.create()`).","Keep `FAIL` and delete the existing file first, or check existence and branch.","Use `createTempFile()` for guaranteed-unique scratch files."],"exampleFix":"// before\nworkingDir.putFile(Path.of(\"out.csv\"), in, FileExistComportment.FAIL);\n\n// after — overwrite is fine\nworkingDir.putFile(Path.of(\"out.csv\"), in, FileExistComportment.OVERWRITE);\n// or unique name\nworkingDir.putFile(Path.of(\"out-\" + i + \".csv\"), in, FileExistComportment.FAIL);","handlingStrategy":"validation","validationCode":"Path target = path;\nif (Files.exists(workingDir.resolve(target)) && comportment == FileExistComportment.FAIL) {\n    // either delete first or choose a unique name\n    target = Path.of(target + \".\" + IdUtils.create());\n}\nreturn workingDir.putFile(target, in, comportment);","typeGuard":null,"tryCatchPattern":"try {\n    workingDir.putFile(path, in, FileExistComportment.FAIL);\n} catch (FileAlreadyExistsException e) {\n    // collision detected — choose a unique name and retry once\n    workingDir.putFile(Path.of(path + \".\" + IdUtils.create()), in, FileExistComportment.FAIL);\n}","preventionTips":["Choose the comportment that matches intent: OVERWRITE to replace, FAIL to detect collisions, IGNORE to skip.","Generate unique filenames in loops to avoid collisions.","Use `createTempFile()` for scratch files where uniqueness is required."],"tags":["working-dir","io","file-exists","comportment"],"backgroundTag":null,"analyzedSha":"823fada9274c4f9c251ea0a516460a4f7d958032","analyzedAt":"2026-08-14T06:15:17.947Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}