{"record":{"id":"b84b14238543bc97","repo":"juanfont/headscale","slug":"marshalling-machine-key-w","errorCode":null,"errorMessage":"marshalling machine key: %w","messagePattern":"marshalling machine key: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/headscale/cli/generate.go","lineNumber":29,"sourceCode":"\trootCmd.AddCommand(generateCmd)\n\tgenerateCmd.AddCommand(generatePrivateKeyCmd)\n}\n\nvar generateCmd = &cobra.Command{\n\tUse:     \"generate\",\n\tShort:   \"Generate commands\",\n\tAliases: []string{\"gen\"},\n}\n\nvar generatePrivateKeyCmd = &cobra.Command{\n\tUse:   \"private-key\",\n\tShort: \"Generate a private key for the headscale server\",\n\tRunE: func(cmd *cobra.Command, args []string) error {\n\t\tmachineKey := key.NewMachine()\n\n\t\tmachineKeyStr, err := machineKey.MarshalText()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"marshalling machine key: %w\", err)\n\t\t}\n\n\t\treturn printOutput(cmd, map[string]string{\n\t\t\t\"private_key\": string(machineKeyStr),\n\t\t},\n\t\t\tstring(machineKeyStr))\n\t},\n}\n","sourceCodeStart":11,"sourceCodeEnd":38,"githubUrl":"https://github.com/juanfont/headscale/blob/565fd254d06c4c7f9a8cad1714a43445c79ba420/cmd/headscale/cli/generate.go#L11-L38","documentation":"Error from 'headscale generate private-key' when MachineKey.MarshalText fails while serializing a freshly generated key.NewMachine() value. MarshalText on a machine key encodes it to its text (base64-style) form; it fails only if the underlying key data cannot be encoded, which for a newly generated key is effectively unreachable defensive error handling.","triggerScenarios":"Practically never: it would require key.NewMachine to produce data the encoder rejects (zero-length or malformed key), which the constructor prevents. Seen only as noise in code paths that must satisfy the error-returning MarshalText API.","commonSituations":"None realistically; if it ever appears, suspect memory corruption or a patched/forked key package rather than operational misconfiguration.","solutions":["Rerun the command — a transient occurrence points to toolchain/binary corruption, not config","Rebuild the binary from clean sources (make clean && make build)","Verify the tailscale key package is unmodified if running a fork"],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"if _, err := machineKey.MarshalText(); err != nil {\n\t// practically unreachable; regenerate rather than handle\n\tmachineKey = key.NewMachine()\n}","preventionTips":["Do not build handling around this error; treat as a bug if observed","Rebuild from clean sources on occurrence"],"tags":["crypto","key-generation","unreachable-defensive","go"],"backgroundTag":null,"analyzedSha":"565fd254d06c4c7f9a8cad1714a43445c79ba420","analyzedAt":"2026-08-15T13:12:30.133Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}