{"record":{"id":"5b4955e307b2b400","repo":"hashicorp/terraform","slug":"option-write-cannot-be-used-when-reading-from-std","errorCode":null,"errorMessage":"Option -write cannot be used when reading from stdin","messagePattern":"Option -write cannot be used when reading from stdin","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"internal/command/fmt.go","lineNumber":120,"sourceCode":"\t\tif list {\n\t\t\tio.Copy(&cli.UiWriter{Ui: c.Ui}, buf)\n\t\t}\n\t\tif ok {\n\t\t\treturn 0\n\t\t} else {\n\t\t\treturn 3\n\t\t}\n\t}\n\n\treturn 0\n}\n\nfunc (c *FmtCommand) fmt(paths []string, stdin io.Reader, stdout io.Writer) tfdiags.Diagnostics {\n\tvar diags tfdiags.Diagnostics\n\n\tif len(paths) == 0 { // Assuming stdin, then.\n\t\tif c.write {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Option -write cannot be used when reading from stdin\"))\n\t\t\treturn diags\n\t\t}\n\t\tfileDiags := c.processFile(\"<stdin>\", stdin, stdout, true)\n\t\tdiags = diags.Append(fileDiags)\n\t\treturn diags\n\t}\n\n\tfor _, path := range paths {\n\t\tpath = c.normalizePath(path)\n\t\tinfo, err := os.Stat(path)\n\t\tif err != nil {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"No file or directory at %s\", path))\n\t\t\treturn diags\n\t\t}\n\t\tif info.IsDir() {\n\t\t\tdirDiags := c.processDir(path, stdout)\n\t\t\tdiags = diags.Append(dirDiags)\n\t\t} else {","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/fmt.go#L102-L138","documentation":"Returned by FmtCommand.fmt (fmt.go:115) when paths is empty (stdin mode) and the -write flag is still true. When fmt reads from stdin it cannot write back to a source file, so -write is meaningless; the Run method already disables write when args[0] == \"-\", but this check catches the case where the user explicitly forces -write=true with stdin input. It is a precondition failure surfaced as a diagnostic.","triggerScenarios":"Invoking `terraform fmt -write=true -` (explicit write with stdin marker), or piping input while keeping the default -write=true behavior in a code path that does not pre-clear it. The Run method auto-disables write for stdin, so this fires mainly when -write is explicitly set after the auto-disable.","commonSituations":"Piping HCL into fmt with `-write` left on: `cat main.tf | terraform fmt -write=true -`; a wrapper script that always passes -write=true; copy-paste of a fmt invocation that worked on files but now targets stdin.","solutions":["Drop the -write flag when reading from stdin: `terraform fmt -` (write is auto-disabled for stdin).","Explicitly disable it: `terraform fmt -write=false -`.","If you want to write formatted output to a file from stdin, redirect stdout: `terraform fmt - < in.tf > out.tf`.","For files/directories, keep -write (the default) — the error only applies to stdin."],"exampleFix":"# before\necho 'resource \"x\" \"y\"{}' | terraform fmt -write=true -\n# Option -write cannot be used when reading from stdin\n\n# after\necho 'resource \"x\" \"y\"{}' | terraform fmt -","handlingStrategy":"validation","validationCode":"// Clear -write automatically when the target is stdin\nisStdin := len(paths) == 0 || (len(args) > 0 && args[0] == stdinArg)\nif isStdin && c.write {\n    c.write = false\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Do not pass -write when piping via stdin.","Let fmt auto-disable -write for stdin rather than forcing it.","Redirect stdout to capture formatted stdin output instead of using -write.","Document the stdin-vs-file flag matrix for wrapper scripts."],"tags":["cli","fmt","stdin","flags","user-input"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}