{"record":{"id":"4f801fe1c510e58f","repo":"hashicorp/nomad","slug":"unable-to-parse-var-file-v","errorCode":null,"errorMessage":"unable to parse var file: %v","messagePattern":"unable to parse var file: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"jobspec2/parse.go","lineNumber":140,"sourceCode":"\t// parsedVarFiles represent parsed HCL AST of the passed EnvVars\n\tparsedVarFiles []*hcl.File\n}\n\nfunc (c *ParseConfig) normalize() {\n\tif c.BaseDir == \"\" {\n\t\tc.BaseDir = filepath.Dir(c.Path)\n\t}\n}\n\nfunc decode(c *jobConfig) error {\n\tconfig := c.ParseConfig\n\n\tfile, diags := parseHCLOrJSON(config.Body, config.Path)\n\n\tfor _, varFile := range config.VarFiles {\n\t\tparsedVarFile, ds := parseFile(varFile)\n\t\tif parsedVarFile == nil || ds.HasErrors() {\n\t\t\treturn fmt.Errorf(\"unable to parse var file: %v\", ds.Error())\n\t\t}\n\n\t\tconfig.parsedVarFiles = append(config.parsedVarFiles, parsedVarFile)\n\t\tdiags = append(diags, ds...)\n\t}\n\n\tif config.VarContent != \"\" {\n\t\thclFile, hclDiagnostics := parseHCLOrJSON([]byte(config.VarContent), \"input.hcl\")\n\t\tif hclDiagnostics.HasErrors() {\n\t\t\treturn fmt.Errorf(\"unable to parse var content: %v\", hclDiagnostics.Error())\n\t\t}\n\t\tconfig.parsedVarFiles = append(config.parsedVarFiles, hclFile)\n\t}\n\n\t// Return early if the input job or variable files are not valid.\n\t// Decoding and evaluating invalid files may result in unexpected results.\n\tif diags.HasErrors() {\n\t\treturn diags","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/jobspec2/parse.go#L122-L158","documentation":"During variable decoding, each file in config.VarFiles is parsed as HCL or JSON. If parsing a var file yields no body or HCL diagnostics contain errors, decode aborts with 'unable to parse var file: <diagnostics>'. The wrapped message carries the actual HCL syntax/type error.","triggerScenarios":"Calling ParseWithConfig/parseWithConfigImpl where a VarFiles entry is unreadable, malformed HCL/JSON, empty, or of the wrong format so parseFile returns nil or diagnostics with errors.","commonSituations":"Typo or syntax error in a .hcl/.json var file; passing a YAML or TOML file; passing a directory or nonexistent path; file saved with wrong extension; BOM or encoding issues.","solutions":["Fix the syntax error reported in the wrapped HCL diagnostics for that var file.","Verify the file exists, is readable, and is valid HCL or JSON (validate with hclfmt or a JSON linter).","Convert non-HCL/JSON files (YAML, TOML, env-style) to HCL or JSON before use.","Re-encode the file as plain UTF-8 without BOM and retry."],"exampleFix":"// before (vars.yaml passed as var file)\n-var-file=vars.yaml\n\n// after (convert to valid HCL)\n// vars.hcl\nkey = \"value\"","handlingStrategy":"validation","validationCode":"for _, f := range varFiles {\n    data, err := os.ReadFile(f)\n    if err != nil { return fmt.Errorf(\"var file unreadable: %s: %w\", f, err) }\n    if err := hclwrite.Format(data); err != nil && json.Valid(data) == false {\n        return fmt.Errorf(\"var file is not valid HCL/JSON: %s\", f)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := ParseWithConfig(cfg); err != nil {\n    var pe *parseErr\n    if strings.HasPrefix(err.Error(), \"unable to parse var file\") {\n        // surface the wrapped diagnostics to the user\n    }\n}","preventionTips":["Run hclfmt on var files in CI.","Keep var files in HCL/JSON only; reject YAML/TOML early.","Check file existence/permissions before invoking the parser."],"tags":["hcl","parsing","jobspec","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"}