{"record":{"id":"5753fec72424527a","repo":"ipfs/kubo","slug":"core-commands-unexpected-type-t-expected-core","errorCode":null,"errorMessage":"core/commands: unexpected type %T, expected *\"core/commands\".Command","messagePattern":"core/commands: unexpected type %T, expected \\*\"core/commands\"\\.Command","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/commands/commands.go","lineNumber":30,"sourceCode":"\t\"os\"\n\t\"slices\"\n\t\"strings\"\n\n\tcmds \"github.com/ipfs/go-ipfs-cmds\"\n)\n\ntype commandEncoder struct {\n\tw io.Writer\n}\n\nfunc (e *commandEncoder) Encode(v any) error {\n\tvar (\n\t\tcmd *Command\n\t\tok  bool\n\t)\n\n\tif cmd, ok = v.(*Command); !ok {\n\t\treturn fmt.Errorf(`core/commands: unexpected type %T, expected *\"core/commands\".Command`, v)\n\t}\n\n\tfor _, s := range cmdPathStrings(cmd, cmd.showOpts) {\n\t\t_, err := e.w.Write([]byte(s + \"\\n\"))\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\treturn nil\n}\n\ntype Command struct {\n\tName        string\n\tSubcommands []Command\n\tOptions     []Option\n\n\tshowOpts bool","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/core/commands/commands.go#L12-L48","documentation":"This is the Encode method of the commands command-listing encoder: it expects the value handed to it to be a *Command from package core/commands. Any other value means the command's Run/encoder wiring returned the wrong result type. The message includes the actual dynamic type for diagnosis.","triggerScenarios":"Invoking the `ipfs commands` listing command path with an encoder whose Run/Emitter produced something other than *core/commands.Command; changing the command's Run return type without updating the encoder.","commonSituations":"Modifying core/commands/commands.go Run function to emit a different structure; custom forks or embedded builds where the emitted value type drifted from the encoder's expectation.","solutions":["Ensure the command's Run emits exactly a *core/commands.Command via the emitter (e.Emit(cmd))","If you changed the emitted type, update Encode to match the new type","Check for nil/mis-typed emitter values in custom wiring"],"exampleFix":"// before\nreturn e.Emit(cmdStrings) // []string\n// after\nreturn e.Emit(cmd) // *Command","handlingStrategy":"type-guard","validationCode":"cmd, ok := v.(*Command)\nif !ok {\n    return fmt.Errorf(\"encoder expected *Command, got %T\", v)\n}","typeGuard":"func asCommand(v interface{}) (*Command, bool) {\n    c, ok := v.(*Command)\n    return c, ok\n}","tryCatchPattern":"if err := e.Emit(cmd); err != nil {\n    if strings.Contains(err.Error(), \"unexpected type\") {\n        log.Fatalf(\"command Run must emit *Command, got wrong type\")\n    }\n    return err\n}","preventionTips":["Keep the emitted value type in Run aligned with Encode's expectation","Run `go build ./core/commands/...` after changing command Run signatures","Add a unit test exercising the commands listing encoder"],"tags":["cli","encoding","type-assertion"],"backgroundTag":"unexpected-encoder-type","analyzedSha":"329838acdfafae224582930457efe80aa217afc0","analyzedAt":"2026-09-03T18:30:52.135Z","contentChangedAt":"2026-09-03T18:30:52.135Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}