{"record":{"id":"de1c199bcf69e4fa","repo":"halo-dev/halo","slug":"target-directory-path-was-not-empty","errorCode":null,"errorMessage":"Target directory: {path} was not empty","messagePattern":"Target directory: (.+?) was not empty","errorType":"exception","errorClass":"DirectoryNotEmptyException","httpStatus":null,"severity":"warning","filePath":"application/src/main/java/run/halo/app/infra/utils/FileUtils.java","lineNumber":172,"sourceCode":"        Assert.notNull(path, \"Path must not be null\");\n\n        if (Files.notExists(path)) {\n            // Create directories\n            Files.createDirectories(path);\n\n            log.debug(\"Created directory: [{}]\", path);\n        }\n    }\n\n    /**\n     * The given path must be empty.\n     *\n     * @param path path must not be null\n     * @throws IOException io exception\n     */\n    public static void ensureEmpty(Path path) throws IOException {\n        if (!isEmpty(path)) {\n            throw new DirectoryNotEmptyException(\"Target directory: \" + path + \" was not empty\");\n        }\n    }\n\n    /**\n     * Checks if the given path is empty.\n     *\n     * @param path path must not be null\n     * @return true if the given path is empty; false otherwise\n     * @throws IOException io exception\n     */\n    public static boolean isEmpty(Path path) throws IOException {\n        Assert.notNull(path, \"Path must not be null\");\n\n        if (!Files.isDirectory(path) || Files.notExists(path)) {\n            return true;\n        }\n\n        try (Stream<Path> pathStream = Files.list(path)) {","sourceCodeStart":154,"sourceCodeEnd":190,"githubUrl":"https://github.com/halo-dev/halo/blob/d2f5165f9c8f055ffcb3fa9c3f4032821a7b68c8/application/src/main/java/run/halo/app/infra/utils/FileUtils.java#L154-L190","documentation":"Thrown as java.nio.file.DirectoryNotEmptyException by FileUtils.ensureEmpty when the given Path is a non-empty directory. Callers use ensureEmpty to assert a target is clear before writing into it (e.g. extraction/cleanup targets).","triggerScenarios":"Calling FileUtils.ensureEmpty(path) on a directory that contains any entries (files or subdirectories), including hidden files.","commonSituations":"Re-running setup into a previously populated data dir; leftover temp files from a crashed run; hidden files (.DS_Store, lock files) blocking an 'empty' check; pointing at the wrong directory.","solutions":["Clear the directory contents (or delete and recreate it) before calling ensureEmpty.","Point the path at the intended empty target.","If partial contents are expected, switch to a cleanup step instead of asserting emptiness.","Check for hidden/lock files and remove them."],"exampleFix":"// before\nFileUtils.ensureEmpty(dataDir); // throws\n\n// after\nFileUtils.deleteRecursively(dataDir);\nFiles.createDirectories(dataDir);\nFileUtils.ensureEmpty(dataDir);","handlingStrategy":"validation","validationCode":"if (!FileUtils.isEmpty(path)) {\n    // decide: clean it or pick another target\n    Files.list(path).forEach(p -> { try { Files.deleteIfExists(p); } catch (IOException ignored) {} });\n}","typeGuard":"static boolean pathIsEmpty(Path p) throws IOException {\n    return Files.notExists(p) || (Files.isDirectory(p) && Files.list(p).findAny().isEmpty());\n}","tryCatchPattern":"try {\n    FileUtils.ensureEmpty(target);\n} catch (DirectoryNotEmptyException e) {\n    // either clean and retry or report to the user\n    FileUtils.deleteRecursively(target);\n    Files.createDirectories(target);\n}","preventionTips":["Always clean known target dirs in setup scripts before running.","Watch for hidden/lock files that defeat 'empty' checks.","Point extraction/cleanup at fresh, dedicated temp directories."],"tags":["filesystem","io","directory","setup"],"backgroundTag":null,"analyzedSha":"d2f5165f9c8f055ffcb3fa9c3f4032821a7b68c8","analyzedAt":"2026-08-14T00:18:38.915Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}