gohugoio/hugo · error

failed to copy module file to vendor: %w

Error message

failed to copy module file to vendor: %w

What it means

In `hugo mod vendor` (modules/client.go:256), after successfully creating the target directory, Hugo copies a file-typed mount source into _vendor/ with hugio.CopyFile. This error wraps any failure of that single-file copy.

Source

Thrown at modules/client.go:256

			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.
		configDir := filepath.Join(dir, "config")
		_, err = c.fs.Stat(configDir)
		if err == nil {
			if err := hugio.CopyDir(c.fs, configDir, filepath.Join(vendorDir, t.PathVersionQuery(true), "config"), nil); err != nil {

View on GitHub ↗ (pinned to 8a468df065)

Solutions

  1. Remove _vendor entirely and re-run hugo mod vendor to eliminate stale conflicting entries.
  2. Verify the source file is readable (check the wrapped error's path against the module cache).
  3. Ensure write permission and disk space in the project directory.
  4. Close programs that may hold locks on the files (Windows).
Defensive patterns

Strategy: try-catch

Validate before calling

// Check per-file readability for mounted files before vendoring
if f, err := os.Open(sourceFile); err != nil {
    return fmt.Errorf("mounted file unreadable: %w", err)
} else { f.Close() }

Try / catch

if err := client.Vendor(); err != nil {
    if strings.Contains(err.Error(), "failed to copy module file to vendor") {
        return fmt.Errorf("a single-file mount could not be copied — check the file exists in the module: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: hugio.CopyFile(c.fs, sourceFilename, targetFilename) fails for a mount whose source is a file — the source became unreadable between the earlier Stat and the copy, the target can't be created/written, or an I/O error occurs mid-copy.

Common situations: Permission problems on the source in the module cache or on the target under _vendor/; disk full during vendoring; file locked by another process (editor, indexer, AV) on Windows; a directory unexpectedly sitting at the target file path from an earlier partial vendor.

Related errors


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