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
- Check free disk space and write permissions in the system temp directory (TMPDIR on Unix, %TEMP% on Windows) where Packer creates the script.
- Clear space / fix the temp directory and re-run the build.
- If TMPDIR is customized, point it at a normal writable directory (e.g. TMPDIR=/tmp packer build ...).
- 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
- Monitor free space on the volume backing TMPDIR/%TEMP% in CI images.
- Avoid pointing TMPDIR at network or read-only filesystems.
- Keep inline command lists modest in size to limit temp usage.
- Clean stale temp files from CI runners regularly.
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
- Only one of script or scripts can be specified.
- Either a script file or inline script must be specified.
- could not write final plugin binary file: %w
- failed to get current working directory for Packer SBOM: %s
- failed to create temporary file in user SBOM directory %s: %
AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05).
Data as JSON: /api/errors/24370873fb50d877.
Report an issue: GitHub.