{"record":{"id":"4805e1a7dfdf3bfe","repo":"hashicorp/terraform","slug":"failed-to-generate-diff-for-s-s","errorCode":null,"errorMessage":"Failed to generate diff for %s: %s","messagePattern":"Failed to generate diff for (.+?): (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/fmt.go","lineNumber":213,"sourceCode":"\n\tresult := c.formatSourceCode(src, path)\n\n\tif !bytes.Equal(src, result) {\n\t\t// Something was changed\n\t\tif c.list {\n\t\t\tfmt.Fprintln(w, path)\n\t\t}\n\t\tif c.write {\n\t\t\terr := os.WriteFile(path, result, 0644)\n\t\t\tif err != nil {\n\t\t\t\tdiags = diags.Append(fmt.Errorf(\"Failed to write %s\", path))\n\t\t\t\treturn diags\n\t\t\t}\n\t\t}\n\t\tif c.diff {\n\t\t\tdiff, err := bytesDiff(src, result, path)\n\t\t\tif err != nil {\n\t\t\t\tdiags = diags.Append(fmt.Errorf(\"Failed to generate diff for %s: %s\", path, err))\n\t\t\t\treturn diags\n\t\t\t}\n\t\t\tw.Write(diff)\n\t\t}\n\t}\n\n\tif !c.list && !c.write && !c.diff {\n\t\t_, err = w.Write(result)\n\t\tif err != nil {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Failed to write result\"))\n\t\t}\n\t}\n\n\treturn diags\n}\n\nfunc (c *FmtCommand) processDir(path string, stdout io.Writer) tfdiags.Diagnostics {\n\tvar diags tfdiags.Diagnostics","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/fmt.go#L195-L231","documentation":"Thrown by processFile when the -diff flag is set and the internal bytesDiff helper returns an error while computing a unified diff between the original and formatted source. The underlying error is appended with %s.","triggerScenarios":"Running `terraform fmt -diff` where bytesDiff fails — typically an internal failure generating the diff textual output. Rare in practice since bytesDiff operates on in-memory byte slices.","commonSituations":"Edge cases in the diff algorithm with very large files or unusual content; generally indicates an internal bug rather than a user config problem.","solutions":["Retry without -diff to confirm formatting itself works: `terraform fmt -write <path>`.","Run `terraform fmt -check` to identify changed files without producing a diff.","Report the issue with the offending file contents if it reproduces, as bytesDiff failures are likely internal bugs."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// Run -diff in a fallback that drops to -list on diff failure.\nif d := runFmt(\"-diff\", path); d.HasErrors() {\n    _ = runFmt(\"-list\", path) // best-effort fallback\n}","preventionTips":["Treat -diff failures as likely internal bugs; keep -check as the primary CI signal.","Pin a stable Terraform version to avoid regressions in the diff code path."],"tags":["terraform-fmt","diff","internal"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}