{"record":{"id":"24370873fb50d877","repo":"hashicorp/packer","slug":"error-preparing-shell-script-s-243708","errorCode":null,"errorMessage":"Error preparing shell script: %s","messagePattern":"Error preparing shell script: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"provisioner/windows-shell/provisioner.go","lineNumber":152,"sourceCode":"\t}\n\n\treturn errs\n}\n\n// This function takes the inline scripts, concatenates them\n// into a temporary file and returns a string containing the location\n// of said file.\nfunc extractScript(p *Provisioner) (string, error) {\n\ttemp, err := tmp.File(\"windows-shell-provisioner\")\n\tif err != nil {\n\t\tlog.Printf(\"Unable to create temporary file for inline scripts: %s\", err)\n\t\treturn \"\", err\n\t}\n\twriter := bufio.NewWriter(temp)\n\tfor _, command := range p.config.Inline {\n\t\tlog.Printf(\"Found command: %s\", command)\n\t\tif _, err := writer.WriteString(command + \"\\n\"); err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"Error preparing shell script: %s\", err)\n\t\t}\n\t}\n\n\tif err := writer.Flush(); err != nil {\n\t\treturn \"\", fmt.Errorf(\"Error preparing shell script: %s\", err)\n\t}\n\n\ttemp.Close()\n\n\treturn temp.Name(), nil\n}\n\nfunc (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packersdk.Communicator, generatedData map[string]interface{}) error {\n\tui.Say(\"Provisioning with windows-shell...\")\n\tscripts := make([]string, len(p.config.Scripts))\n\tcopy(scripts, p.config.Scripts)\n\tp.generatedData = generatedData\n","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/provisioner/windows-shell/provisioner.go#L134-L170","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// ensure temp dir is writable before building\nif f, err := os.CreateTemp(os.TempDir(), \"pk-test\"); err != nil {\n    log.Fatalf(\"temp dir not writable: %v\", err)\n} else { f.Close(); os.Remove(f.Name()) }","typeGuard":null,"tryCatchPattern":"if strings.Contains(buildErr, \"Error preparing shell script\") {\n    checkDiskFree(os.TempDir())\n    retryBuildWithTempDir(\"/var/tmp\")\n}","preventionTips":["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."],"tags":["provisioner","windows","filesystem","io-error"],"backgroundTag":"temp-file-write-failed","analyzedSha":"eb36e3c3e48a036f3e8cc94087636ee72e1303c9","analyzedAt":"2026-09-05T13:20:43.127Z","contentChangedAt":"2026-09-05T13:20:43.127Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}