{"record":{"id":"612485e2da2a14a2","repo":"golang/go","slug":"creating-cpu-profile-s","errorCode":null,"errorMessage":"creating cpu profile: %s","messagePattern":"creating cpu profile: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/gofmt/gofmt.go","lineNumber":393,"sourceCode":"\t// call gofmtMain in a separate function\n\t// so that it can use defer and have them\n\t// run before the exit.\n\tgofmtMain(s)\n\tos.Exit(s.GetExitCode())\n}\n\nfunc gofmtMain(s *sequencer) {\n\tcounter.Open()\n\tflag.Usage = usage\n\tflag.Parse()\n\tcounter.Inc(\"gofmt/invocations\")\n\tcounter.CountFlags(\"gofmt/flag:\", *flag.CommandLine)\n\n\tif *cpuprofile != \"\" {\n\t\tfdSem <- true\n\t\tf, err := os.Create(*cpuprofile)\n\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","sourceCodeStart":375,"sourceCodeEnd":411,"githubUrl":"https://github.com/golang/go/blob/b6b368adc57c96c3151d224d172029f233ead2c3/src/cmd/gofmt/gofmt.go#L375-L411","documentation":"Thrown by gofmtMain when os.Create(*cpuprofile) fails while the -cpuprofile flag is set. The error is wrapped with the creating context and reported via s.AddReport; gofmt then returns without profiling. The underlying error is whatever os.Create returns (permission denied, no such directory, read-only fs).","triggerScenarios":"Running `gofmt -cpuprofile=<path> ...` where <path> is in a non-existent directory, an unwritable location, a path that exists as a directory, or a filesystem that is read-only.","commonSituations":"Forgetting to create the output directory, pointing -cpuprofile at a path under /proc or another special filesystem, running as a user without write permission to the target, or specifying a directory path instead of a file path.","solutions":["Create the parent directory: `mkdir -p $(dirname <path>)`.","Check write permissions on the target directory and use a writable location like /tmp.","Ensure <path> is a file path, not an existing directory.","Drop -cpuprofile if profiling is not needed."],"exampleFix":"// before\ngofmt -cpuprofile=/nonexistent/cpu.prof .\n\n// after\nmkdir -p /tmp/prof && gofmt -cpuprofile=/tmp/prof/cpu.prof .","handlingStrategy":"validation","validationCode":"profDir := filepath.Dir(*cpuprofile)\nif info, err := os.Stat(profDir); err != nil || !info.IsDir() {\n    return fmt.Errorf(\"cpuprofile dir %s missing or not a dir\", profDir)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use a known-writable directory like /tmp for -cpuprofile output.","mkdir -p the output directory before invoking gofmt in scripts.","Avoid paths under /proc, sysfs, or other special filesystems."],"tags":["gofmt","profiling","filesystem","permissions"],"analyzedSha":"b6b368adc57c96c3151d224d172029f233ead2c3","analyzedAt":"2026-08-12T00:22:02.250Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}