{"record":{"id":"18f841ce8219a36b","repo":"go-kratos/kratos","slug":"s-already-exists","errorCode":null,"errorMessage":"%s already exists","messagePattern":"(.+?) already exists","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"cmd/kratos/internal/proto/add/proto.go","lineNumber":37,"sourceCode":"// Generate generate a proto template.\nfunc (p *Proto) Generate() error {\n\tbody, err := p.execute()\n\tif err != nil {\n\t\treturn err\n\t}\n\twd, err := os.Getwd()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tto := filepath.Join(wd, p.Path)\n\tif _, err := os.Stat(to); os.IsNotExist(err) {\n\t\tif err := os.MkdirAll(to, 0o700); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\tname := filepath.Join(to, p.Name)\n\tif _, err := os.Stat(name); !os.IsNotExist(err) {\n\t\treturn fmt.Errorf(\"%s already exists\", p.Name)\n\t}\n\treturn os.WriteFile(name, body, 0o644)\n}\n","sourceCodeStart":19,"sourceCodeEnd":41,"githubUrl":"https://github.com/go-kratos/kratos/blob/668db92c2c001e9552594ba5a8aede8456af6d7e/cmd/kratos/internal/proto/add/proto.go#L19-L41","documentation":"proto.Add (used by kratos proto add / new for API templates) is create-only: it creates the target directory if missing, then refuses to overwrite an existing file with the same name. The error names the file that already exists under the target path.","triggerScenarios":"Calling the proto add path twice, or scaffolding an API file whose final path os.Stat finds already existing (p.Path/p.Name combination present on disk).","commonSituations":"Re-running generation after a partial failure; two services sharing the same proto file name; leftover files from a previous layout attempt.","solutions":["Delete or rename the existing file, then re-run the command","Choose a different proto file/service name for the new API","If regenerating on purpose, script the delete step first — the tool never overwrites"],"exampleFix":"# before\n$ kratos proto add user/v1/user.proto\n# ERROR: user.proto already exists\n\n# after\n$ rm api/user/v1/user.proto\n$ kratos proto add user/v1/user.proto","handlingStrategy":"validation","validationCode":"target := filepath.Join(wd, p.Path, p.Name)\nif _, err := os.Stat(target); err == nil {\n    return fmt.Errorf(\"skip %s: already exists\", p.Name)\n}\n// proceed with proto add","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Delete stale generated files before re-running generators","Adopt unique proto file names per version directory","Treat generators as create-only: remove-then-generate in a script"],"tags":["cli","proto","filesystem","code-generation"],"backgroundTag":null,"analyzedSha":"668db92c2c001e9552594ba5a8aede8456af6d7e","analyzedAt":"2026-08-16T02:07:20.704Z","schemaVersion":2},"datasetVersion":"2026-08-16T03:17:38.424Z"}