{"record":{"id":"2603e71f318b53a5","repo":"usebruno/bruno","slug":"directory-dir-already-exists-2603e7","errorCode":null,"errorMessage":"directory: ${dir} already exists","messagePattern":"directory: (.+?) already exists","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/bruno-electron/src/utils/filesystem.js","lineNumber":162,"sourceCode":"\nconst hasRequestExtension = (filename, format = null) => {\n  if (!filename || typeof filename !== 'string') return false;\n\n  if (format) {\n    const ext = format === 'yml' ? 'yml' : 'bru';\n    return filename.toLowerCase().endsWith(`.${ext}`);\n  }\n\n  return ['bru', 'yml'].some((ext) => filename.toLowerCase().endsWith(`.${ext}`));\n};\n\nconst createDirectory = async (dir) => {\n  if (!dir) {\n    throw new Error(`directory: path is null`);\n  }\n\n  if (fs.existsSync(dir)) {\n    throw new Error(`directory: ${dir} already exists`);\n  }\n\n  return fs.mkdirSync(dir);\n};\n\nconst browseDirectory = async (win) => {\n  const { filePaths } = await dialog.showOpenDialog(win, {\n    properties: ['openDirectory', 'createDirectory']\n  });\n\n  if (!filePaths || !filePaths[0]) {\n    return false;\n  }\n\n  const resolvedPath = path.resolve(filePaths[0]);\n  return isDirectory(resolvedPath) ? resolvedPath : false;\n};\n","sourceCodeStart":144,"sourceCodeEnd":180,"githubUrl":"https://github.com/usebruno/bruno/blob/9bdd81c7bdc57006e5f5ebffb79321a8d979f712/packages/bruno-electron/src/utils/filesystem.js#L144-L180","documentation":"Thrown by createDirectory when fs.existsSync(dir) is true. Unlike mkdirSync(recursive), createDirectory intentionally refuses to overwrite an existing directory — it is a create-not-overwrite primitive.","triggerScenarios":"Calling createDirectory for a path that already exists on disk; two concurrent creations racing on the same path; retry after a partial run that already made the folder.","commonSituations":"Re-running an import/create after a previous success; duplicate folder name; a sync from git already populated the directory.","solutions":["Check existence first or switch to a unique-name strategy (generateUniqueName).","If the existing directory is the intended target, skip creation rather than treating it as an error.","Use fs.mkdirSync(dir, { recursive: true }) directly when overwrite-equivalent idempotency is desired."],"exampleFix":"// before\nif (fs.existsSync(dir)) {\n  throw new Error(`directory: ${dir} already exists`);\n}\nreturn fs.mkdirSync(dir);\n\n// after: idempotent when caller signals reuse\nconst createDirectory = async (dir, { existsOk = false } = {}) => {\n  if (!dir) throw new Error('directory: path is null');\n  if (fs.existsSync(dir)) {\n    if (existsOk) return dir;\n    throw new Error(`directory: ${dir} already exists`);\n  }\n  return fs.mkdirSync(dir);\n};","handlingStrategy":"validation","validationCode":"async function createDirectoryIdempotent(dir) {\n  if (fs.existsSync(dir)) return dir;\n  return createDirectory(dir);\n}","typeGuard":"function directoryMissing(dir) {\n  return !fs.existsSync(dir);\n}","tryCatchPattern":"try {\n  await createDirectory(dir);\n} catch (err) {\n  if (err.message.endsWith('already exists')) return dir; // idempotent success\n  throw err;\n}","preventionTips":["Check existence first if you want idempotent semantics.","Use generateUniqueName when the name may collide.","Reserve createDirectory for create-not-overwrite intent only."],"tags":["filesystem","name-collision"],"backgroundTag":null,"analyzedSha":"9bdd81c7bdc57006e5f5ebffb79321a8d979f712","analyzedAt":"2026-08-13T04:09:25.751Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}