{"record":{"id":"7ada1f1eeedb40e5","repo":"different-ai/openwork","slug":"destination-exists","errorCode":"destination-exists","errorMessage":"Download destination already exists.","messagePattern":"Download destination already exists\\.","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"apps/desktop/electron/binary-transfer.mjs","lineNumber":322,"sourceCode":"        bytes += value.byteLength;\n        if (bytes > maxBytes) {\n          await reader.cancel();\n          throw transferError(`Download exceeds the ${maxBytes}-byte limit.`, \"file-too-large\");\n        }\n        await writeAll(stagingFile, value);\n      }\n    }\n    if (bytes === 0) throw transferError(\"Download response was empty.\", \"zero-byte-file\");\n    // Only a complete download reaches the workspace. Revalidate the\n    // destination, create it exclusively (\"wx\" never follows a final\n    // symlink), and prove by device and inode that the created file resides\n    // inside the authorized root before a single byte is written to it.\n    await resolveAuthorizedPath(input?.destinationPath, options?.authorizedRoots);\n    try {\n      destinationFile = await open(destinationPath, \"wx\");\n    } catch (error) {\n      if (error?.code === \"EEXIST\") {\n        throw transferError(\"Download destination already exists.\", \"destination-exists\");\n      }\n      throw error;\n    }\n    await verifyOpenFileWithinRoot(destinationFile, destinationPath, destination.rootRealPath, \"Download destination\");\n    const buffer = Buffer.allocUnsafe(1024 * 1024);\n    let position = 0;\n    while (position < bytes) {\n      signal?.throwIfAborted();\n      const { bytesRead } = await stagingFile.read(buffer, 0, Math.min(buffer.length, bytes - position), position);\n      if (bytesRead === 0) {\n        throw transferError(\"Downloaded data changed while it was being saved.\", \"size-mismatch\");\n      }\n      await writeAll(destinationFile, buffer.subarray(0, bytesRead));\n      position += bytesRead;\n    }\n    await destinationFile.sync();\n    await destinationFile.close();\n    destinationFile = undefined;","sourceCodeStart":304,"sourceCodeEnd":340,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/apps/desktop/electron/binary-transfer.mjs#L304-L340","documentation":"downloadBinaryToPath creates the destination exclusively with open(path, \"wx\"), which never follows a final symlink; an EEXIST is converted to the coded error 'destination-exists'. This prevents silently overwriting user files and blocks symlink attacks on the destination path. The destination must also resolve inside the authorized roots before this check.","triggerScenarios":"Calling downloadBinaryToPath with destinationPath pointing at a path that already exists (file, or dangling/symlinked entry) — open(\"wx\") raises EEXIST which is remapped to this error.","commonSituations":"Re-running a setup script that already downloaded the file; two concurrent workers downloading to the same path; stale artifact from a previous version; a symlink planted at the destination path.","solutions":["Delete or rename the existing file first (fs.rm / rename) if overwriting is intended.","Pick a unique destinationPath (timestamp/UUID suffix or version in the filename).","Add an existence check before the call and branch: skip, resume, or overwrite explicitly.","Serialize concurrent workers so only one downloads to a given path."],"exampleFix":"// before\nawait downloadBinaryToPath({ url, destinationPath });\n// after\nimport { rm } from \"node:fs/promises\";\nawait rm(destinationPath, { force: true }); // explicit overwrite intent\nawait downloadBinaryToPath({ url, destinationPath });","handlingStrategy":"validation","validationCode":"import { lstat } from \"node:fs/promises\";\nlet exists = false;\ntry { await lstat(destinationPath); exists = true; } catch (e) { if (e.code !== \"ENOENT\") throw e; }\nif (exists) throw new Error(`Choose another destination; ${destinationPath} already exists`);","typeGuard":"async function isFreeDestination(p) {\n  try { await lstat(p); return false; } catch (e) { return e.code === \"ENOENT\"; }\n}","tryCatchPattern":"try {\n  await downloadBinaryToPath({ url, destinationPath });\n} catch (e) {\n  if (e?.code === \"destination-exists\") {\n    return { ok: false, reason: \"exists\", path: destinationPath };\n  }\n  throw e;\n}","preventionTips":["Check lstat on the destination and branch (skip/rename/overwrite) before downloading.","Use unique filenames (version/hash/UUID) for generated artifacts.","Serialize or lock concurrent workers writing to the same destination."],"tags":["download","filesystem","exclusive-create","symlink"],"backgroundTag":"file-already-exists","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}