{"record":{"id":"c166dc252e9d2b70","repo":"hashicorp/nomad","slug":"could-not-open-existing-file-v","errorCode":null,"errorMessage":"could not open existing file: %v","messagePattern":"could not open existing file: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"helper/escapingfs/copydir.go","lineNumber":39,"sourceCode":"\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tnewPath := filepath.Join(dst, oldPath)\n\t\tif d.IsDir() {\n\t\t\tinfo, err := d.Info()\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"could not stat directory: %v\", err)\n\t\t\t}\n\t\t\treturn os.MkdirAll(newPath, info.Mode())\n\t\t}\n\t\tif !d.Type().IsRegular() {\n\t\t\treturn fmt.Errorf(\"copying cannot traverse symlinks\")\n\t\t}\n\n\t\tr, err := srcFs.Open(oldPath)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"could not open existing file: %v\", err)\n\t\t}\n\t\tdefer r.Close()\n\t\tinfo, err := r.Stat()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"could not stat file: %v\", err)\n\t\t}\n\n\t\tw, err := os.OpenFile(newPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, info.Mode())\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\n\t\tif _, err := io.Copy(w, r); err != nil {\n\t\t\tw.Close()\n\t\t\treturn fmt.Errorf(\"could not copy file: %v\", err)\n\t\t}\n\t\treturn w.Close()\n\t})","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/helper/escapingfs/copydir.go#L21-L57","documentation":"During CopyDir, after confirming the entry is a regular file, the walker tries to open it via the os.DirFS handle. If opening fails (permission denied, file deleted between walk and open, I/O error), the walk aborts with this message wrapping the underlying error.","triggerScenarios":"CopyDir walking a regular file that cannot be opened: missing read permission, file removed/renamed between WalkDir listing and Open, or an OS-level I/O error (bad sector, NFS stale handle, disk full).","commonSituations":"Copying directories with mixed ownership/permissions (e.g. a chroot dir where some files are root-only); files deleted by a concurrent process during the copy; stale NFS or network mounts; running as a non-root user on directories with restrictive modes.","solutions":["Check and fix permissions on the source file so the process user can read it (`chmod`/`chown` or run with sufficient privileges)","Verify the file still exists; if a concurrent process deletes files, stop that process or snapshot/copy at a stable point","Check OS-level issues: dmesg/filesystem errors, stale mounts, disk health","If it is a permissions intent, copy as a user with read access rather than changing modes broadly"],"exampleFix":"// before\n$ ls -l job-dir/secret\n-rw------- 1 root root ... job-dir/secret\n// after (run copy as root or grant read)\n$ sudo chmod o+r job-dir/secret\n$ chown -R job:job job-dir/","handlingStrategy":"try-catch","validationCode":"info, err := os.Stat(path)\nif err != nil { return fmt.Errorf(\"file %s unreadable: %w\", path, err) }\nif info.Mode().Perm()&0400 == 0 { return fmt.Errorf(\"file %s not readable by process user\", path) }","typeGuard":null,"tryCatchPattern":"if err := escapingfs.CopyDir(src, dst); err != nil {\n    if strings.HasPrefix(err.Error(), \"could not open existing file\") {\n        // fall back: log path, check permissions, retry with elevated user or skip file\n        var pe *fs.PathError\n        if errors.As(err, &pe) { log.Warn(\"unreadable\", \"path\", pe.Path, \"err\", pe.Err) }\n    }\n    return err\n}","preventionTips":["Ensure the process user has read access to every file being copied (check ownership/modes of task dirs)","Avoid deleting or renaming files in the source directory while CopyDir is running","Check mount health (NFS/SMB) before large copy operations","Compare the error's wrapped PathError path to pinpoint the failing file"],"tags":["filesystem","permissions","io"],"backgroundTag":"file-open-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}