hashicorp/packer · error

Error preparing shell script: %s

Error message

Error preparing shell script: %s

What it means

Returned by the windows-shell provisioner's extractScript while writing the `inline` commands into a temporary batch/PowerShell script on the host. When bufio.Writer.WriteString fails for one of the inline commands, the write error is wrapped as "Error preparing shell script: %s". This means the temp file backing the inline script could not be written, usually due to disk or file-descriptor problems.

Source

Thrown at provisioner/windows-shell/provisioner.go:152

	}

	return errs
}

// This function takes the inline scripts, concatenates them
// into a temporary file and returns a string containing the location
// of said file.
func extractScript(p *Provisioner) (string, error) {
	temp, err := tmp.File("windows-shell-provisioner")
	if err != nil {
		log.Printf("Unable to create temporary file for inline scripts: %s", err)
		return "", err
	}
	writer := bufio.NewWriter(temp)
	for _, command := range p.config.Inline {
		log.Printf("Found command: %s", command)
		if _, err := writer.WriteString(command + "\n"); err != nil {
			return "", fmt.Errorf("Error preparing shell script: %s", err)
		}
	}

	if err := writer.Flush(); err != nil {
		return "", fmt.Errorf("Error preparing shell script: %s", err)
	}

	temp.Close()

	return temp.Name(), nil
}

func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, generatedData map[string]interface{}) error {
	ui.Say("Provisioning with windows-shell...")
	scripts := make([]string, len(p.config.Scripts))
	copy(scripts, p.config.Scripts)
	p.generatedData = generatedData

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Check free disk space and write permissions in the system temp directory (TMPDIR on Unix, %TEMP% on Windows) where Packer creates the script.
  2. Clear space / fix the temp directory and re-run the build.
  3. If TMPDIR is customized, point it at a normal writable directory (e.g. TMPDIR=/tmp packer build ...).
  4. Retry the build if the failure was transient I/O; the wrapped original error in the message tells the exact OS cause.
Defensive patterns

Strategy: validation

Validate before calling

// ensure temp dir is writable before building
if f, err := os.CreateTemp(os.TempDir(), "pk-test"); err != nil {
    log.Fatalf("temp dir not writable: %v", err)
} else { f.Close(); os.Remove(f.Name()) }

Try / catch

if strings.Contains(buildErr, "Error preparing shell script") {
    checkDiskFree(os.TempDir())
    retryBuildWithTempDir("/var/tmp")
}

Prevention

When it happens

Trigger: Provision (or the extractScript unit test) calls extractScript with p.config.Inline set; the loop `for _, command := range p.config.Inline` writes each command plus "\n" to the temp file and WriteString returns an error (disk full, I/O error on the temp directory, closed/invalid temp file handle). provisioner.go:146-153.

Common situations: Host disk full or read-only filesystem where Packer creates temp files (TMPDIR/tmp); quota exceeded on a CI runner; exotic TMPDIR pointing at a device that rejects writes; temp file cleanup racing with the write.

Related errors


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