{"record":{"id":"17d1a3c0ee99ed05","repo":"eclipse-vertx/vert.x","slug":"cannot-truncate-file-path-does-not-exist","errorCode":null,"errorMessage":"Cannot truncate file ${path}. Does not exist","messagePattern":"Cannot truncate file (.+?)\\. Does not exist","errorType":"exception","errorClass":"FileSystemException","httpStatus":null,"severity":"error","filePath":"vertx-core/src/main/java/io/vertx/core/file/impl/FileSystemImpl.java","lineNumber":504,"sourceCode":"        } catch (IOException e) {\n          throw new FileSystemException(getFileMoveErrorMessage(from, to), e);\n        }\n        return null;\n      }\n    };\n  }\n\n  private BlockingAction<Void> truncateInternal(String p, long len) {\n    Objects.requireNonNull(p);\n    return new BlockingAction<Void>() {\n      public Void perform() {\n        try {\n          String path = resolveFile(p).getAbsolutePath();\n          if (len < 0) {\n            throw new FileSystemException(\"Cannot truncate file to size < 0\");\n          }\n          if (!Files.exists(Paths.get(path))) {\n            throw new FileSystemException(\"Cannot truncate file \" + path + \". Does not exist\");\n          }\n          try (RandomAccessFile raf = new RandomAccessFile(path, \"rw\")) {\n            raf.setLength(len);\n          }\n        } catch (IOException e) {\n          throw new FileSystemException(getFileAccessErrorMessage(\"truncate\", p) ,e);\n        }\n        return null;\n      }\n    };\n  }\n\n  private BlockingAction<Void> chmodInternal(String path, String perms) {\n    return chmodInternal(path, perms, null);\n  }\n\n  protected BlockingAction<Void> chmodInternal(String path, String perms, String dirPerms) {\n    Objects.requireNonNull(path);","sourceCodeStart":486,"sourceCodeEnd":522,"githubUrl":"https://github.com/eclipse-vertx/vert.x/blob/fb308bd8c3f12c79f4ae89bef67fadf6c80d036e/vertx-core/src/main/java/io/vertx/core/file/impl/FileSystemImpl.java#L486-L522","documentation":"The truncate action checks Files.exists on the resolved absolute path and throws FileSystemException 'Cannot truncate file <path>. Does not exist' when the file is absent. Vert.x refuses to create/implicitly touch a file through truncate; the target must already exist. The path in the message is the resolved absolute path after FileResolver processing.","triggerScenarios":"vertx.fileSystem().truncate(path, len) on a path that was never created or was deleted/renamed before the call; also when a fat-jar classpath-relative path resolves to a location that doesn't exist on the real filesystem.","commonSituations":"Truncating a rotating log file that was renamed by a log rotation; race with another process deleting the file; typo'd or wrong working-directory-relative path when running from a jar; file created lazily later than the truncate call.","solutions":["Check fs.exists(path) and create the file first (fs.createFile) if absence is legitimate.","Log/print the resolved absolute path from the message and verify it matches expectations — fix relative-path assumptions.","Re-check the flow that deletes/renames the file concurrently and serialize those operations.","If truncation is optional, treat 'does not exist' as a no-op instead of an error in your caller code."],"exampleFix":"// before\nvertx.fileSystem().truncateBlocking(\"logs/app.log\", 0);\n// after\nFileSystem fs = vertx.fileSystem();\nif (fs.existsBlocking(\"logs/app.log\")) {\n  fs.truncateBlocking(\"logs/app.log\", 0);\n} else {\n  fs.createFileBlocking(\"logs/app.log\");\n}","handlingStrategy":"validation","validationCode":"FileSystem fs = vertx.fileSystem();\nString abs = Paths.get(path).toAbsolutePath().toString();\nif (!fs.existsBlocking(path)) {\n  fs.createFileBlocking(path); // or skip truncation\n}\nfs.truncateBlocking(path, len);","typeGuard":"static boolean truncatable(FileSystem fs, String path) {\n  return fs.existsBlocking(path) && fs.propsBlocking(path).isFile();\n}","tryCatchPattern":"try {\n  fs.truncateBlocking(path, len);\n} catch (FileSystemException e) {\n  if (e.getMessage().endsWith(\"Does not exist\")) {\n    fs.createFileBlocking(path);\n    fs.truncateBlocking(path, len);\n  } else throw e;\n}","preventionTips":["Check exists() before every truncate, or create the file on demand","Beware relative paths under fat-jar deployment — verify the resolved absolute path","Serialize truncate with any delete/rename (log rotation) logic","Treat 'file absent' as a legitimate no-op where the semantics allow it"],"tags":["file-system","truncate","file-not-found"],"backgroundTag":"file-not-found","analyzedSha":"fb308bd8c3f12c79f4ae89bef67fadf6c80d036e","analyzedAt":"2026-09-06T11:37:12.241Z","contentChangedAt":"2026-09-06T11:37:12.241Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}