{"record":{"id":"d2c469cd0bf42690","repo":"usebruno/bruno","slug":"cannot-copy-path-basename-source-already-exis","errorCode":null,"errorMessage":"Cannot copy, ${path.basename(source)} already exists in ${path.basename(destination)}","messagePattern":"Cannot copy, (.+?) already exists in (.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/bruno-electron/src/utils/filesystem.js","lineNumber":412,"sourceCode":"    fsExtra.outputFileSync(safePath, data, options);\n  } catch (err) {\n    console.error(`Error writing file at ${safePath}:`, err);\n    return Promise.reject(err);\n  }\n}\n\nfunction safeWriteFileSync(filePath, data) {\n  const safePath = getSafePathToWrite(filePath);\n  fs.writeFileSync(safePath, data);\n}\n\n// Recursively copies a source <file/directory> to a destination <directory>.\nconst copyPath = async (source, destination) => {\n  let targetPath = `${destination}/${path.basename(source)}`;\n\n  const targetPathExists = await fsPromises.access(targetPath).then(() => true).catch(() => false);\n  if (targetPathExists) {\n    throw new Error(`Cannot copy, ${path.basename(source)} already exists in ${path.basename(destination)}`);\n  }\n\n  const copy = async (source, destination) => {\n    const stat = await fsPromises.lstat(source);\n    if (stat.isDirectory()) {\n      await fsPromises.mkdir(destination, { recursive: true });\n      const entries = await fsPromises.readdir(source);\n      for (const entry of entries) {\n        const srcPath = path.join(source, entry);\n        const destPath = path.join(destination, entry);\n        await copy(srcPath, destPath);\n      }\n    } else {\n      await fsPromises.copyFile(source, destination);\n    }\n  };\n\n  await copy(source, targetPath);","sourceCodeStart":394,"sourceCodeEnd":430,"githubUrl":"https://github.com/usebruno/bruno/blob/9bdd81c7bdc57006e5f5ebffb79321a8d979f712/packages/bruno-electron/src/utils/filesystem.js#L394-L430","documentation":"Thrown by copyPath when destination/path.basename(source) already exists. copyPath is a copy-into-directory primitive that refuses to overwrite, so any pre-existing entry with the source's basename at the destination aborts the copy before it starts.","triggerScenarios":"Copying a folder/file into a destination that already contains an entry of the same name; duplicate copy operation; destination not cleaned between runs.","commonSituations":"Re-running a copy/duplicate-collection action; merging two trees where names collide; target directory already holds a prior version.","solutions":["Pre-check existence of the target path and pick a unique name (generateUniqueName) before copying.","Prompt the user to overwrite, rename, or skip on collision.","If overwrite is genuinely desired, delete the target first or use a copy primitive that supports overwrite."],"exampleFix":"// before\nconst targetPathExists = await fsPromises.access(targetPath).then(() => true).catch(() => false);\nif (targetPathExists) {\n  throw new Error(`Cannot copy, ${path.basename(source)} already exists in ${path.basename(destination)}`);\n}\n\n// after: de-collide\nlet target = `${destination}/${path.basename(source)}`;\ntarget = await ensureUnique(target); // appends ' copy', ' copy 2', ...","handlingStrategy":"validation","validationCode":"const target = path.join(destination, path.basename(source));\nif (await fsPromises.access(target).then(() => true).catch(() => false)) {\n  throw new Error(`${path.basename(source)} already exists in ${destination}`);\n}\nawait copyPath(source, destination);","typeGuard":"async function copyTargetIsClear(source, destination) {\n  const target = path.join(destination, path.basename(source));\n  return fsPromises.access(target).then(() => false).catch(() => true);\n}","tryCatchPattern":"try {\n  await copyPath(source, destination);\n} catch (err) {\n  if (err.message.startsWith('Cannot copy')) {\n    // pick a unique destination name and retry\n    const unique = await generateUniqueTarget(source, destination);\n    return copyPath(source, path.dirname(unique));\n  }\n  throw err;\n}","preventionTips":["Pre-check the target path before copying.","Use generateUniqueName to avoid collisions in duplicate actions.","Offer overwrite/rename/skip on collision."],"tags":["filesystem","name-collision","copy"],"backgroundTag":null,"analyzedSha":"9bdd81c7bdc57006e5f5ebffb79321a8d979f712","analyzedAt":"2026-08-13T04:09:25.751Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}