{"record":{"id":"643a3e64aa4b7531","repo":"arduino/Arduino","slug":"failed-to-rename-0-to-1","errorCode":null,"errorMessage":"Failed to rename \"{0}\" to \"{1}\"","messagePattern":"Failed to rename \"(.+?)\" to \"(.+?)\"","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"arduino-core/src/processing/app/SketchFile.java","lineNumber":184,"sourceCode":"\n  /**\n   * Rename the given file to get the given name.\n   *\n   * @param newName\n   *          The new name, including extension, excluding directory\n   *          name.\n   * @throws IOException\n   *           When a problem occurs, or is expected to occur. The error\n   *           message should be already translated.\n   */\n  public void renameTo(String newName) throws IOException {\n    File newFile = new File(file.getParentFile(), newName);\n    sketch.checkNewFilename(newFile);\n    if (!file.exists() || file.renameTo(newFile)) {\n      renamedTo(newFile);\n    } else {\n      String msg = I18n.format(tr(\"Failed to rename \\\"{0}\\\" to \\\"{1}\\\"\"), file.getName(), newName);\n      throw new IOException(msg);\n    }\n  }\n\n  /**\n   * Should be called when this file was renamed and renameTo could not\n   * be used (e.g. when renaming the entire sketch directory).\n   */\n  protected void renamedTo(File what) {\n    file = what;\n  }\n\n  /*\n   * Returns the filename include extension.\n   */\n  public String getFileName() {\n    return file.getName();\n  }\n","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/arduino/Arduino/blob/a0df6e0e83b652c72bc78b0a1376c54d6ebc3bee/arduino-core/src/processing/app/SketchFile.java#L166-L202","documentation":"SketchFile.renameTo validates the new name against the sketch, then performs the OS rename of the underlying file; this IOException is thrown when the file is missing or the rename syscall fails. It is normally reached via nameCode when renaming a sketch tab.","triggerScenarios":"Renaming a tab/file where File.renameTo returns false or file.exists() is false: file deleted externally, locked by another process, target name exists on a case-insensitive filesystem, or unwritable directory.","commonSituations":"Renaming a tab while the file is open/locked in an external editor; the file was removed on disk behind the IDE's back; renaming only to change case on Windows/macOS.","solutions":["Verify the file still exists in the sketch folder and restore it if deleted","Close external editors holding the file open","Pick a name that differs beyond case, or rename in two steps (foo.ino -> foo2.ino -> Foo.ino)","Check write permission on the sketch folder"],"exampleFix":"// before\nsketchFile.renameTo(\"Led\"); // may fail if led.ino locked or case-only rename on Windows\n// after\nFile f = sketchFile.getFile();\nif (f.exists() && f.getParentFile().canWrite()) {\n  File tmp = new File(f.getParentFile(), \"tmp_renameme.ino\");\n  if (f.renameTo(tmp) && tmp.renameTo(new File(f.getParentFile(), \"Led.ino\"))) {\n    sketchFile.renamedTo(new File(f.getParentFile(), \"Led.ino\"));\n  }\n}","handlingStrategy":"try-catch","validationCode":"if (!file.exists()) throw new IllegalStateException(\"Source file missing: \" + file);\nif (!file.getParentFile().canWrite()) throw new IllegalStateException(\"Folder not writable\");","typeGuard":null,"tryCatchPattern":"try {\n  sketchFile.renameTo(newName);\n} catch (IOException e) {\n  System.err.println(\"Rename failed: \" + e.getMessage());\n  // offer Save As or manual file move\n}","preventionTips":["Confirm the file exists on disk before renaming tabs","Close external editors that may lock the file","Avoid case-only renames on Windows/macOS — rename in two steps","Check folder write permissions before programmatic renames"],"tags":["filesystem","rename","io"],"backgroundTag":"file-write-failed","analyzedSha":"a0df6e0e83b652c72bc78b0a1376c54d6ebc3bee","analyzedAt":"2026-09-06T10:13:38.901Z","contentChangedAt":"2026-09-06T10:13:38.901Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}