hashicorp/nomad · error

could not open existing file: %v

Error message

could not open existing file: %v

What it means

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.

Source

Thrown at helper/escapingfs/copydir.go:39

		if err != nil {
			return err
		}

		newPath := filepath.Join(dst, oldPath)
		if d.IsDir() {
			info, err := d.Info()
			if err != nil {
				return fmt.Errorf("could not stat directory: %v", err)
			}
			return os.MkdirAll(newPath, info.Mode())
		}
		if !d.Type().IsRegular() {
			return fmt.Errorf("copying cannot traverse symlinks")
		}

		r, err := srcFs.Open(oldPath)
		if err != nil {
			return fmt.Errorf("could not open existing file: %v", err)
		}
		defer r.Close()
		info, err := r.Stat()
		if err != nil {
			return fmt.Errorf("could not stat file: %v", err)
		}

		w, err := os.OpenFile(newPath, os.O_CREATE|os.O_EXCL|os.O_WRONLY, info.Mode())
		if err != nil {
			return err
		}

		if _, err := io.Copy(w, r); err != nil {
			w.Close()
			return fmt.Errorf("could not copy file: %v", err)
		}
		return w.Close()
	})

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Check and fix permissions on the source file so the process user can read it (`chmod`/`chown` or run with sufficient privileges)
  2. Verify the file still exists; if a concurrent process deletes files, stop that process or snapshot/copy at a stable point
  3. Check OS-level issues: dmesg/filesystem errors, stale mounts, disk health
  4. If it is a permissions intent, copy as a user with read access rather than changing modes broadly

Example fix

// before
$ ls -l job-dir/secret
-rw------- 1 root root ... job-dir/secret
// after (run copy as root or grant read)
$ sudo chmod o+r job-dir/secret
$ chown -R job:job job-dir/
Defensive patterns

Strategy: try-catch

Validate before calling

info, err := os.Stat(path)
if err != nil { return fmt.Errorf("file %s unreadable: %w", path, err) }
if info.Mode().Perm()&0400 == 0 { return fmt.Errorf("file %s not readable by process user", path) }

Try / catch

if err := escapingfs.CopyDir(src, dst); err != nil {
    if strings.HasPrefix(err.Error(), "could not open existing file") {
        // fall back: log path, check permissions, retry with elevated user or skip file
        var pe *fs.PathError
        if errors.As(err, &pe) { log.Warn("unreadable", "path", pe.Path, "err", pe.Err) }
    }
    return err
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "open() failed", "failed to open file", "cannot create file" — what a file open error means and how to fix it — this error's family across 42 libraries.

Related errors


AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04). Data as JSON: /api/errors/c166dc252e9d2b70. Report an issue: GitHub.