{"record":{"id":"ce1e3210a7277e46","repo":"mikefarah/yq","slug":"failed-copying-from-v-to-v-w","errorCode":null,"errorMessage":"failed copying from %v to %v: %w","messagePattern":"failed copying from (.+?) to (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yqlib/file_utils.go","lineNumber":14,"sourceCode":"package yqlib\n\nimport (\n\t\"fmt\"\n\t\"io\"\n\t\"os\"\n)\n\nfunc tryRenameFile(from string, to string) error {\n\tif info, err := os.Lstat(to); err == nil && info.Mode()&os.ModeSymlink != 0 {\n\t\tlog.Debug(\"Target file is symlink, skipping rename and attempting to copy contents\")\n\n\t\tif copyError := copyFileContents(from, to); copyError != nil {\n\t\t\treturn fmt.Errorf(\"failed copying from %v to %v: %w\", from, to, copyError)\n\t\t}\n\t\ttryRemoveTempFile(from)\n\t\treturn nil\n\t} else if renameError := os.Rename(from, to); renameError != nil {\n\t\tlog.Debugf(\"Error renaming from %v to %v, attempting to copy contents\", from, to)\n\t\tlog.Debug(renameError.Error())\n\t\tlog.Debug(\"going to try copying instead\")\n\t\t// can't do this rename when running in docker to a file targeted in a mounted volume,\n\t\t// so gracefully degrade to copying the entire contents.\n\t\tif copyError := copyFileContents(from, to); copyError != nil {\n\t\t\treturn fmt.Errorf(\"failed copying from %v to %v: %w\", from, to, copyError)\n\t\t}\n\t\ttryRemoveTempFile(from)\n\t}\n\treturn nil\n}\n\nfunc tryRemoveTempFile(filename string) {","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/mikefarah/yq/blob/8b5af0694bb82b41d4ae180fac9972029066f90a/pkg/yqlib/file_utils.go#L1-L32","documentation":"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.","triggerScenarios":"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>'.","commonSituations":"In-place edits of files under symlinked config dirs (dotfiles repos), container volumes with permission mismatches, read-only bind mounts.","solutions":["Fix permissions on the symlink's target file (chmod/u+w) and retry","Write to stdout (`yq '.' file > out`) and move manually instead of -i","Resolve the symlink and edit the real file directly"],"exampleFix":"// before\nyq -i '.' ~/.config/link.yml   # fails: cannot copy into symlink target\n// after\nchmod u+w ~/.config/real.yml && yq -i '.' ~/.config/real.yml","handlingStrategy":"validation","validationCode":"target := \"~/.config/link.yml\"\nreal, _ := filepath.EvalSymlinks(target)\ninfo, err := os.Stat(real)\nif err != nil {\n    return err\n}\nif info.Mode().Perm()&0200 == 0 {\n    return fmt.Errorf(\"%s is not writable\", real)\n}","typeGuard":null,"tryCatchPattern":"err := runYqInPlace(file)\nif err != nil && strings.Contains(err.Error(), \"failed copying from\") {\n    // fall back to stdout redirect instead of in-place edit\n    return fallbackToStdoutEdit(file)\n}","preventionTips":["Resolve symlinks with readlink/realpath before using -i","Ensure the symlink target (not the link) has write permission","In containers, run as the file owner or fix UID/GID on mounted volumes"],"tags":["filesystem","in-place","symlink"],"backgroundTag":"in-place-write-copy-failed","analyzedSha":"8b5af0694bb82b41d4ae180fac9972029066f90a","analyzedAt":"2026-09-05T10:57:22.766Z","contentChangedAt":"2026-09-05T10:57:22.766Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}