hashicorp/nomad · error

failed to parse HCL file %s: %w

Error message

failed to parse HCL file %s: %w

What it means

After decoding, ParseConfigFile re-parses the file with hcl.Parse to manually extract multiple 'vault' blocks (which lack labels). If the HCL text itself has a syntax error, hcl.Parse fails and the error is wrapped as 'failed to parse HCL file <path>'.

Source

Thrown at command/agent/config_parse.go:81

		RPC:       &RPCConfig{},
		Audit:     &config.AuditConfig{},
		Consuls:   []*config.ConsulConfig{},
		Autopilot: &config.AutopilotConfig{},
		Telemetry: &Telemetry{},
		Vaults:    []*config.VaultConfig{},
		Reporting: config.DefaultReporting(),
	}

	err = hcl.Decode(c, buf.String())
	if err != nil {
		return nil, fmt.Errorf("failed to decode HCL file %s: %w", path, err)
	}

	// Re-parse the file to extract the multiple Vault configurations, which we
	// need to parse by hand because we don't have a label on the block
	root, err := hcl.Parse(buf.String())
	if err != nil {
		return nil, fmt.Errorf("failed to parse HCL file %s: %w", path, err)
	}
	list, ok := root.Node.(*ast.ObjectList)
	if !ok {
		return nil, fmt.Errorf("error parsing: root should be an object")
	}
	matches := list.Filter("vault")
	if len(matches.Items) > 0 {
		if err := parseVaults(c, matches); err != nil {
			return nil, fmt.Errorf("error parsing 'vault': %w", err)
		}
	}
	matches = list.Filter("consul")
	if len(matches.Items) > 0 {
		if err := parseConsuls(c, matches); err != nil {
			return nil, fmt.Errorf("error parsing 'consul': %w", err)
		}
	}

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Run hclfmt (or hcl2 linter) on the file — it reports the syntax error location
  2. Check for unbalanced braces/brackets and missing commas around the reported line
  3. Inspect the file for merge-conflict markers or truncation from failed deploys
  4. Regenerate the file from a known-good template

Example fix

// before
service {
  name = "web"
// missing closing brace
// after
service {
  name = "web"
}
Defensive patterns

Strategy: validation

Validate before calling

src, err := os.ReadFile(path)
if err != nil { return err }
if _, err := hcl.Parse(string(src)); err != nil {
    return fmt.Errorf("HCL syntax pre-check failed for %s: %w", path, err)
}

Try / catch

if _, err := hcl.Parse(string(src)); err != nil {
    if pos := errPos(err); pos != nil {
        return fmt.Errorf("syntax error at line %d: %w", pos.Line, err)
    }
    return err
}

Prevention

When it happens

Trigger: Any HCL syntax error in the config file: unbalanced braces/brackets, missing commas or newlines between assignments, invalid tokens, or truncated files.

Common situations: Hand-edited .hcl files with unbalanced braces, merge conflicts left in config files, templating that emitted invalid HCL, or copy-paste dropping closing braces.

Understand the failure class

Related errors


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