{"record":{"id":"dc698d69c270d2e9","repo":"kopia/kopia","slug":"error-marshaling-computed-snapshot-diff-stats","errorCode":null,"errorMessage":"error marshaling computed snapshot diff stats","messagePattern":"error marshaling computed snapshot diff stats","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command_diff.go","lineNumber":77,"sourceCode":"\t\treturn errors.Wrap(err, \"error creating comparer\")\n\t}\n\tdefer d.Close() //nolint:errcheck\n\n\tif c.diffCompareFiles {\n\t\tparts := strings.Split(c.diffCommandCommand, \" \")\n\t\td.DiffCommand = parts[0]\n\t\td.DiffArguments = parts[1:]\n\t}\n\n\tif isDir1 {\n\t\tsnapshotDiffStats, err := d.Compare(ctx, ent1, ent2)\n\t\tif err != nil {\n\t\t\treturn errors.Wrap(err, \"error comparing directories\")\n\t\t}\n\n\t\tb, err := json.Marshal(snapshotDiffStats)\n\t\tif err != nil {\n\t\t\treturn errors.Wrap(err, \"error marshaling computed snapshot diff stats\")\n\t\t}\n\n\t\tfmt.Fprintf(c.out.stdout(), \"%s\", b) //nolint:errcheck\n\n\t\treturn nil\n\t}\n\n\treturn errors.New(\"comparing files not implemented yet\")\n}\n\nfunc defaultDiffCommand() string {\n\tif isWindows() {\n\t\treturn \"cmp\"\n\t}\n\n\treturn \"diff -u\"\n}\n","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/kopia/kopia/blob/82495e54b584c1ef6073c9e1be048f57f8aef078/cli/command_diff.go#L59-L95","documentation":"Wraps a failure of json.Marshal(snapshotDiffStats) after a successful directory comparison in `kopia diff`. marshaling a well-formed diff stats struct essentially never fails in practice, so this indicates an internal invariant violation or an unsupported value sneaking into the stats (e.g. NaN/Inf from division in stats computation).","triggerScenarios":"Directory-mode diff completes, but json.Marshal of the snapshotDiffStats value returns an error — practically only when the stats struct contains values JSON cannot represent or a programming defect changes the struct.","commonSituations":"Rare; encountered after custom builds/patches to the diff stats types, or in the presence of NaN/Inf values produced by stat calculations.","solutions":["Re-run on a stock kopia build to rule out local modifications","If reproducible, file a bug with the repository/directory pair involved","Work around by using stats-only or non-directory diff output"],"exampleFix":"// before\nb, err := json.Marshal(snapshotDiffStats)\nif err != nil {\n\treturn errors.Wrap(err, \"error marshaling computed snapshot diff stats\")\n}\n// after\nb, err := json.Marshal(snapshotDiffStats)\nif err != nil {\n\tlog(ctx).Errorf(\"diff stats: %+v\", snapshotDiffStats)\n\treturn errors.Wrap(err, \"error marshaling computed snapshot diff stats\")\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"b, err := json.Marshal(snapshotDiffStats)\nif err != nil {\n\tlog.Printf(\"diff stats marshal failed: %v; stats=%+v\", err, snapshotDiffStats)\n\treturn errors.Wrap(err, \"error marshaling computed snapshot diff stats\")\n}","preventionTips":["Keep diff stats types JSON-marshalable (no NaN/Inf fields)","Rebuild from upstream source if using local patches","Report persistent occurrences as upstream bugs with reproduction steps"],"tags":["kopia","cli","json","internal"],"backgroundTag":"json-marshal-failed","analyzedSha":"82495e54b584c1ef6073c9e1be048f57f8aef078","analyzedAt":"2026-09-07T20:35:21.689Z","contentChangedAt":"2026-09-07T20:35:21.689Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}