{"record":{"id":"66164d8467f52799","repo":"hashicorp/nomad","slug":"failed-to-parse-policy-w","errorCode":null,"errorMessage":"failed to parse policy: %w","messagePattern":"failed to parse policy: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"acl/policy.go","lineNumber":755,"sourceCode":"\t\t}\n\t}()\n\n\tif err = hcl.Decode(p, rules); err != nil {\n\t\treturn err\n\t}\n\n\t// Manually parse the policy to fix blocks without labels.\n\t//\n\t// Due to a bug in the way HCL decodes files, a block without a label may\n\t// return an incorrect key value and make it impossible to determine if the\n\t// key was set by the user or incorrectly set by the decoder.\n\t//\n\t// By manually parsing the file we are able to determine if the label is\n\t// missing in the file and set them to an empty string so the policy\n\t// validation can return the appropriate errors.\n\troot, err := hcl.Parse(rules)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to parse policy: %w\", err)\n\t}\n\n\tlist, ok := root.Node.(*ast.ObjectList)\n\tif !ok {\n\t\treturn errors.New(\"error parsing: root should be an object\")\n\t}\n\n\tnsList := list.Filter(\"namespace\")\n\tfor i, nsObj := range nsList.Items {\n\t\t// Fix missing namespace key.\n\t\tif len(nsObj.Keys) == 0 {\n\t\t\tp.Namespaces[i].Name = \"\"\n\t\t}\n\t\tif i > 0 {\n\t\t\tp.removeExtraKey(\"namespace\")\n\t\t}\n\n\t\t// Fix missing variable paths.","sourceCodeStart":737,"sourceCodeEnd":773,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/acl/policy.go#L737-L773","documentation":"This error is returned when hcl.Parse fails on the raw ACL policy rules string in acl/policy.go. It wraps the underlying HCL syntax error with %w, so the root cause (line/column of the syntax error) is preserved. This happens before any semantic validation of the policy.","triggerScenarios":"Passing syntactically invalid HCL to acl policy parsing — unbalanced braces, missing quotes, invalid tokens, or non-HCL content in policy rules (e.g. from `consul acl policy create/update` with a bad -rules file, or agent config with malformed policy).","commonSituations":"Typos in HCL policy files; shell heredocs mangling quotes; JSON policy files accidentally saved with HCL syntax or vice versa; editors inserting smart quotes.","solutions":["Read the wrapped HCL error for the exact line/column and fix the syntax in the policy file.","Run the rules through an HCL linter or `hcltools`/validator before applying.","If the policy should be JSON, ensure it uses valid HCL-parseable syntax (HCL is a superset of JSON, so check braces/commas)."],"exampleFix":"// before (invalid HCL)\nkey_prefix \"foo\" {\n  policy = read\n// after\nkey_prefix \"foo\" {\n  policy = \"read\"\n}","handlingStrategy":"validation","validationCode":"// pre-parse the rules to catch HCL syntax errors early\nif _, err := hclparse.Parser{}.ParseHCL([]byte(rules), \"policy.hcl\"); err != nil {\n    return fmt.Errorf(\"policy has HCL syntax errors: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"// handle wrapped hcl errors with errors.As\n_, err := parsePolicy(rules)\nvar hclErr *hcl.Diagnostic\nif errors.As(err, &hclErr) {\n    log.Printf(\"fix policy syntax: %v\", hclErr)\n}","preventionTips":["Run HCL linting on policy files in CI.","Avoid generating HCL via string concatenation; use a templating library with escaping.","Check editor settings so quotes are not converted to smart quotes."],"tags":["acl","hcl","syntax-error","consul"],"backgroundTag":"hcl-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"}