usebruno/bruno · error · Error
Cannot copy, ${path.basename(source)} already exists in ${pa
Error message
Cannot copy, ${path.basename(source)} already exists in ${path.basename(destination)} What it means
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.
Source
Thrown at packages/bruno-electron/src/utils/filesystem.js:412
fsExtra.outputFileSync(safePath, data, options);
} catch (err) {
console.error(`Error writing file at ${safePath}:`, err);
return Promise.reject(err);
}
}
function safeWriteFileSync(filePath, data) {
const safePath = getSafePathToWrite(filePath);
fs.writeFileSync(safePath, data);
}
// Recursively copies a source <file/directory> to a destination <directory>.
const copyPath = async (source, destination) => {
let targetPath = `${destination}/${path.basename(source)}`;
const targetPathExists = await fsPromises.access(targetPath).then(() => true).catch(() => false);
if (targetPathExists) {
throw new Error(`Cannot copy, ${path.basename(source)} already exists in ${path.basename(destination)}`);
}
const copy = async (source, destination) => {
const stat = await fsPromises.lstat(source);
if (stat.isDirectory()) {
await fsPromises.mkdir(destination, { recursive: true });
const entries = await fsPromises.readdir(source);
for (const entry of entries) {
const srcPath = path.join(source, entry);
const destPath = path.join(destination, entry);
await copy(srcPath, destPath);
}
} else {
await fsPromises.copyFile(source, destination);
}
};
await copy(source, targetPath);View on GitHub (pinned to 9bdd81c7bd)
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.
Example fix
// before
const targetPathExists = await fsPromises.access(targetPath).then(() => true).catch(() => false);
if (targetPathExists) {
throw new Error(`Cannot copy, ${path.basename(source)} already exists in ${path.basename(destination)}`);
}
// after: de-collide
let target = `${destination}/${path.basename(source)}`;
target = await ensureUnique(target); // appends ' copy', ' copy 2', ... Defensive patterns
Strategy: validation
Validate before calling
const target = path.join(destination, path.basename(source));
if (await fsPromises.access(target).then(() => true).catch(() => false)) {
throw new Error(`${path.basename(source)} already exists in ${destination}`);
}
await copyPath(source, destination); Type guard
async function copyTargetIsClear(source, destination) {
const target = path.join(destination, path.basename(source));
return fsPromises.access(target).then(() => false).catch(() => true);
} Try / catch
try {
await copyPath(source, destination);
} catch (err) {
if (err.message.startsWith('Cannot copy')) {
// pick a unique destination name and retry
const unique = await generateUniqueTarget(source, destination);
return copyPath(source, path.dirname(unique));
}
throw err;
} Prevention
- Pre-check the target path before copying.
- Use generateUniqueName to avoid collisions in duplicate actions.
- Offer overwrite/rename/skip on collision.
When it happens
Trigger: Copying a folder/file into a destination that already contains an entry of the same name; duplicate copy operation; destination not cleaned between runs.
Common situations: Re-running a copy/duplicate-collection action; merging two trees where names collide; target directory already holds a prior version.
Related errors
- collection: ${collectionPath} already exists
- directory: ${dir} already exists
- Failed to export ${environmentType} environments.
- Failed to process ${parsedFile.fileName}: ${err.message}
- Failed to parse the file – ensure it is valid JSON or YAML
AI-assisted analysis of usebruno/bruno@9bdd81c7bd (2026-08-13).
Data as JSON: /api/errors/d2c469cd0bf42690.
Report an issue: GitHub.