mikefarah/yq · error

failed copying from %v to %v: %w

Error message

failed copying from %v to %v: %w

What it means

tryRenameFile wraps copyFileContents failures when the output target is a symlink. yq writes to a temp file and normally renames it; for symlinked targets it copies contents instead, and if that copy fails (e.g. permission or I/O error) this error is returned from FinishWriteInPlace.

Source

Thrown at pkg/yqlib/file_utils.go:14

package yqlib

import (
	"fmt"
	"io"
	"os"
)

func tryRenameFile(from string, to string) error {
	if info, err := os.Lstat(to); err == nil && info.Mode()&os.ModeSymlink != 0 {
		log.Debug("Target file is symlink, skipping rename and attempting to copy contents")

		if copyError := copyFileContents(from, to); copyError != nil {
			return fmt.Errorf("failed copying from %v to %v: %w", from, to, copyError)
		}
		tryRemoveTempFile(from)
		return nil
	} else if renameError := os.Rename(from, to); renameError != nil {
		log.Debugf("Error renaming from %v to %v, attempting to copy contents", from, to)
		log.Debug(renameError.Error())
		log.Debug("going to try copying instead")
		// can't do this rename when running in docker to a file targeted in a mounted volume,
		// so gracefully degrade to copying the entire contents.
		if copyError := copyFileContents(from, to); copyError != nil {
			return fmt.Errorf("failed copying from %v to %v: %w", from, to, copyError)
		}
		tryRemoveTempFile(from)
	}
	return nil
}

func tryRemoveTempFile(filename string) {

View on GitHub (pinned to 8b5af0694b)

Solutions

  1. Fix permissions on the symlink's target file (chmod/u+w) and retry
  2. Write to stdout (`yq '.' file > out`) and move manually instead of -i
  3. Resolve the symlink and edit the real file directly

Example fix

// before
yq -i '.' ~/.config/link.yml   # fails: cannot copy into symlink target
// after
chmod u+w ~/.config/real.yml && yq -i '.' ~/.config/real.yml
Defensive patterns

Strategy: validation

Validate before calling

target := "~/.config/link.yml"
real, _ := filepath.EvalSymlinks(target)
info, err := os.Stat(real)
if err != nil {
    return err
}
if info.Mode().Perm()&0200 == 0 {
    return fmt.Errorf("%s is not writable", real)
}

Try / catch

err := runYqInPlace(file)
if err != nil && strings.Contains(err.Error(), "failed copying from") {
    // fall back to stdout redirect instead of in-place edit
    return fallbackToStdoutEdit(file)
}

Prevention

When it happens

Trigger: Running `yq -i '.' symlinked.yml` where the symlink target is unwritable, read-only, or on a failing filesystem — copyFileContents fails and gets wrapped as 'failed copying from <tmp> to <target>'.

Common situations: In-place edits of files under symlinked config dirs (dotfiles repos), container volumes with permission mismatches, read-only bind mounts.


AI-assisted analysis of mikefarah/yq@8b5af0694b (2026-09-05). Data as JSON: /api/errors/ce1e3210a7277e46. Report an issue: GitHub.