{"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/8a468df065a75c1c7cf9f6850f32148746590ea5/commands/hugobuilder.go#L161-L197","documentation":"Returned by initCPUProfile when os.Create fails for the path given via the --profile-cpu flag (c.r.cpuprofile). Hugo wraps the underlying OS error so the user sees why the profile output file could not be created.","triggerScenarios":"Running hugo with --profile-cpu <path> where the path's directory does not exist, is not writable, or the path is a directory; wraps errors like ENOENT, EACCES, EISDIR from os.Create.","commonSituations":"Passing a profile path in a non-existent output directory, running in a read-only container filesystem, or pointing at a location owned by another user.","solutions":["Verify the parent directory of the --profile-cpu path exists and is writable; create it first.","Use a simple writable path like ./cpu.prof.","Check filesystem permissions / read-only mounts in CI or containers."],"exampleFix":"// before\nhugo --profile-cpu /nonexistent/dir/cpu.prof\n// after\nmkdir -p ./profiles && hugo --profile-cpu ./profiles/cpu.prof","handlingStrategy":"validation","validationCode":"// verify the profile output path is writable before enabling profiling\nf, err := os.Create(profilePath)\nif err != nil {\n    return fmt.Errorf(\"cpu profile path not writable: %w\", err)\n}\nf.Close()","typeGuard":"var pathErr *os.PathError\nif errors.As(err, &pathErr) {\n    // filesystem-level failure (permissions, missing dir)\n}","tryCatchPattern":"if err := b.build(); err != nil {\n    var pe *os.PathError\n    if errors.As(err, &pe) && strings.Contains(err.Error(), \"CPU profile\") {\n        // disable profiling flag or choose another path\n    }\n    return err\n}","preventionTips":["Only pass --profile-cpu with a path in an existing, writable directory","Create parent directories before enabling profiling","Run with profiling disabled in restricted/read-only environments (CI, containers)"],"tags":["hugo","profiling","filesystem","cli"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}