{"record":{"id":"9299a2a791a7abfb","repo":"arduino/Arduino","slug":"could-not-replace-0","errorCode":null,"errorMessage":"Could not replace {0}","messagePattern":"Could not replace (.+?)","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"arduino-core/src/processing/app/BaseNoGui.java","lineNumber":912,"sourceCode":"    PApplet.saveStrings(temp, strArray);\n\n    try {\n      file = file.toPath().toRealPath().toFile().getCanonicalFile();\n    } catch (IOException e) {\n    }\n\n    if (file.exists()) {\n      boolean result = file.delete();\n      if (!result) {\n        throw new IOException(\n      I18n.format(\n        tr(\"Could not remove old version of {0}\"),\n        file.getAbsolutePath()));\n      }\n    }\n    boolean result = temp.renameTo(file);\n    if (!result) {\n      throw new IOException(\n    I18n.format(\n      tr(\"Could not replace {0}\"),\n      file.getAbsolutePath()));\n    }\n  }\n\n  static public void selectBoard(TargetBoard targetBoard) {\n    TargetPlatform targetPlatform = targetBoard.getContainerPlatform();\n    TargetPackage targetPackage = targetPlatform.getContainerPackage();\n\n    PreferencesData.set(\"target_package\", targetPackage.getId());\n    PreferencesData.set(\"target_platform\", targetPlatform.getId());\n    PreferencesData.set(\"board\", targetBoard.getId());\n\n    File platformFolder = targetPlatform.getFolder();\n    PreferencesData.set(\"runtime.platform.path\", platformFolder.getAbsolutePath());\n    PreferencesData.set(\"runtime.hardware.path\", platformFolder.getParentFile().getAbsolutePath());\n  }","sourceCodeStart":894,"sourceCodeEnd":930,"githubUrl":"https://github.com/arduino/Arduino/blob/a0df6e0e83b652c72bc78b0a1376c54d6ebc3bee/arduino-core/src/processing/app/BaseNoGui.java#L894-L930","documentation":"BaseNoGui's replaceFileToBeInstalled throws this IOException when the downloaded temp file cannot be renamed onto the target path (temp.renameTo(file) returned false) after the old version was removed. renameTo() fails silently for cross-filesystem moves or when the target is locked.","triggerScenarios":"Installation of a library/tool: after deleting the old file, temp.renameTo(file) returns false — typically because temp and file live on different filesystems/mount points, or the destination is locked/undeletable in practice.","commonSituations":"Temp directory on a different drive/partition than the sketchbook (common on Windows or with TMPDIR overrides); antivirus locking the destination; installing to a read-only or admin-owned directory.","solutions":["Ensure the temp directory and target directory are on the same filesystem (e.g. set temp files next to the destination, or move TMPDIR onto the same volume)","Close programs locking the destination (IDE instances, antivirus) and retry","Run with write permission on the destination (fix ownership or install to the user sketchbook)","If implementing, replace renameTo with a copy+delete fallback (java.nio.file.Files.move with REPLACE_EXISTING)"],"exampleFix":"// before: renameTo fails across filesystems\nboolean result = temp.renameTo(file);\nif (!result) throw ...;\n// after: robust move\njava.nio.file.Files.move(temp.toPath(), file.toPath(),\n  java.nio.file.StandardCopyOption.REPLACE_EXISTING,\n  java.nio.file.StandardCopyOption.ATOMIC_MOVE); // or fall back to copy+delete","handlingStrategy":"fallback","validationCode":"String tempFs = temp.toPath().toAbsolutePath().getRoot().toString();\nString destFs = file.toPath().toAbsolutePath().getRoot().toString();\nif (!tempFs.equals(destFs)) throw new IllegalStateException(\"Temp and target on different volumes\");","typeGuard":null,"tryCatchPattern":"try {\n  installer.install(lib, replaceMode);\n} catch (IOException e) {\n  if (e.getMessage().startsWith(\"Could not replace\")) {\n    // destination locked or cross-volume; fix TMPDIR or close locking apps and retry\n  } else throw e;\n}","preventionTips":["Keep the OS temp dir on the same volume as the installation target","Don't set TMPDIR to a different drive than the sketchbook","Close file lockers (antivirus, editors, other IDEs) during installs","Prefer java.nio Files.move for your own move logic"],"tags":["filesystem","io","rename","installation"],"backgroundTag":"file-move-failed","analyzedSha":"a0df6e0e83b652c72bc78b0a1376c54d6ebc3bee","analyzedAt":"2026-09-06T10:13:38.901Z","contentChangedAt":"2026-09-06T10:13:38.901Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}