{"record":{"id":"6bc43e7c4eceb299","repo":"HMCL-dev/HMCL","slug":"cannot-fix-optifine","errorCode":null,"errorMessage":"Cannot fix optifine","messagePattern":"Cannot fix optifine","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/GameLibrariesTask.java","lineNumber":174,"sourceCode":"                                    .injectURLWithCandidates(fmlLib.downloadUrl());\n                            dependencies.add(new FileDownloadTask(uris, file)\n                                    .withCounter(\"hmcl.install.libraries\"));\n                        }\n                    }\n                }\n            }\n\n            Path file = gameRepository.getLayout().getLibraryFile(manifest.id(), library);\n            if (\"optifine\".equals(library.groupId()) && Files.exists(file)) {\n                if (Files.exists(file) && libraries.stream().filter(it -> it.is(\"optifine\", \"OptiFine\"))\n                        .anyMatch(it -> it.version().startsWith(\"1.20.4_\"))) {\n                    @Nullable String forgeVersion = GameComponentAnalyzer.analyze(manifest, GameVersionNumber.asGameVersion(\"1.20.4\"))\n                            .getVersion(GameComponentType.FORGE);\n                    if (forgeVersion != null && GameComponentAnalyzer.FORGE_OPTIFINE_BROKEN_RANGE.contains(VersionNumber.asVersion(forgeVersion))) {\n                        try (FileSystem fs2 = CompressingUtils.createWritableZipFileSystem(file)) {\n                            Files.deleteIfExists(fs2.getPath(\"/META-INF/mods.toml\"));\n                        } catch (IOException e) {\n                            throw new IOException(\"Cannot fix optifine\", e);\n                        }\n                    }\n                }\n            } else if (library.is(\"org.jackhuang.hmcl\", \"mmc-bootstrap\")) {\n                if (!Files.exists(file)) {\n                    try (InputStream input = Objects.requireNonNull(\n                            GameLibrariesTask.class.getResourceAsStream(\n                                    \"/assets/game/HMCLMultiMCBootstrap-1.0.jar\"),\n                            \"Bundled HMCLMultiMCBootstrap is missing.\")) {\n                        Files.createDirectories(file.getParent());\n                        Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING);\n                    }\n                }\n            } else if (library.is(\"org.jackhuang.hmcl\", \"transformer-discovery-service\")) {\n                try (InputStream input = Objects.requireNonNull(\n                        GameLibrariesTask.class.getResourceAsStream(\n                                \"/assets/game/HMCLTransformerDiscoveryService-1.0.jar\"),\n                        \"Bundled HMCLTransformerDiscoveryService is missing.\")) {","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/HMCL-dev/HMCL/blob/24702dc5a0214034f4c27166d5fd30cad08cec19/HMCLCore/src/main/java/org/jackhuang/hmcl/download/game/GameLibrariesTask.java#L156-L192","documentation":"GameLibrariesTask.execute() wraps an IOException thrown while repairing a known broken Forge+OptiFine combination: it opens the library JAR as a writable zip filesystem and deletes META-INF/mods.toml, and if that zip manipulation fails it rethrows as 'Cannot fix optifine'. The failure means the OptiFine library file could not be patched in place.","triggerScenarios":"The manifest declares Forge within FORGE_OPTIFINE_BROKEN_RANGE (around 1.20.4) together with OptiFine, and CompressingUtils.createWritableZipFileSystem(file) or Files.deleteIfExists throws — e.g. the file is not a zip, is read-only, or is locked by another process.","commonSituations":"OptiFine JAR corrupted during download, JAR opened/locked by another program (or antivirus on Windows), read-only file permissions, or the path points to a non-zip file.","solutions":["Close any program holding the OptiFine JAR open (IDE, file manager, antivirus scan) and retry.","Delete the OptiFine library file from the libraries folder and let HMCL re-download it.","Check file write permissions on the libraries directory.","Remove the OptiFine dependency or update Forge outside the broken range."],"exampleFix":"// before: relying on HMCL to patch the broken pair\noptifineLib = detectOptiFine();\ninstallLibraries(...); // may throw 'Cannot fix optifine'\n// after: pre-check and re-download a corrupt JAR\nif (isOptiFinePair(forgeVersion) && !isUsableZip(optifineFile)) {\n    Files.delete(optifineFile); // force fresh download before task runs\n}\ninstallLibraries(...);","handlingStrategy":"try-catch","validationCode":"if (forgeVersion != null && FORGE_OPTIFINE_BROKEN_RANGE.contains(VersionNumber.asVersion(forgeVersion)) && !isWritable(file)) closeLockingProcesses(file);","typeGuard":"boolean isWritableJar(Path p) throws IOException { return Files.isRegularFile(p) && Files.isWritable(p); }","tryCatchPattern":"try { await(librariesTask); } catch (IOException e) { if (e.getMessage().equals(\"Cannot fix optifine\")) { Files.delete(optifineLib); await(librariesTask); } else throw e; }","preventionTips":["Avoid combining Forge builds in the known broken range with OptiFine","Ensure the libraries directory is writable and not on a read-only mount","Don't open library JARs in other tools while HMCL runs","Re-download OptiFine if the JAR is corrupt"],"tags":["optifine","forge","zip","file","download"],"backgroundTag":"file-write-failed","analyzedSha":"24702dc5a0214034f4c27166d5fd30cad08cec19","analyzedAt":"2026-09-10T12:36:46.680Z","contentChangedAt":"2026-09-10T12:36:46.680Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}