{"record":{"id":"8f707a74aa7e7156","repo":"Unity-Technologies/UnityCsReference","slug":"a-command-with-id-id-already-exists","errorCode":null,"errorMessage":"A command with id {id} already exists","messagePattern":"A command with id (.+?) already exists","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Commands/CommandService.cs","lineNumber":114,"sourceCode":"        static void Initialize()\n        {\n#pragma warning disable UA2001 // The Banned API Analyzer produces compile errors for any new Linq code. This pre-existing usage has been suppressed, but should be rewritten if possible.\n            s_Commands = ScanAttributes().ToDictionary(c => c.id, c => c);\n#pragma warning restore UA2001\n        }\n\n        public static string GetCommandLabel(string commandId)\n        {\n            if (!Exists(commandId))\n                throw new ArgumentException($\"No command with id {commandId} exists\", nameof(commandId));\n\n            return s_Commands[commandId].label;\n        }\n\n        public static void RegisterCommand(string id, string label, CommandHandler handler, CommandHint hint = CommandHint.Any)\n        {\n            if (Exists(id))\n                throw new ArgumentException($\"A command with id {id} already exists\", nameof(id));\n\n            s_Commands[id] = new Command {id = id, label = label ?? id, hint = hint, handler = handler, managed = false};\n        }\n\n        public static void RegisterCommand(string id, CommandHandler handler, CommandHint hint = CommandHint.Any)\n        {\n            RegisterCommand(id, id, handler, hint);\n        }\n\n        public static bool UnregisterCommand(string id)\n        {\n            if (!Exists(id))\n                return false;\n\n            if (s_Commands[id].managed)\n                Debug.LogWarning($\"The command {id} was not explicitly registered by the user and should not be unregistered.\");\n\n            return s_Commands.Remove(id);","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Commands/CommandService.cs#L96-L132","documentation":"ArgumentException thrown by CommandService.RegisterCommand when a command with the given id already exists in s_Commands. The service forbids duplicate registrations to prevent handler ambiguity; both attribute-scanned (managed) and runtime-registered commands share the namespace.","triggerScenarios":"Calling RegisterCommand(id, ...) with an id that already exists — either declared via a Command attribute on a method, or previously registered at runtime. Common when a package/editor extension re-registers on domain reload without unregistering first.","commonSituations":"InitializeOnLoad or OnCodeLoaded code that registers commands every reload; two packages claiming the same command id; re-running a registration routine after a hot reload.","solutions":["Call UnregisterCommand(id) before registering, or guard with Exists(id).","Register commands once (idempotently) using Exists(id) checks.","Namespace command ids by package/author to avoid collisions with attribute-scanned commands."],"exampleFix":"// before\nCommandService.RegisterCommand(\"my.cmd\", handler);\n\n// after\nif (!CommandService.Exists(\"my.cmd\"))\n    CommandService.RegisterCommand(\"my.cmd\", handler);","handlingStrategy":"validation","validationCode":"if (CommandService.Exists(id)) return;\nCommandService.RegisterCommand(id, label, handler, hint);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Guard registrations with Exists (idempotent).","Namespace command ids by package/author.","Unregister before re-registering on reload."],"tags":["unity-editor","commands","argument-validation","duplicate-registration"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}