{"record":{"id":"2989d35e3f3f3773","repo":"gohugoio/hugo","slug":"failed-to-create-cpu-profile-w","errorCode":null,"errorMessage":"failed to create CPU profile: %w","messagePattern":"failed to create CPU profile: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"commands/hugobuilder.go","lineNumber":179,"sourceCode":"\n// getDirList provides NewWatcher() with a list of directories to watch for changes.\nfunc (c *hugoBuilder) getDirList() ([]string, error) {\n\th, err := c.hugo()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn hstrings.UniqueStringsSorted(h.PathSpec.BaseFs.WatchFilenames()), nil\n}\n\nfunc (c *hugoBuilder) initCPUProfile() (func(), error) {\n\tif c.r.cpuprofile == \"\" {\n\t\treturn nil, nil\n\t}\n\n\tf, err := os.Create(c.r.cpuprofile)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to create CPU profile: %w\", err)\n\t}\n\tif err := pprof.StartCPUProfile(f); err != nil {\n\t\tf.Close()\n\t\treturn nil, fmt.Errorf(\"failed to start CPU profile: %w\", err)\n\t}\n\treturn func() {\n\t\tpprof.StopCPUProfile()\n\t\tf.Close()\n\t}, nil\n}\n\nfunc (c *hugoBuilder) initMemProfile() {\n\tif c.r.memprofile == \"\" {\n\t\treturn\n\t}\n\n\tf, err := os.Create(c.r.memprofile)\n\tif err != nil {","sourceCodeStart":161,"sourceCodeEnd":197,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/commands/hugobuilder.go#L161-L197","documentation":"Hugo's `hugoBuilder.initCPUProfile` calls `os.Create(c.r.cpuprofile)` to open the output file for a Go CPU profile (set via `--cpuprofile`). This error wraps the OS-level file creation failure. It fires before any profiling starts, so the process has not yet begun capturing samples.","triggerScenarios":"Invoked when `c.r.cpuprofile != \"\"` (the `--cpuprofile` flag was passed) and `os.Create` returns a non-nil error — e.g. a directory in the path does not exist, the path is read-only, or the user lacks write permission.","commonSituations":"Passing a relative `--cpuprofile=cpu.prof` from a working directory that is not writable; pointing the flag at an unwritable or non-existent directory like `/var/log/cpu.prof`; a typo in the path including a missing parent directory.","solutions":["Verify the parent directory exists and is writable, then re-run `hugo --cpuprofile=<abs/path/cpu.prof>`.","Use an absolute path under a directory you own (e.g. `~/cpu.prof`) instead of a bare filename.","Drop `--cpuprofile` entirely if you do not need profiling — `initCPUProfile` is a no-op when the flag is empty."],"exampleFix":"// before\n// $ hugo --cpuprofile=/var/run/cpu.prof\n// after\n// $ mkdir -p /tmp/hugoprof && hugo --cpuprofile=/tmp/hugoprof/cpu.prof","handlingStrategy":"validation","validationCode":"// Validate writability before launching Hugo with --cpuprofile.\nimport (\n  \"os\"\n  \"path/filepath\"\n)\nfunc validateProfilePath(p string) error {\n  dir := filepath.Dir(p)\n  info, err := os.Stat(dir)\n  if err != nil { return err }\n  if !info.IsDir() { return os.ErrNotExist }\n  // probe writability\n  f, err := os.CreateTemp(dir, \".hugocpu-*\")\n  if err != nil { return err }\n  f.Close(); os.Remove(f.Name())\n  return nil\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Always pass an absolute `--cpuprofile` path under a writable directory you own.","Pre-flight check that the parent directory exists before running `hugo --cpuprofile`.","Leave the flag unset during normal builds to skip profiling entirely."],"tags":["hugo","profiling","filesystem","go"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}