hashicorp/nomad · error

Failed to read var file(s): %w

Error message

Failed to read var file(s): %w

What it means

submissionFromJob builds a job submission by extracting all -var-file contents into one concatenated blob via extractVarFiles. If reading/parsing any var file fails, the error is wrapped as 'Failed to read var file(s): %w' and returned to ParseWithConfigEx callers.

Source

Thrown at jobspec2/parse.go:234

	}
	return false
}

const (
	formatJSON = "json"
	formatHCL2 = "hcl2"
)

func submissionFromJob(args *ParseConfig, j *jobConfig) (*api.JobSubmission, error) {
	format := formatHCL2
	if isJSON(args.Body) {
		format = formatJSON
	}

	// combine any -var-file data into one big blob
	varFileCat, readVarFileErr := extractVarFiles(args.VarFiles)
	if readVarFileErr != nil {
		return nil, fmt.Errorf("Failed to read var file(s): %w", readVarFileErr)
	}

	if varFileCat != "" && args.VarContent != "" {
		varFileCat = strings.TrimRight(varFileCat, "\n") + "\n\n" + args.VarContent
	} else if varFileCat == "" {
		varFileCat = args.VarContent
	}

	// Extract variables declared by the -var flag and as environment
	// variables. Merge the two maps ensuring that variables defined by -var
	// flags take precedence over the environment
	extractedVarFlags := extractVarFlags(args.ArgVars)
	extractedEnvVars := extractJobSpecEnvVars(args.Envs)
	maps.Copy(extractedEnvVars, extractedVarFlags)

	// Separate simple and complex variables from -var based on types from
	// schema. Simple types (string, number, bool) go to VariableFlags as
	// strings. Complex types (lists, maps, objects) go to Variables in HCL

View on GitHub (pinned to 482b49bf1a)

Solutions

  1. Check the wrapped error to identify which var file failed; verify its path and readability.
  2. Fix syntax/format errors in the failing var file (must be HCL or JSON).
  3. Ensure all var files are present and accessible to the submitting process (CI artifact checks, permissions).
  4. Consolidate or inline variables if external files are fragile in your pipeline.

Example fix

// before
args.VarFiles = ["/stale/path/vars.hcl"] // deleted file

// after
args.VarFiles = ["/current/path/vars.hcl"] // verified exists & parses
Defensive patterns

Strategy: validation

Validate before calling

for _, f := range args.VarFiles {
    if fi, err := os.Stat(f); err != nil || fi.IsDir() {
        return fmt.Errorf("var file missing or invalid: %s", f)
    }
}

Try / catch

job, err := ParseWithConfigEx(args)
if err != nil && strings.Contains(err.Error(), "Failed to read var file(s)") {
    // identify the failing file from the wrapped error and fix path/content
}

Prevention

When it happens

Trigger: Calling ParseWithConfigEx (or ParseWithConfig with submission conversion) where args.VarFiles contains a file that extractVarFiles cannot read or parse (missing, unreadable, invalid HCL/JSON).

Common situations: Submitting a job whose var files were deleted or moved; wrong paths in CI pipelines; permission changes on var files; var files that are valid YAML/TOML but not HCL/JSON.

Related errors


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