hashicorp/packer · error

failed to read %s: %s

Error message

failed to read %s: %s

What it means

After opening the file, processFile reads it fully via io.ReadAll; an I/O failure at this stage (disk errors, reading a directory handle, device errors) yields 'failed to read %s: %s' wrapping the OS error.

Source

Thrown at hcl2template/formatter.go:139

		return nil, fmt.Errorf("file %s is not a HCL file", filename)
	}

	var in io.Reader
	var err error

	if filename == "-" {
		in = os.Stdin
		f.ShowDiff = false
	} else {
		in, err = os.Open(filename)
		if err != nil {
			return nil, fmt.Errorf("failed to open %s: %s", filename, err)
		}
	}

	inSrc, err := io.ReadAll(in)
	if err != nil {
		return nil, fmt.Errorf("failed to read %s: %s", filename, err)
	}

	_, diags := f.parser.ParseHCL(inSrc, filename)
	if diags.HasErrors() {
		return nil, multierror.Append(nil, diags.Errs()...)
	}

	outSrc := hclwrite.Format(inSrc)

	if bytes.Equal(inSrc, outSrc) {
		if filename == "-" {
			_, _ = f.Output.Write(outSrc)
		}

		return nil, nil
	}

	if filename != "-" {

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Ensure the target is a regular file, not a directory or special file
  2. Check the filesystem/mount is healthy and reachable
  3. Re-run to rule out a transient I/O error and inspect the wrapped OS error text

Example fix

// before
packer fmt /proc/self/cmdline
// after
packer fmt ./templates/build.pkr.hcl
Defensive patterns

Strategy: try-catch

Validate before calling

info, err := os.Stat(path)
// require err == nil && info.Mode().IsRegular()

Try / catch

if err := f.FormatFile(path); err != nil {
  if strings.Contains(err.Error(), "failed to read") { /* retry or report I/O issue */ }
}

Prevention

When it happens

Trigger: io.ReadAll failing inside processFile after a successful os.Open — typically reading a directory or special file that opened without error, or hardware/permission issues mid-read.

Common situations: Reading special files (/proc entries, device files) accidentally matched by globs; directories passed as paths on platforms where open succeeds; failing disks or network mounts dropping mid-read.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/ed51769fb1477c19. Report an issue: GitHub.