hashicorp/nomad · error

error reading file: %w

Error message

error reading file: %w

What it means

A value beginning with '@' tells KVBuilder to read the value from the file named after '@' (e.g. -var key=@config.json). This error wraps the os.ReadFile failure when that file cannot be opened or read. The %w chain preserves the underlying OS error (not found, permission denied, etc.).

Source

Thrown at command/var.go:241

			if err != nil {
				return err
			}
			defer f.Close()

			return b.addReader(f)
		}
	}

	if len(parts) != 2 {
		return fmt.Errorf("format must be key=value")
	}
	key, value := parts[0], parts[1]

	if len(value) > 0 {
		if value[0] == '@' {
			contents, err := os.ReadFile(value[1:])
			if err != nil {
				return fmt.Errorf("error reading file: %w", err)
			}

			value = string(contents)
		} else if value[0] == '\\' && value[1] == '@' {
			value = value[1:]
		} else if value == "-" {
			if b.Stdin == nil {
				return fmt.Errorf("stdin is not supported")
			}
			if b.stdin {
				return fmt.Errorf("stdin already consumed")
			}
			b.stdin = true

			var buf bytes.Buffer
			if _, err := io.Copy(&buf, b.Stdin); err != nil {
				return err
			}

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Verify the file exists at the given path relative to the current working directory (ls the path, mind the leading @ is stripped before the path)
  2. Check file read permissions for the running user
  3. Use an absolute path to remove working-directory ambiguity
  4. Wrap the call and inspect errors.Is(err, fs.ErrNotExist) to distinguish missing vs permission problems

Example fix

// before
-var db_password=@secrets/passwd.txt  // file does not exist
// after
-var db_password=@/absolute/path/to/secrets/passwd.txt  // verified with ls
Defensive patterns

Strategy: validation

Validate before calling

path := strings.TrimPrefix(value, "@")
if fi, err := os.Stat(path); err != nil || fi.IsDir() {
	// resolve or fail fast before calling Add
}

Try / catch

if _, err := os.ReadFile("secrets/passwd.txt"); err != nil {
	if errors.Is(err, fs.ErrNotExist) { /* fix path */ }
	if errors.Is(err, fs.ErrPermission) { /* fix perms */ }
}

Prevention

When it happens

Trigger: Calling KVBuilder.Add("key=@path/to/file") where path/to/file does not exist, is a directory, or lacks read permission. On the CLI: -var key=@missing.json.

Common situations: Typos in file paths; running from a different working directory than expected; files not created yet by earlier pipeline steps; secrets files with restrictive permissions.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


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