usebruno/bruno · error · Error

collection: ${collectionPath} already exists

Error message

collection: ${collectionPath} already exists

What it means

Thrown by importCollection at the top of the flow: the computed collectionPath (collectionLocation + sanitized folder name) already exists on disk. This is a preflight check before createDirectory is called, intended to stop an import from clobbering an existing collection.

Source

Thrown at packages/bruno-electron/src/utils/collection-import.js:34

  if (fs.existsSync(collectionPath)) {
    return findUniqueFolderName(baseName, collectionLocation, counter + 1);
  }

  return folderName;
}

/**
 * Import a collection - shared logic used by both IPC handler and onboarding service
 * @param {Object} options - Optional settings
 * @param {boolean} options.skipOpenEvent - If true, don't send main:collection-opened event (caller will handle it)
 */
async function importCollection(collection, collectionLocation, mainWindow, uniqueFolderName = null, format = DEFAULT_COLLECTION_FORMAT, options = {}) {
  // Use provided unique folder name or use collection name
  let folderName = uniqueFolderName ? sanitizeName(uniqueFolderName) : sanitizeName(collection.name);
  let collectionPath = path.join(collectionLocation, folderName);

  if (fs.existsSync(collectionPath)) {
    throw new Error(`collection: ${collectionPath} already exists`);
  }

  // Recursive function to parse the collection items and create files/folders
  const parseCollectionItems = async (items = [], currentPath) => {
    for (const item of items) {
      if (['http-request', 'graphql-request', 'grpc-request'].includes(item.type)) {
        let sanitizedFilename = sanitizeName(item.filename || `${item.name}.${format}`);
        const content = await stringifyRequestViaWorker(item, { format });
        const filePath = path.join(currentPath, sanitizedFilename);
        safeWriteFileSync(filePath, content);
      }
      if (item.type === 'folder') {
        let sanitizedFolderName = sanitizeName(item.filename || item.name);
        const folderPath = path.join(currentPath, sanitizedFolderName);
        fs.mkdirSync(folderPath);

        if (item.root?.meta?.name) {
          const folderFilePath = path.join(folderPath, `folder.${format}`);

View on GitHub (pinned to 9bdd81c7bd)

Solutions

  1. Use findUniqueFolderName(baseName, collectionLocation) to auto-pick a non-colliding name before calling importCollection.
  2. Prompt the user to rename, overwrite, or skip when a collision is detected.
  3. If the folder is a leftover from a failed import, remove it or choose a different location.
  4. Pass an explicit uniqueFolderName argument to importCollection.

Example fix

// before
let folderName = uniqueFolderName ? sanitizeName(uniqueFolderName) : sanitizeName(collection.name);
let collectionPath = path.join(collectionLocation, folderName);
if (fs.existsSync(collectionPath)) {
  throw new Error(`collection: ${collectionPath} already exists`);
}

// after: de-collide first
const folderName = await findUniqueFolderName(
  uniqueFolderName || collection.name,
  collectionLocation
);
const collectionPath = path.join(collectionLocation, sanitizeName(folderName));
Defensive patterns

Strategy: validation

Validate before calling

const folderName = await findUniqueFolderName(uniqueFolderName || collection.name, collectionLocation);
const collectionPath = path.join(collectionLocation, sanitizeName(folderName));
if (fs.existsSync(collectionPath)) {
  throw new Error(`Refusing to import: ${collectionPath} already exists`);
}

Type guard

function isImportPathClear(collectionLocation, folderName) {
  return !fs.existsSync(path.join(collectionLocation, sanitizeName(folderName)));
}

Try / catch

try {
  await importCollection(collection, collectionLocation, mainWindow, uniqueFolderName, format, options);
} catch (err) {
  if (err.message.includes('already exists')) {
    const unique = await findUniqueFolderName(collection.name, collectionLocation);
    return importCollection(collection, collectionLocation, mainWindow, unique, format, options);
  }
  throw err;
}

Prevention

When it happens

Trigger: Importing a collection whose sanitized name collides with an existing folder at collectionLocation; re-importing the same collection twice; user typed a name that sanitizes to an already-present folder.

Common situations: Duplicate import attempts; two collections with the same name; sanitizeName collapsing different names to the same slug; leftover folder from a failed prior import.

Related errors


AI-assisted analysis of usebruno/bruno@9bdd81c7bd (2026-08-13). Data as JSON: /api/errors/1004a8661efc4b41. Report an issue: GitHub.