{"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":"warning","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/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/fmt.go#L195-L231","documentation":"Returned by FmtCommand.processFile (fmt.go:213) when bytesDiff fails to produce a unified diff between the original and reformatted source (only when -diff is set and the content changed). bytesDiff writes both byte slices to os.CreateTemp temp files and shells out to the external `diff` command; the error message includes the path and the underlying error for diagnosis.","triggerScenarios":"bytesDiff returns a non-nil error: os.CreateTemp fails (no temp dir / disk full), the `diff` binary is not on PATH (exec.LookUp fails → exec error), the external `diff` command exits abnormally with no output (signal, crash), or writing the temp files fails.","commonSituations":"Minimal containers (distroless, scratch-based) without the `diff` utility installed; PATH not inherited in the shell running Terraform; /tmp or TMPDIR full or read-only; `diff` binary missing on Windows unless a Unix-y toolchain is installed.","solutions":["Install the `diff` utility (part of `diffutils`/`coreutils`): in Debian/Alpine containers `apt-get install -y diffutils` or `apk add diffutils`.","Ensure the directory containing `diff` is on PATH for the Terraform process: `which diff` and export PATH.","Ensure TMPDIR/TEMP is writable and has free space for the two temp files.","If you cannot install `diff`, drop the -diff flag and inspect changes via -write=false output redirection."],"exampleFix":"# before (minimal container)\n$ terraform fmt -diff main.tf\nFailed to generate diff for main.tf: exec: \"diff\": executable file not found in $PATH\n\n# after\n$ apk add --no-cache diffutils   # or apt-get install -y diffutils\nterraform fmt -diff main.tf","handlingStrategy":"validation","validationCode":"// Probe for the diff utility and a usable temp dir before enabling -diff\nif c.diff {\n    if _, err := exec.LookPath(\"diff\"); err != nil {\n        return errors.New(\"-diff requires the 'diff' utility on PATH\")\n    }\n    if f, err := os.CreateTemp(\"\", \"\"); err != nil {\n        return fmt.Errorf(\"-diff needs a writable temp dir: %w\", err)\n    } else { f.Close(); os.Remove(f.Name()) }\n}","typeGuard":"// DiffAvailable reports whether the external `diff` command exists on PATH.\nfunc DiffAvailable() bool {\n    _, err := exec.LookPath(\"diff\")\n    return err == nil\n}","tryCatchPattern":"// If -diff generation fails, degrade gracefully by continuing without the diff rather than failing the whole fmt run.\nif diff, err := bytesDiff(src, result, path); err != nil {\n    log.Printf(\"warning: could not generate diff for %s (%v)\", path, err)\n} else {\n    w.Write(diff)\n}","preventionTips":["Install diffutils/diff in minimal containers.","Verify TMPDIR/TEMP is writable before using -diff.","Drop -diff when the utility is unavailable; rely on -write=false output.","Inherit a sane PATH in shells that launch Terraform."],"tags":["cli","fmt","diff","external-command","containers"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}