{"record":{"id":"384a9318e2042633","repo":"hashicorp/nomad","slug":"failed-to-read-var-file-s-w","errorCode":null,"errorMessage":"Failed to read var file(s): %w","messagePattern":"Failed to read var file\\(s\\): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"jobspec2/parse.go","lineNumber":234,"sourceCode":"\t}\n\treturn false\n}\n\nconst (\n\tformatJSON = \"json\"\n\tformatHCL2 = \"hcl2\"\n)\n\nfunc submissionFromJob(args *ParseConfig, j *jobConfig) (*api.JobSubmission, error) {\n\tformat := formatHCL2\n\tif isJSON(args.Body) {\n\t\tformat = formatJSON\n\t}\n\n\t// combine any -var-file data into one big blob\n\tvarFileCat, readVarFileErr := extractVarFiles(args.VarFiles)\n\tif readVarFileErr != nil {\n\t\treturn nil, fmt.Errorf(\"Failed to read var file(s): %w\", readVarFileErr)\n\t}\n\n\tif varFileCat != \"\" && args.VarContent != \"\" {\n\t\tvarFileCat = strings.TrimRight(varFileCat, \"\\n\") + \"\\n\\n\" + args.VarContent\n\t} else if varFileCat == \"\" {\n\t\tvarFileCat = args.VarContent\n\t}\n\n\t// Extract variables declared by the -var flag and as environment\n\t// variables. Merge the two maps ensuring that variables defined by -var\n\t// flags take precedence over the environment\n\textractedVarFlags := extractVarFlags(args.ArgVars)\n\textractedEnvVars := extractJobSpecEnvVars(args.Envs)\n\tmaps.Copy(extractedEnvVars, extractedVarFlags)\n\n\t// Separate simple and complex variables from -var based on types from\n\t// schema. Simple types (string, number, bool) go to VariableFlags as\n\t// strings. Complex types (lists, maps, objects) go to Variables in HCL","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/jobspec2/parse.go#L216-L252","documentation":"submissionFromJob builds a job submission by extracting all -var-file contents into one concatenated blob via extractVarFiles. If reading/parsing any var file fails, the error is wrapped as 'Failed to read var file(s): %w' and returned to ParseWithConfigEx callers.","triggerScenarios":"Calling ParseWithConfigEx (or ParseWithConfig with submission conversion) where args.VarFiles contains a file that extractVarFiles cannot read or parse (missing, unreadable, invalid HCL/JSON).","commonSituations":"Submitting a job whose var files were deleted or moved; wrong paths in CI pipelines; permission changes on var files; var files that are valid YAML/TOML but not HCL/JSON.","solutions":["Check the wrapped error to identify which var file failed; verify its path and readability.","Fix syntax/format errors in the failing var file (must be HCL or JSON).","Ensure all var files are present and accessible to the submitting process (CI artifact checks, permissions).","Consolidate or inline variables if external files are fragile in your pipeline."],"exampleFix":"// before\nargs.VarFiles = [\"/stale/path/vars.hcl\"] // deleted file\n\n// after\nargs.VarFiles = [\"/current/path/vars.hcl\"] // verified exists & parses","handlingStrategy":"validation","validationCode":"for _, f := range args.VarFiles {\n    if fi, err := os.Stat(f); err != nil || fi.IsDir() {\n        return fmt.Errorf(\"var file missing or invalid: %s\", f)\n    }\n}","typeGuard":null,"tryCatchPattern":"job, err := ParseWithConfigEx(args)\nif err != nil && strings.Contains(err.Error(), \"Failed to read var file(s)\") {\n    // identify the failing file from the wrapped error and fix path/content\n}","preventionTips":["Verify var file paths exist in the submission environment (especially CI).","Check file permissions for the submitting user.","Vendor or inline variables when files are ephemeral."],"tags":["jobspec","parsing","files","variables"],"backgroundTag":"config-parse-error","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"}