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

  1. Fix ownership/permissions on the content directory tree (e.g. `chown -R $USER content`).
  2. Check no regular file exists at the target directory path shown in the error; rename or remove the conflicting file.
  3. 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

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


AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31). Data as JSON: /data/errors/db7c28bbe9dab6ef.json. Report an issue: GitHub ↗.