gohugoio/hugo · error
failed to create target directory for %q: %w
Error message
failed to create target directory for %q: %w
What it means
Still in the directory-archetype copy loop, Hugo creates the destination directory for a non-content file with MkdirAll before writing the copy. This error means MkdirAll on the source filesystem failed for a reason other than the directory already existing, wrapping the OS error.
Source
Thrown at create/content.go:196
for i, filename := range contentTargetFilenames {
if err := b.applyArcheType(filename, b.dirMap.contentFiles[i]); err != nil {
return err
}
}
// Copy the rest as is.
for _, fi := range b.dirMap.otherFiles {
meta := fi.Meta()
in, err := meta.Open()
if err != nil {
return fmt.Errorf("failed to open non-content file: %w", err)
}
targetFilename := filepath.Join(baseDir, b.targetPath, strings.TrimPrefix(fi.Meta().Filename, b.archetypeFi.Meta().Filename))
targetDir := filepath.Dir(targetFilename)
if err := b.sourceFs.MkdirAll(targetDir, 0o777); err != nil && !os.IsExist(err) {
return fmt.Errorf("failed to create target directory for %q: %w", targetDir, err)
}
out, err := b.sourceFs.Create(targetFilename)
if err != nil {
return err
}
_, err = io.Copy(out, in)
if err != nil {
return err
}
in.Close()
out.Close()
}
b.h.Log.Printf("Content dir %q created", filepath.Join(baseDir, b.targetPath))
View on GitHub ↗ (pinned to 8a468df065)
Solutions
- Fix ownership/permissions on the content directory tree (e.g. `chown -R $USER content`).
- Check no regular file exists at the target directory path shown in the error; rename or remove the conflicting file.
- Verify the volume is writable and has free space (common in Docker/CI mounts).
Defensive patterns
Strategy: try-catch
Validate before calling
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil { return err } Try / catch
if err := create.NewContent(...); err != nil {
if errors.Is(err, fs.ErrPermission) {
// content dir not writable — fix ownership/permissions
}
return err
} Prevention
- Verify write permissions on the content directory before generating content
- Avoid running builds as a different user than the one owning the site tree
- Watch for read-only mounts (Docker volumes, CI caches)
When it happens
Trigger: contentBuilder.buildDir calling b.sourceFs.MkdirAll(targetDir, 0o777) while copying archetype 'other' files into the new bundle under content/ — failing on permission denied, a read-only filesystem, a path component that exists as a regular file, or disk-full.
Common situations: content/ owned by another user (e.g. created by root/Docker) so the current user can't create subdirectories; read-only mounts in CI or containers; a file already existing where a directory is needed (name collision with the bundle path); exhausted disk or quota.
Related errors
- failed to open non-content file: %w
- failed to save file %q:: %w
- failed to save file %q: %s
- failed to create workingDir: %w
- no existing content directory configured for this project
AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31).
Data as JSON: /data/errors/db7c28bbe9dab6ef.json.
Report an issue: GitHub ↗.