{"record":{"id":"60441395910b79df","repo":"golang/go","slug":"error-cannot-use-w-with-standard-input","errorCode":null,"errorMessage":"error: cannot use -w with standard input","messagePattern":"error: cannot use -w with standard input","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/gofmt/gofmt.go","lineNumber":410,"sourceCode":"\t\tif err != nil {\n\t\t\ts.AddReport(fmt.Errorf(\"creating cpu profile: %s\", err))\n\t\t\treturn\n\t\t}\n\t\tdefer func() {\n\t\t\tf.Close()\n\t\t\t<-fdSem\n\t\t}()\n\t\tpprof.StartCPUProfile(f)\n\t\tdefer pprof.StopCPUProfile()\n\t}\n\n\tinitParserMode()\n\tinitRewrite()\n\n\targs := flag.Args()\n\tif len(args) == 0 {\n\t\tif *write {\n\t\t\ts.AddReport(fmt.Errorf(\"error: cannot use -w with standard input\"))\n\t\t\treturn\n\t\t}\n\t\ts.Add(0, func(r *reporter) error {\n\t\t\treturn processFile(\"<standard input>\", nil, os.Stdin, r)\n\t\t})\n\t\treturn\n\t}\n\n\tfor _, arg := range args {\n\t\t// Walk each given argument as a directory tree.\n\t\t// If the argument is not a directory, it's always formatted as a Go file.\n\t\t// If the argument is a directory, we walk it, ignoring non-Go files.\n\t\tif err := filepath.WalkDir(arg, func(path string, d fs.DirEntry, err error) error {\n\t\t\tswitch {\n\t\t\tcase err != nil:\n\t\t\t\treturn err\n\t\t\tcase d.IsDir():\n\t\t\t\treturn nil // simply recurse into directories","sourceCodeStart":392,"sourceCodeEnd":428,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/gofmt/gofmt.go#L392-L428","documentation":"Thrown by gofmtMain when no file/directory arguments are supplied (len(args)==0) and the -w (write) flag is set. With no arguments gofmt reads from standard input, and writing formatted output back over stdin is meaningless/impossible, so the combination is rejected up front.","triggerScenarios":"Invoking `gofmt -w` with no positional arguments, or piping a file into gofmt while requesting in-place write, e.g. `cat foo.go | gofmt -w` or just `gofmt -w`.","commonSituations":"Forgetting to pass the file path after -w, copy-pasting a pipeline from a snippet that used -w, or scripts that conditionally drop the filename argument.","solutions":["Pass the target files explicitly: `gofmt -w <files...>` or `gofmt -w .`.","Drop -w when reading from stdin: `gofmt < foo.go` writes formatted output to stdout.","If scripting, ensure the argument list is non-empty before adding -w."],"exampleFix":"// before\ncat main.go | gofmt -w\n\n// after\ngofmt -w main.go","handlingStrategy":"validation","validationCode":"if *write && len(flag.Args()) == 0 {\n    return errors.New(\"-w requires file/dir arguments; reading stdin is incompatible with -w\")\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass explicit files/dirs when using -w.","When piping from stdin, omit -w and redirect stdout to the target file.","In wrapper scripts, assert a non-empty argument list before forwarding -w."],"tags":["gofmt","cli","stdin","flags"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}