{"record":{"id":"2695e803bdeafd62","repo":"ruvnet/ruflo","slug":"config-file-already-exists-targetpath-use-f","errorCode":null,"errorMessage":"Config file already exists: ${targetPath}. Use --force to overwrite.","messagePattern":"Config file already exists: (.+?)\\. Use --force to overwrite\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"v3/@claude-flow/cli/src/services/config-file-manager.ts","lineNumber":125,"sourceCode":"    const config = this.getConfig(cwd);\n    return getNestedValue(config, key);\n  }\n\n  /** Set a nested config value by dot-separated key */\n  set(cwd: string, key: string, value: unknown): void {\n    const config = this.getConfig(cwd);\n    setNestedValue(config, key, value);\n    this.config = config;\n    const targetPath = this.configPath ?? path.resolve(cwd, CONFIG_FILENAMES[0]);\n    this.writeAtomic(targetPath, config);\n    this.configPath = targetPath;\n  }\n\n  /** Create a new config file with defaults */\n  create(cwd: string, overrides?: Record<string, unknown>, force?: boolean): string {\n    const targetPath = path.resolve(cwd, CONFIG_FILENAMES[0]);\n    if (fs.existsSync(targetPath) && !force) {\n      throw new Error(`Config file already exists: ${targetPath}. Use --force to overwrite.`);\n    }\n    const config = { ...DEFAULT_CONFIG, ...overrides };\n    this.writeAtomic(targetPath, config);\n    this.config = config;\n    this.configPath = targetPath;\n    return targetPath;\n  }\n\n  /** Reset config to defaults */\n  reset(cwd: string): string {\n    const targetPath = this.configPath ?? path.resolve(cwd, CONFIG_FILENAMES[0]);\n    this.writeAtomic(targetPath, DEFAULT_CONFIG);\n    this.config = { ...DEFAULT_CONFIG };\n    this.configPath = targetPath;\n    return targetPath;\n  }\n\n  /** Export config to a specific path */","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/ruvnet/ruflo/blob/fa13ee4ad60ac2090b1480656eb233521790d640/v3/@claude-flow/cli/src/services/config-file-manager.ts#L107-L143","documentation":"ConfigFileManager.create(cwd, overrides?, force?) refuses to clobber an existing config file: it resolves cwd/claude-flow.config.json (CONFIG_FILENAMES[0]) and, if that path already exists and force was not passed as true, throws with a pointer to use --force. This protects existing settings from being reset to { ...DEFAULT_CONFIG, ...overrides } by a re-run of init.","triggerScenarios":"Running project init twice (e.g. `claude-flow init` re-run in an already-initialized repo), automated scaffolding scripts that call create() on every run, or a CI bootstrap step hitting a repo where claude-flow.config.json is committed.","commonSituations":"Re-running a setup wizard after a partial failure where the file was already written, template generators that initialize the config unconditionally, onboarding scripts for new team members run in a cloned repo that already ships config.","solutions":["If you only want to change values, use set(cwd, key, value) instead of create() — it merges into the existing file","If you truly want defaults again, pass force: manager.create(cwd, overrides, true) (CLI: --force), after backing up the current file","Otherwise, detect the existing file first with fs.existsSync and skip creation"],"exampleFix":"// before\nmanager.create(cwd, { swarm: { topology: 'mesh' } }); // repo already has claude-flow.config.json -> throws\n\n// after\nif (fs.existsSync(path.join(cwd, 'claude-flow.config.json'))) {\n  manager.set(cwd, 'swarm.topology', 'mesh'); // merge into existing\n} else {\n  manager.create(cwd, { swarm: { topology: 'mesh' } });\n}","handlingStrategy":"validation","validationCode":"const configPath = path.join(cwd, 'claude-flow.config.json'); // CONFIG_FILENAMES[0]\nif (fs.existsSync(configPath)) {\n  manager.set(cwd, 'some.key', value); // merge, don't clobber\n} else {\n  manager.create(cwd, overrides);\n}","typeGuard":"const configExists = (cwd: string): boolean => fs.existsSync(path.join(cwd, 'claude-flow.config.json'));","tryCatchPattern":"try {\n  manager.create(cwd, overrides);\n} catch (e) {\n  if (e instanceof Error && e.message.includes('Use --force to overwrite')) {\n    manager.set(cwd, 'key', value); // or create(cwd, overrides, true) after backing up\n  } else throw e;\n}","preventionTips":["Make init scripts idempotent: existsSync check or set() instead of unconditional create()","Use create(..., true) / --force only after backing up the existing config","Prefer set() for value changes — create() resets everything not listed in overrides"],"tags":["config","init","file-exists","idempotency"],"backgroundTag":"file-already-exists","analyzedSha":"fa13ee4ad60ac2090b1480656eb233521790d640","analyzedAt":"2026-08-18T21:34:22.708Z","contentChangedAt":"2026-08-18T21:34:22.708Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}