{"record":{"id":"f6f1c37056380bb4","repo":"laurent22/joplin","slug":"the-created-file-name-was-not-as-expected-and-the-existing","errorCode":null,"errorMessage":"The created file name was not as expected and the existing destination could not be resolved: input name was '${fileName}' but got: '${createdFileName}'","messagePattern":"The created file name was not as expected and the existing destination could not be resolved: input name was '(.+?)' but got: '(.+?)'","errorType":"exception","errorClass":"IOExceptionFast","httpStatus":null,"severity":"error","filePath":"packages/react-native-saf-x/android/src/main/java/com/reactnativesafx/utils/EfficientDocumentHelper.java","lineNumber":489,"sourceCode":"    if (!createdFileName.equals(fileName)) {\n      // A provider can change the name when a document with the requested name\n      // already exists. Do not try to rename this document into the occupied\n      // destination: a rename can partially succeed before reporting an error.\n      // Delete the document returned by this create call. Replacement-capable\n      // callers can then resolve and overwrite the intended destination;\n      // create-only callers retain their collision failure semantics.\n      unlink(createdFile);\n      if (!replaceIfDestinationExists) {\n        throw new IOExceptionFast(\n          \"The created file name was not as expected: input name was '\"\n            + fileName\n            + \"' but got: '\"\n            + createdFileName\n            + \"'\");\n      }\n      DocumentStat existingFile = findFile(parentDirOfFile, fileName);\n      if (existingFile == null || existingFile.isDirectory()) {\n        throw new IOExceptionFast(\n          \"The created file name was not as expected and the existing destination could not be resolved: input name was '\"\n            + fileName\n            + \"' but got: '\"\n            + createdFileName\n            + \"'\");\n      }\n      return existingFile.getInternalUri();\n    }\n\n    return createdFile;\n  }\n\n  private void writeToFile(final Uri uri, final String data, final String encoding, final boolean append) throws IOException {\n    byte[] bytes = GeneralHelper.stringToBytes(data, encoding);\n\n    try (OutputStream out =\n           context\n             .getContentResolver()","sourceCodeStart":471,"sourceCodeEnd":507,"githubUrl":"https://github.com/laurent22/joplin/blob/981a03c5c9e88130bccff4db47c411d35ec7ae2c/packages/react-native-saf-x/android/src/main/java/com/reactnativesafx/utils/EfficientDocumentHelper.java#L471-L507","documentation":"This is the replace-capable branch of the same name-mismatch check: the provider created a document whose name differs from the requested fileName, and when the library tried to resolve the intended existing destination (findFile on the parent directory) it found nothing, or found a directory. Since it cannot rewrite the created document onto the intended destination, it deletes the stray file and throws, leaving the filesystem unchanged.","triggerScenarios":"createFile with replaceIfDestinationExists=true where the provider renamed the created document (extension appended, collision dedupe) AND findFile(parentDirOfFile, fileName) returns null or a directory — i.e. no regular file with exactly the requested name exists in the target folder.","commonSituations":"Requesting 'data' with a mime that makes the provider create 'data.json'; names containing characters the provider normalizes (case/whitespace) so exact-match lookup fails; requesting a name that matches an existing folder, so findFile resolves a directory and the destination cannot be treated as a file.","solutions":["Pass a fileName that exactly matches what the provider will store, including the extension implied by mimeType","Ensure the requested name is not the name of an existing directory in the target folder","Create with a fresh unique name (timestamp/uuid suffix) instead of relying on replace semantics","Catch this error, list the directory contents, and retry with a corrected name"],"exampleFix":"// before\nawait Safx.createFile(treeUri, 'backup', 'application/json', true); // provider makes 'backup.json', no 'backup' exists -> throws\n// after\nawait Safx.createFile(treeUri, 'backup.json', 'application/json', true);","handlingStrategy":"try-catch","validationCode":"const existing = await Safx.statFile(treeUri, fileName);\nif (!existing || existing.isDirectory) {\n\t// destination not a regular file: don't rely on replace semantics; choose a new name\n}","typeGuard":null,"tryCatchPattern":"try {\n\treturn await Safx.createFile(treeUri, fileName, mimeType, true);\n} catch (e) {\n\tif (String(e?.message).includes('could not be resolved')) {\n\t\tconst unique = `${Date.now()}-${fileName}`;\n\t\treturn await Safx.createFile(treeUri, unique, mimeType, false);\n\t}\n\tthrow e;\n}","preventionTips":["Use file names that include the mimeType's expected extension so provider renames don't occur","Check with statFile that the destination exists as a regular file before using replace semantics","Never use a name that collides with an existing folder in the target directory","Fall back to a unique generated name on any name-mismatch failure"],"tags":["android","saf","filename","collision"],"backgroundTag":"file-not-found","analyzedSha":"981a03c5c9e88130bccff4db47c411d35ec7ae2c","analyzedAt":"2026-09-17T14:49:40.960Z","contentChangedAt":"2026-09-17T14:49:40.960Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}