{"record":{"id":"31bdb8cd0b8a20ec","repo":"larksuite/cli","slug":"hooks-newargs-must-return-s","errorCode":null,"errorMessage":"Hooks.NewArgs must return *%s","messagePattern":"Hooks\\.NewArgs must return \\*(.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"shortcuts/common/typed_external.go","lineNumber":32,"sourceCode":")\n\n// CompileCommandDefinition is the single sealed entry from commandhost into\n// the existing shortcut compiler. All authoring fields retain\n// extension/command as their owner; the internal token prevents this function\n// from becoming a second public compiler API.\nfunc CompileCommandDefinition(definition commandbridge.Definition, _ commandbridge.Access) (Shortcut, error) {\n\tif definition.ArgsType == nil || definition.DataType == nil {\n\t\treturn Shortcut{}, fmt.Errorf(\"ArgsType and DataType are required\")\n\t}\n\tif definition.Hooks.NewArgs == nil {\n\t\treturn Shortcut{}, fmt.Errorf(\"Hooks.NewArgs is required\")\n\t}\n\tnewArgs, err := probeNewArgs(definition.Hooks.NewArgs)\n\tif err != nil {\n\t\treturn Shortcut{}, err\n\t}\n\tif newArgs == nil || reflect.TypeOf(newArgs) != reflect.PointerTo(definition.ArgsType) {\n\t\treturn Shortcut{}, fmt.Errorf(\"Hooks.NewArgs must return *%s\", definition.ArgsType)\n\t}\n\n\toutput := definition.Output\n\tif definition.PageOutput {\n\t\toutput.Meta.Pagination = true\n\t}\n\tcompiled, err := compileDefinitionParts(\n\t\tdefinition.Metadata,\n\t\tdefinition.Input,\n\t\toutput,\n\t\tdefinition.ArgsType,\n\t\tdefinition.DataType,\n\t\tadaptBridgeHooks(definition.Hooks),\n\t\tbridgeRendererMarkers(definition.Hooks.Renderers),\n\t\tdefinition.PageOutput,\n\t)\n\tif err != nil {\n\t\treturn Shortcut{}, err","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/larksuite/cli/blob/7fd6ef3c07182257ce776cdc5a614e122d5bd4b3/shortcuts/common/typed_external.go#L14-L50","documentation":"After invoking NewArgs via probeNewArgs, the compiler verifies the returned value is exactly a pointer to the declared ArgsType (reflect.TypeOf(newArgs) == reflect.PointerTo(definition.ArgsType)). A nil result or a different concrete type fails with the expected type name in the message.","triggerScenarios":"NewArgs returns a value type instead of a pointer (return MyArgs{} instead of &MyArgs{}); NewArgs returns a different struct's pointer than ArgsType declares; NewArgs returns nil; mismatch after changing ArgsType without updating the hook.","commonSituations":"Refactoring the args struct name/type in one place only; copy-pasting NewArgs from another command with different args; generics or interface wrappers causing the returned reflect type to differ from the declared ArgsType.","solutions":["Return a pointer to the exact declared args type: func() any { return &MyArgs{} }","Keep ArgsType = reflect.TypeOf(MyArgs{}) and NewArgs returning *MyArgs in sync","If NewArgs can return nil, fix the constructor — nil is always rejected"],"exampleFix":"// before\nNewArgs: func() any { return MyArgs{} },\n// after\nNewArgs: func() any { return &MyArgs{} },","handlingStrategy":"type-guard","validationCode":"v := definition.Hooks.NewArgs()\nif v == nil || reflect.TypeOf(v) != reflect.PointerTo(definition.ArgsType) { return errors.New(\"NewArgs return type mismatch\") }","typeGuard":"func newArgsMatches(d commandbridge.Definition) bool {\n    v, err := probeSafe(d.Hooks.NewArgs)\n    return err == nil && v != nil && reflect.TypeOf(v) == reflect.PointerTo(d.ArgsType)\n}","tryCatchPattern":"sc, err := common.CompileCommandDefinition(def, access)\nif err != nil {\n    return fmt.Errorf(\"compile %s: %w\", def.Metadata.Command, err)\n}","preventionTips":["Always write NewArgs as returning &MyArgs{}, never a value","Change ArgsType and NewArgs in the same edit","Keep ArgsType derived from the same struct NewArgs constructs, e.g. via a single generic helper"],"tags":["go","shortcut-compiler","commandbridge","type-mismatch","hooks"],"backgroundTag":"type-mismatch","analyzedSha":"7fd6ef3c07182257ce776cdc5a614e122d5bd4b3","analyzedAt":"2026-09-04T21:17:44.649Z","contentChangedAt":"2026-09-04T21:17:44.649Z","schemaVersion":2},"datasetVersion":"2026-09-12T02:17:10.037Z"}