hashicorp/nomad · error

error parsing hcl: %w

Error message

error parsing hcl: %w

What it means

When `-in=hcl` is used (or a .hcl spec file is detected), makeVariable hands the contents to parseVariableSpec, which parses HCL into the Variable structure. Any HCL syntax or schema error is wrapped as `error parsing hcl: <detail>`.

Source

Thrown at command/var_put.go:428

	var err error
	out := new(api.Variable)
	if len(c.contents) == 0 {
		out.Path = path
		out.Namespace = c.Meta.namespace
		out.Items = make(map[string]string)
		return out, nil
	}

	switch c.inFmt {
	case "json":
		err = json.Unmarshal(c.contents, out)
		if err != nil {
			return nil, fmt.Errorf("error unmarshaling json: %w", err)
		}
	case "hcl":
		out, err = parseVariableSpec(c.contents, c.verbose)
		if err != nil {
			return nil, fmt.Errorf("error parsing hcl: %w", err)
		}
	case "":
		return nil, errors.New("format flag required")
	default:
		return nil, fmt.Errorf("unknown format flag value")
	}

	// It is possible a specification file was used which did not declare any
	// items. Therefore, default the entry to avoid panics and ensure this type
	// of use is valid.
	if out.Items == nil {
		out.Items = make(map[string]string)
	}

	// Handle cases where values are provided by CLI flags that modify the
	// the created variable. Typical of a "copy" operation, it is a convenience
	// to reset the Create and Modify metadata to zero.
	var resetIndex bool

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Fix the HCL syntax reported in the wrapped error message (line/col detail is included by the HCL parser).
  2. Run `nomad var init` to get a known-good .hcl template and match its structure.
  3. Verify the flag matches the content: `-in=hcl` for HCL, `-in=json` for JSON.
  4. Use `hclfmt` or an editor HCL plugin to catch syntax problems before submitting.

Example fix

// before (missing '=' in key)
items { key "value" }
// after
items { key = "value" }
Defensive patterns

Strategy: validation

Validate before calling

# lint the HCL spec before use
hclfmt -check spec.hcl || { echo 'invalid HCL' >&2; exit 1; }

Try / catch

if err := run(); err != nil && strings.Contains(err.Error(), "error parsing hcl:") {
    // HCL diagnostics include file:line,col — surface them verbatim to the user
    log.Fatalf("fix spec.hcl: %v", err)
}

Prevention

When it happens

Trigger: `nomad var put ... -in=hcl @file` where the file has HCL syntax errors (unclosed braces, missing `=`), or the top level is not an object/list, or nested item values are of unsupported types.

Common situations: Editing the .hcl spec by hand and breaking block syntax; using JSON syntax inside a .hcl file; mixing `-in=json` with HCL content so the JSON parser (1412) or vice versa; older Nomad versions with stricter HCL parsing.

Related errors


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