gohugoio/hugo · error

failed to make target dir: %w

Error message

failed to make target dir: %w

What it means

In `hugo mod vendor` (modules/client.go:252), when a mount source is a single file rather than a directory, Hugo first creates the target's parent directory inside _vendor/ with MkdirAll(0o755). This error means that directory creation failed, so the file copy never starts.

Source

Thrown at modules/client.go:252

		dir := t.Dir()

		for _, mount := range t.Mounts() {
			sourceFilename := filepath.Join(dir, mount.Source)
			targetFilename := filepath.Join(vendorDir, t.PathVersionQuery(true), mount.Source)
			fi, err := c.fs.Stat(sourceFilename)
			if err != nil {
				return fmt.Errorf("failed to vendor module: %w", err)
			}

			if fi.IsDir() {
				if err := hugio.CopyDir(c.fs, sourceFilename, targetFilename, nil); err != nil {
					return fmt.Errorf("failed to copy module to vendor dir: %w", err)
				}
			} else {
				targetDir := filepath.Dir(targetFilename)

				if err := c.fs.MkdirAll(targetDir, 0o755); err != nil {
					return fmt.Errorf("failed to make target dir: %w", err)
				}

				if err := hugio.CopyFile(c.fs, sourceFilename, targetFilename); err != nil {
					return fmt.Errorf("failed to copy module file to vendor: %w", err)
				}
			}
		}

		// Include the resource cache if present.
		resourcesDir := filepath.Join(dir, files.FolderResources)
		_, err := c.fs.Stat(resourcesDir)
		if err == nil {
			if err := hugio.CopyDir(c.fs, resourcesDir, filepath.Join(vendorDir, t.PathVersionQuery(true), files.FolderResources), nil); err != nil {
				return fmt.Errorf("failed to copy resources to vendor dir: %w", err)
			}
		}

		// Include the config directory if present.

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Delete the existing _vendor directory and re-run hugo mod vendor to clear conflicting leftovers.
  2. Ensure the working directory is writable by the user running Hugo (fix ownership/ACLs, don't run in a read-only mount).
  3. Check free disk space and, on Windows, path-length limits.
  4. Inspect the wrapped error for the exact path that failed.
Defensive patterns

Strategy: try-catch

Validate before calling

// Verify the target parent directory can be created
if err := os.MkdirAll(filepath.Dir(targetPath), 0o755); err != nil {
    return fmt.Errorf("cannot create vendor target: %w", err)
}

Type guard

func isPermissionErr(err error) bool { return errors.Is(err, fs.ErrPermission) }

Try / catch

if err := client.Vendor(); err != nil {
    if errors.Is(err, fs.ErrPermission) {
        return fmt.Errorf("no write access to _vendor tree — fix ownership/permissions: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: c.fs.MkdirAll(filepath.Dir(targetFilename), 0o755) fails for a file-typed mount during vendoring — e.g. no write permission in the project, a path component in _vendor/ already exists as a regular file, or the filesystem is read-only/full.

Common situations: Running hugo mod vendor in a read-only checkout or container; a previous partial vendor left a file where a directory is now needed; overly long paths on Windows; restrictive umask/ACLs in CI.

Related errors


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