{"record":{"id":"5657b511df590acf","repo":"hashicorp/nomad","slug":"error-reading-file-w","errorCode":null,"errorMessage":"error reading file: %w","messagePattern":"error reading file: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/var.go","lineNumber":241,"sourceCode":"\t\t\tif err != nil {\n\t\t\t\treturn err\n\t\t\t}\n\t\t\tdefer f.Close()\n\n\t\t\treturn b.addReader(f)\n\t\t}\n\t}\n\n\tif len(parts) != 2 {\n\t\treturn fmt.Errorf(\"format must be key=value\")\n\t}\n\tkey, value := parts[0], parts[1]\n\n\tif len(value) > 0 {\n\t\tif value[0] == '@' {\n\t\t\tcontents, err := os.ReadFile(value[1:])\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"error reading file: %w\", err)\n\t\t\t}\n\n\t\t\tvalue = string(contents)\n\t\t} else if value[0] == '\\\\' && value[1] == '@' {\n\t\t\tvalue = value[1:]\n\t\t} else if value == \"-\" {\n\t\t\tif b.Stdin == nil {\n\t\t\t\treturn fmt.Errorf(\"stdin is not supported\")\n\t\t\t}\n\t\t\tif b.stdin {\n\t\t\t\treturn fmt.Errorf(\"stdin already consumed\")\n\t\t\t}\n\t\t\tb.stdin = true\n\n\t\t\tvar buf bytes.Buffer\n\t\t\tif _, err := io.Copy(&buf, b.Stdin); err != nil {\n\t\t\t\treturn err\n\t\t\t}","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/var.go#L223-L259","documentation":"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.).","triggerScenarios":"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.","commonSituations":"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.","solutions":["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)","Check file read permissions for the running user","Use an absolute path to remove working-directory ambiguity","Wrap the call and inspect errors.Is(err, fs.ErrNotExist) to distinguish missing vs permission problems"],"exampleFix":"// before\n-var db_password=@secrets/passwd.txt  // file does not exist\n// after\n-var db_password=@/absolute/path/to/secrets/passwd.txt  // verified with ls","handlingStrategy":"validation","validationCode":"path := strings.TrimPrefix(value, \"@\")\nif fi, err := os.Stat(path); err != nil || fi.IsDir() {\n\t// resolve or fail fast before calling Add\n}","typeGuard":null,"tryCatchPattern":"if _, err := os.ReadFile(\"secrets/passwd.txt\"); err != nil {\n\tif errors.Is(err, fs.ErrNotExist) { /* fix path */ }\n\tif errors.Is(err, fs.ErrPermission) { /* fix perms */ }\n}","preventionTips":["Verify @file paths exist (and are files, not dirs) before the command runs","Prefer absolute paths in scripts and CI","Check file permissions for the running user"],"tags":["cli","file-io","argument-parsing"],"backgroundTag":"file-read-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}