{"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":"validation","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/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/fmt.go#L102-L138","documentation":"Thrown by FmtCommand.fmt when no path arguments are given (so input is assumed to come from stdin) AND the -write flag is set. Writing back to stdin is meaningless — you cannot overwrite a pipe — so Terraform refuses the combination. The user must either drop -write or supply a real file path.","triggerScenarios":"Running `terraform fmt -write` (or `-w`) with no file arguments, e.g. piping HCL through `cat config.tf | terraform fmt -write`. Also when -write is set globally and the user forgets to name a file.","commonSituations":"User wants to both format stdin output and persist it, forgetting stdin can't be written back; copy-pasting a `-w` habit into a pipe workflow; defaulting -write=true in a wrapper that sometimes runs without paths.","solutions":["Drop -write when reading from stdin: `terraform fmt < file.tf`.","Or pass an explicit path so -write has somewhere to write: `terraform fmt -write file.tf`.","If scripting, branch: use -write only when paths are provided."],"exampleFix":"// before\n//   cat main.tf | terraform fmt -write\n// after (choose one):\n//   cat main.tf | terraform fmt                 # stream, no write\n//   terraform fmt -write main.tf                # in-place write to a file","handlingStrategy":"validation","validationCode":"// In wrappers, only enable -write when real paths are present\nfunc fmtArgs(write bool, paths []string) []string {\n    args := []string{\"fmt\"}\n    if write && len(paths) > 0 { args = append(args, \"-write\") }\n    args = append(args, paths...)\n    return args\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Don't combine -write/-w with stdin input.","Branch scripting on whether paths are provided.","Remember: with no path, fmt always reads stdin and prints to stdout."],"tags":["fmt","cli","stdin","flags","user-error"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}