{"record":{"id":"d42242e20e8602fc","repo":"kopia/kopia","slug":"erruseralreadyexists","errorCode":"ErrUserAlreadyExists","errorMessage":"user already exists","messagePattern":"user already exists","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/user/user_manager.go","lineNumber":27,"sourceCode":"\t\"strings\"\n\n\t\"github.com/pkg/errors\"\n\n\t\"github.com/kopia/kopia/repo\"\n\t\"github.com/kopia/kopia/repo/manifest\"\n)\n\n// ManifestType is the type of the manifest used to represent user accounts.\nconst ManifestType = \"user\"\n\n// UsernameAtHostnameLabel is the manifest label identifying users by username@hostname.\nconst UsernameAtHostnameLabel = \"username\"\n\n// ErrUserNotFound is returned to indicate that a user was not found in the system.\nvar ErrUserNotFound = errors.New(\"user not found\")\n\n// ErrUserAlreadyExists indicates that a user already exist in the system when attempting to create a new one.\nvar ErrUserAlreadyExists = errors.New(\"user already exists\")\n\n// LoadProfileMap returns the map of all users profiles in the repository by username, using old map as a cache.\nfunc LoadProfileMap(ctx context.Context, rep repo.Repository, old map[string]*Profile) (map[string]*Profile, error) {\n\tif rep == nil {\n\t\treturn nil, nil\n\t}\n\n\tentries, err := rep.FindManifests(ctx, map[string]string{manifest.TypeLabelKey: ManifestType})\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"error listing user manifests\")\n\t}\n\n\tresult := map[string]*Profile{}\n\n\tfor _, m := range manifest.DedupeEntryMetadataByLabel(entries, UsernameAtHostnameLabel) {\n\t\tuser := m.Labels[UsernameAtHostnameLabel]\n\n\t\t// same user info as before","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/kopia/kopia/blob/82495e54b584c1ef6073c9e1be048f57f8aef078/internal/user/user_manager.go#L9-L45","documentation":"ErrUserAlreadyExists is the sentinel returned when attempting to create a new user profile whose username@hostname label already has manifests in the repository, enforcing uniqueness of server users (internal/user/user_manager.go:117).","triggerScenarios":"Calling user.GetNewProfile(ctx, rep, username) (or SetupUser) when the repository already contains one or more user manifests labeled with that username (len(manifests) != 0).","commonSituations":"Re-running 'kopia server user add' for an existing user; provisioning scripts that are not idempotent and re-create users on each run; two server instances sharing one repository both registering the same user.","solutions":["Check existence first (GetUserProfile) and update the existing profile instead of creating a new one.","Match with errors.Is(err, user.ErrUserAlreadyExists) and treat as success in idempotent setup scripts.","Use a different username@hostname if a genuinely separate account is intended."],"exampleFix":"// before\np, err := user.SetupUser(ctx, rep, \"alice@somehost\", pwd) // fails on re-run\n// after\nif _, err := user.GetUserProfile(ctx, rep, \"alice@somehost\"); errors.Is(err, user.ErrUserNotFound) {\n    _, err = user.SetupUser(ctx, rep, \"alice@somehost\", pwd)\n}\n// else: user already exists, reuse it","handlingStrategy":"try-catch","validationCode":"// before creating, check for existing user\n_, err := user.GetUserProfile(ctx, rep, \"alice@somehost\")\nexists := !errors.Is(err, user.ErrUserNotFound)","typeGuard":null,"tryCatchPattern":"p, err := user.SetupUser(ctx, rep, \"alice@somehost\", pwd)\nif errors.Is(err, user.ErrUserAlreadyExists) {\n    // idempotent: fetch and reuse the existing profile\n    p, err = user.GetUserProfile(ctx, rep, \"alice@somehost\")\n}","preventionTips":["Make user provisioning idempotent: check then create, or treat already-exists as success.","Always match with errors.Is(err, user.ErrUserAlreadyExists).","Avoid re-running 'kopia server user add' for existing users; update instead.","Ensure only one server instance manages a given repository's users."],"tags":["user","duplicate","repository","server"],"backgroundTag":"file-already-exists","analyzedSha":"82495e54b584c1ef6073c9e1be048f57f8aef078","analyzedAt":"2026-09-07T20:35:21.689Z","contentChangedAt":"2026-09-07T20:35:21.689Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}