{"record":{"id":"ef76ed120d5779c3","repo":"apache/hadoop","slug":"cannot-overwrite-existing-resource-file","errorCode":null,"errorMessage":"Cannot overwrite existing resource file","messagePattern":"Cannot overwrite existing resource file","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"hadoop-tools/hadoop-compat-bench/src/main/java/org/apache/hadoop/fs/compat/common/HdfsCompatShellScope.java","lineNumber":146,"sourceCode":"  protected void copyResource(String res, File dst) throws IOException {\n    copyResource(res, dst, false);\n  }\n\n  private void copyResource(String res, File dst, boolean overwrite)\n      throws IOException {\n    InputStream in = null;\n    try {\n      in = this.getClass().getResourceAsStream(res);\n      if (in == null) {\n        in = this.suite.getClass().getResourceAsStream(res);\n      }\n      if (in == null) {\n        throw new IOException(\"Resource not found\" +\n            \" during scripts prepare: \" + res);\n      }\n\n      if (dst.exists() && !overwrite) {\n        throw new IOException(\"Cannot overwrite existing resource file\");\n      }\n\n      Files.createDirectories(dst.getParentFile().toPath());\n\n      byte[] buf = new byte[1024];\n      try (OutputStream out = new FileOutputStream(dst)) {\n        int nRead = in.read(buf);\n        while (nRead != -1) {\n          out.write(buf, 0, nRead);\n          nRead = in.read(buf);\n        }\n      }\n    } finally {\n      if (in != null) {\n        in.close();\n      }\n    }\n  }","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-tools/hadoop-compat-bench/src/main/java/org/apache/hadoop/fs/compat/common/HdfsCompatShellScope.java#L128-L164","documentation":"copyResource refuses to clobber files it did not create: when the destination file already exists and the call was made with overwrite=false (everything except the sandbox log4j.properties, which passes overwrite=true), it throws IOException('Cannot overwrite existing resource file'). This keeps a prior run's extracted scripts from being silently mixed with a new run's.","triggerScenarios":"Re-running the compat shell scope into the same working/script directory where a previous run already extracted /cases/... files; a failed first attempt leaving partial files; two bench runs sharing one directory.","commonSituations":"Retry after a failed run without cleaning the work dir; CI workspaces that persist between builds; parallel bench runs pointed at the same scratch directory.","solutions":["Delete the previous sandbox/script directory between runs (rm -rf <scriptDir> or the whole work dir)","Give each run a fresh working directory instead of reusing one","Ensure only one bench run uses a given directory at a time"],"exampleFix":"# before: rerun in the same work dir\nhadoop ... HdfsCompatTool -uri ... -suite myshellsuite\n# -> IOException: Cannot overwrite existing resource file\n\n# after: clean work dir first\nrm -rf $COMPAT_WORKDIR && hadoop ... HdfsCompatTool -uri ... -suite myshellsuite","handlingStrategy":"validation","validationCode":"// before preparing scripts, make sure the destination tree is clean\nFile scriptDir = new File(workDir, \"scripts\");\nif (scriptDir.exists()) {\n  FileUtils.deleteQuietly(scriptDir); // bench refuses to overwrite stale files\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat the bench work directory as disposable and recreate it per run","In CI, use a fresh workspace or add an explicit cleanup step before each run","Never point two concurrent bench runs at the same work directory"],"tags":["hadoop-compat-bench","file-conflict","idempotency","shell-tests"],"backgroundTag":"file-already-exists","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}