{"record":{"id":"38d2cd18e6f3de7f","repo":"Grasscutters/Grasscutter","slug":"failed-to-write-to-file","errorCode":null,"errorMessage":"Failed to write to file.","messagePattern":"Failed to write to file\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"src/main/java/emu/grasscutter/tools/Dumpers.java","lineNumber":106,"sourceCode":"                                    new CommandInfo(\n                                            labels,\n                                            description,\n                                            List.of(command.usage()),\n                                            List.of(command.permission(), command.permissionTargeted()),\n                                            command.targetRequirement()));\n                        });\n\n        try {\n            // Create a file for the dump.\n            var file = new File(\"commands.json\");\n            if (file.exists() && !file.delete()) throw new RuntimeException(\"Failed to delete file.\");\n            if (!file.exists() && !file.createNewFile())\n                throw new RuntimeException(\"Failed to create file.\");\n\n            // Write the dump to the file.\n            Files.writeString(file.toPath(), JsonUtils.encode(dump));\n        } catch (IOException ignored) {\n            throw new RuntimeException(\"Failed to write to file.\");\n        }\n    }\n\n    /**\n     * Dumps all avatars to a CSV file.\n     *\n     * @param locale The language to dump the avatars in.\n     */\n    static void dumpAvatars(String locale) {\n        // Reload resources.\n        ResourceLoader.loadAll();\n        Language.loadTextMaps();\n\n        // Convert all known avatars to an avatar map.\n        var dump = new HashMap<Integer, AvatarInfo>();\n        GameData.getAvatarDataMap()\n                .forEach(\n                        (id, avatar) -> {","sourceCodeStart":88,"sourceCodeEnd":124,"githubUrl":"https://github.com/Grasscutters/Grasscutter/blob/f373827a83d3e688fd3c6afb1e1c3759ca1d61c9/src/main/java/emu/grasscutter/tools/Dumpers.java#L88-L124","documentation":"Wraps any IOException thrown while writing the command dump to commands.json via Files.writeString, replacing the original exception with a generic RuntimeException. It indicates the file exists but its content could not be written.","triggerScenarios":"Files.writeString(file.toPath(), JsonUtils.encode(dump)) throwing IOException — e.g. disk full, file locked by another process, permission revoked, or the file deleted between creation and write.","commonSituations":"Disk quota exceeded mid-write; commands.json opened with an exclusive lock (Windows); running on a full tmpfs; JsonUtils.encode output larger than available space.","solutions":["Free disk space / raise quota and re-run the dump.","Close programs locking commands.json, then re-run.","Ensure the user has write permission on commands.json after creation.","If it persists, patch the catch block to log the underlying IOException cause for diagnosis."],"exampleFix":"// before\ncatch (IOException ignored) { throw new RuntimeException(\"Failed to write to file.\"); }\n// after\ncatch (IOException e) { throw new RuntimeException(\"Failed to write to file.\", e); }","handlingStrategy":"try-catch","validationCode":"File f = new File(\"commands.json\");\nif (f.exists() && (!f.canWrite() || f.getFreeSpace() == 0))\n    throw new IllegalStateException(\"Cannot write commands.json: permissions or disk space\");","typeGuard":"static boolean isWritableOutput(String name) {\n    File f = new File(name);\n    try { return !f.exists() || f.canWrite(); } catch (SecurityException e) { return false; }\n}","tryCatchPattern":"try {\n    dumpCommands();\n} catch (RuntimeException e) {\n    if (\"Failed to write to file.\".equals(e.getMessage())) {\n        LOGGER.warn(\"Write failed — check disk space/locks on commands.json\", e);\n    } else throw e;\n}","preventionTips":["Monitor free disk space before running dumps.","Keep output files closed during writes (no Excel/editor locks).","Write to local storage rather than flaky network drives.","Patch the catch block to chain the IOException cause for real diagnostics."],"tags":["filesystem","io","disk-space"],"backgroundTag":"file-write-failed","analyzedSha":"f373827a83d3e688fd3c6afb1e1c3759ca1d61c9","analyzedAt":"2026-09-03T22:43:49.407Z","contentChangedAt":"2026-09-03T22:43:49.407Z","schemaVersion":2},"datasetVersion":"2026-09-11T07:07:21.782Z"}