hashicorp/terraform · error

Error parsing

Error message

Error parsing %s: %s

What it means

Thrown when hcl.Parse fails to parse the CLI configuration file content. This is an HCL syntax error in the .terraformrc file: the file was read successfully but contains invalid HCL syntax that the parser cannot accept.

Solutions

  1. Review the .terraformrc file for HCL syntax errors, paying attention to brace matching and quoting
  2. Validate the file with an HCL-aware linter or terraform's own error output which pinpoints the line
  3. Use JSON format (.tfrc.json) if you prefer JSON syntax and tooling
  4. Compare against the documented .terraformrc format in Terraform CLI docs

Example fix

// before (invalid HCL — missing closing quote)
credentials "app.terraform.io {
  token = "xxx"
}

// after
credentials "app.terraform.io" {
  token = "xxx"
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate HCL syntax of the config file before running terraform
func validateHCLConfig(path string) error {
    src, err := os.ReadFile(path)
    if err != nil {
        return err
    }
    parser := hclparse.NewParser()
    _, diags := parser.ParseHCL(src, path)
    if diags.HasErrors() {
        return diags
    }
    return nil
}

Prevention

When it happens

Trigger: hcl.Parse(string(d)) returns an error after the file bytes are read. Occurs when the .terraformrc file contains malformed HCL: unclosed blocks, invalid key-value syntax, missing quotes around strings, or stray characters.

Common situations: Hand-editing .terraformrc with syntax errors (unclosed braces, missing quotes); mixing JSON syntax into a .tfrc file (use .tfrc.json for JSON); copy-paste introducing invisible characters; outdated config format from very old Terraform versions.

Related errors


AI-assisted analysis of hashicorp/terraform@d32a084675 (2026-08-11). Data as JSON: /api/errors/93ec7cfb164bed72. Report an issue: GitHub.

Appendix: source

Thrown at internal/command/cliconfig/cliconfig.go:167

// loadConfigFile loads the CLI configuration from ".terraformrc" files.
func loadConfigFile(path string) (*Config, tfdiags.Diagnostics) {
	var diags tfdiags.Diagnostics
	result := &Config{}

	log.Printf("Loading CLI configuration from %s", path)

	// Read the HCL file and prepare for parsing
	d, err := ioutil.ReadFile(path)
	if err != nil {
		diags = diags.Append(fmt.Errorf("Error reading %s: %s", path, err))
		return result, diags
	}

	// Parse it
	obj, err := hcl.Parse(string(d))
	if err != nil {
		diags = diags.Append(fmt.Errorf("Error parsing %s: %s", path, err))
		return result, diags
	}

	// Build up the result
	if err := hcl.DecodeObject(&result, obj); err != nil {
		diags = diags.Append(fmt.Errorf("Error parsing %s: %s", path, err))
		return result, diags
	}

	// Deal with the provider_installation block, which is not handled using
	// DecodeObject because its structure is not compatible with the
	// limitations of that function.
	providerInstBlocks, moreDiags := decodeProviderInstallationFromConfig(obj)
	diags = diags.Append(moreDiags)
	result.ProviderInstallation = providerInstBlocks

	// Replace all env vars
	for k, v := range result.Providers {

View on GitHub (pinned to d32a084675)