gohugoio/hugo · error

failed to create target directory for

Error message

failed to create target directory for %q: %w

What it means

During the directory-archetype copy (create/content.go:195), Hugo does sourceFs.MkdirAll(targetDir, 0o777) for each copied file, ignoring only the 'already exists' case. This wraps any other MkdirAll failure (permission denied, read-only fs, disk full, invalid/over-long path).

Solutions

  1. Confirm write permission on the content root and all parent directories of the target.
  2. Free disk space / inodes if the volume is full.
  3. Shorten or simplify the target path to avoid FS limits.
  4. Run Hugo from a writable working directory rather than a read-only checkout.

Example fix

# before
$ hugo new posts/$(printf 'a%.0s' {1..300}).md  # path too long

# after
$ hugo new posts/short-title.md
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the target directory can be created before scaffolding.
func ValidateTargetDirWritable(fs afero.Fs, dir string) error {
    probe := filepath.Join(dir, ".hugo-write-probe")
    if err := fs.MkdirAll(dir, 0o777); err != nil && !os.IsExist(err) {
        return err
    }
    _ = fs.Remove(probe)
    return nil
}

Try / catch

if err := create.NewContent(h, kind, targetPath, force); err != nil {
    return err
}

Prevention

When it happens

Trigger: The resolved target directory cannot be created: content root on a read-only mount, no free disk space/inodes, a path segment not writable, invalid characters or excessive length in the path.

Common situations: CI checkout mounted read-only; running on a scratch container with a small tmpfs; path with spaces/unicode that the FS rejects; sandbox blocking writes outside the project.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/db7c28bbe9dab6ef. Report an issue: GitHub.

Appendix: 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 52c9bd7908)