{"record":{"id":"15cd1b967d2334a8","repo":"nats-io/nats-server","slug":"duplicate-entry-for-q","errorCode":null,"errorMessage":"duplicate entry for %q","messagePattern":"duplicate entry for %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/accounts.go","lineNumber":808,"sourceCode":"\treturn a.AddWeightedMappings(src, NewMapDest(dest, 100))\n}\n\n// AddWeightedMappings will add in a weighted mappings for the destinations.\nfunc (a *Account) AddWeightedMappings(src string, dests ...*MapDest) error {\n\ta.mu.Lock()\n\tdefer a.mu.Unlock()\n\n\tif !IsValidSubject(src) {\n\t\treturn ErrBadSubject\n\t}\n\n\tm := &mapping{src: src, wc: subjectHasWildcard(src), dests: make([]*destination, 0, len(dests)+1)}\n\tseen := make(map[string]struct{})\n\n\tvar tw = make(map[string]uint8)\n\tfor _, d := range dests {\n\t\tif _, ok := seen[d.Subject]; ok {\n\t\t\treturn fmt.Errorf(\"duplicate entry for %q\", d.Subject)\n\t\t}\n\t\tseen[d.Subject] = struct{}{}\n\t\tif d.Weight > 100 {\n\t\t\treturn fmt.Errorf(\"individual weights need to be <= 100\")\n\t\t}\n\t\ttw[d.Cluster] += d.Weight\n\t\tif tw[d.Cluster] > 100 {\n\t\t\treturn fmt.Errorf(\"total weight needs to be <= 100\")\n\t\t}\n\t\terr := ValidateMapping(src, d.Subject)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\ttr, err := NewSubjectTransform(src, d.Subject)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tif d.Cluster == _EMPTY_ {","sourceCodeStart":790,"sourceCodeEnd":826,"githubUrl":"https://github.com/nats-io/nats-server/blob/3a66a489d262bf89b71a71c955c94920394532f3/server/accounts.go#L790-L826","documentation":"This function builds weighted cluster/subject mappings (e.g. service imports or weight mappings). It rejects duplicate destination subjects within a single mapping definition: if two destination entries share the same Subject string, it fails with this error naming the duplicated subject.","triggerScenarios":"Passing a dests slice to the mapping builder where two entries have identical d.Subject values — typically from merging config sources that each contributed the same account/subject, or duplicated entries in server config (accounts/imports/exports lists).","commonSituations":"Overlapping config snippets (operator config + resolver) that both define the same mapping destination; copy-pasted account import entries; automated config generation concatenating lists without deduplication.","solutions":["Deduplicate the dests slice by Subject before building the mapping (keep last/first occurrence per policy)","Find and merge the duplicate source in config (server config, operator config, resolver) so each subject appears once","If duplicates are intentional (multiple weights), aggregate them into a single entry with summed weights instead of separate entries"],"exampleFix":"// before\ndests := []Destination{{Subject: \"a\"}, {Subject: \"a\"}}\nm, err := buildMapping(src, dests) // duplicate entry for \"a\"\n// after\ndests = dedupeBySubject(dests)\nm, err := buildMapping(src, dests)","handlingStrategy":"validation","validationCode":"seen := make(map[string]struct{})\nfor _, d := range dests {\n    if _, dup := seen[d.Subject]; dup {\n        return fmt.Errorf(\"duplicate subject %q in mapping %q\", d.Subject, src)\n    }\n    seen[d.Subject] = struct{}{}\n}","typeGuard":null,"tryCatchPattern":"m, err := buildMapping(src, dests)\nif err != nil {\n    var dupSubj string\n    if n, _ := fmt.Sscanf(err.Error(), \"duplicate entry for %q\", &dupSubj); n == 1 {\n        return fmt.Errorf(\"config error: subject %q defined multiple times in mapping\", dupSubj)\n    }\n    return err\n}","preventionTips":["Deduplicate destination lists when merging config layers","Validate config at load time before building mappings","Treat duplicate subjects as a config-load error with file/line info"],"tags":["server","configuration","duplicate-detection"],"backgroundTag":"duplicate-config-entry","analyzedSha":"3a66a489d262bf89b71a71c955c94920394532f3","analyzedAt":"2026-09-02T04:41:54.247Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}