{"record":{"id":"798bbf6e844e46a4","repo":"nikivdev/code","slug":"tool-already-exists","errorCode":null,"errorMessage":"Tool '{}' already exists","messagePattern":"Tool '(.+?)' already exists","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"src/tools.rs","lineNumber":158,"sourceCode":"        .status()\n        .context(\"failed to run bun\")?;\n\n    if !status.success() {\n        bail!(\"Tool '{}' exited with status: {}\", name, status);\n    }\n\n    Ok(())\n}\n\n/// Create a new tool.\nfn new_tool(name: &str, description: Option<&str>, use_ai: bool) -> Result<()> {\n    let tools_dir = get_tools_dir()?;\n    fs::create_dir_all(&tools_dir).context(\"failed to create tools directory\")?;\n\n    let tool_file = tools_dir.join(format!(\"{}.ts\", name));\n\n    if tool_file.exists() {\n        bail!(\"Tool '{}' already exists\", name);\n    }\n\n    if use_ai {\n        // Use localcode to generate the tool\n        let localcode = find_localcode();\n        if localcode.is_none() {\n            bail!(\n                \"localcode not found. Install it with:\\n  \\\n                 cd <opencode-repo> && flow link\"\n            );\n        }\n\n        let desc = description.unwrap_or(name);\n        let prompt = format!(\n            \"Create a TypeScript tool for Bun called '{}' that: {}\\n\\n\\\n             Requirements:\\n\\\n             - Use Bun APIs (Bun.$, Bun.file, etc.)\\n\\\n             - Add a description comment at the top\\n\\","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/tools.rs#L140-L176","documentation":"Raised by new_tool (src/tools.rs:158) when scaffolding a tool whose target file <tools_dir>/<name>.ts already exists. The scaffold step refuses to overwrite an existing tool to avoid destroying user code.","triggerScenarios":"Calling `f tools new <name>` when a tool with that name was already created — e.g. re-running an init script twice, or intentionally trying to regenerate a tool that exists.","commonSituations":"Idempotent setup scripts re-invoked on an already-configured machine; name collision with an existing tool; forgetting the tool was created earlier in the session.","solutions":["If regeneration is intended, delete or rename the existing <tools_dir>/<name>.ts first, then re-run `f tools new <name>`.","If the tool already exists, just use it — no need to scaffold again.","Choose a different name if you meant to create a distinct tool.","Guard setup scripts: skip `tools new` if the .ts file already exists."],"exampleFix":"// before: naive setup script\nf tools new deploy\n// after: idempotent\n[ -f \"$TOOLS_DIR/deploy.ts\" ] || f tools new deploy","handlingStrategy":"validation","validationCode":"const fs = require(\"fs\");\nconst toolFile = path.join(toolsDir, name + \".ts\");\nif (fs.existsSync(toolFile)) {\n  console.error(`tool ${name} already exists at ${toolFile}; skipping scaffold`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  newTool(name);\n} catch (e) {\n  if (String(e).includes(\"already exists\")) {\n    console.error(\"edit the existing tool instead of re-scaffolding\");\n  } else throw e;\n}","preventionTips":["Make setup scripts idempotent: check existence before `tools new`.","Keep a consistent tool naming convention to avoid collisions.","Back up an existing tool before intentionally regenerating it."],"tags":["already-exists","tooling","cli"],"backgroundTag":"file-already-exists","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}