{"record":{"id":"af3e1ba654654182","repo":"hashicorp/packer","slug":"failed-to-wrap-script-contents-w","errorCode":null,"errorMessage":"failed to wrap script contents: %w","messagePattern":"failed to wrap script contents: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"provisioner/powershell/provisioner.go","lineNumber":317,"sourceCode":"}\n\n// Takes the inline scripts, adds a wrapper around the inline scripts, concatenates them into a temporary file and\n// returns a string containing the location of said file.\nfunc extractInlineScript(p *Provisioner) (string, error) {\n\ttemp, err := tmp.File(\"powershell-provisioner\")\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\tdefer temp.Close()\n\n\tvar commandBuilder strings.Builder\n\n\t// we concatenate all the inline commands\n\tfor _, command := range p.config.Inline {\n\t\tlog.Printf(\"Found command: %s\", command)\n\t\tif _, err := commandBuilder.WriteString(command + \"\\n\\t\"); err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to wrap script contents: %w\", err)\n\t\t}\n\t}\n\n\t// injecting all the variables in the string\n\tctxData := p.generatedData\n\tctxData[\"Vars\"] = p.createFlattenedEnvVars(p.config.ElevatedUser != \"\")\n\tctxData[\"Payload\"] = commandBuilder.String()\n\tctxData[\"DebugMode\"] = p.config.DebugMode\n\tp.config.ctx.Data = ctxData\n\n\tdata, err := interpolate.Render(wrapPowershellString, &p.config.ctx)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"Error building powershell wrapper: %w\", err)\n\t}\n\n\tlog.Printf(\"Writing PowerShell script to file: %s\", temp.Name())\n\tif _, err := temp.WriteString(data); err != nil {\n\t\treturn \"\", fmt.Errorf(\"Error writing PowerShell script: %w\", err)","sourceCodeStart":299,"sourceCodeEnd":335,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/provisioner/powershell/provisioner.go#L299-L335","documentation":"extractInlineScript concatenates all `inline` commands into a strings.Builder to build the payload for the PowerShell wrapper template. WriteString can only fail with an unrecoverable Builder panic per docs, but the code defensively wraps any returned error as \"failed to wrap script contents: %w\" and aborts rendering the wrapper script.","triggerScenarios":"extractInlineScript called from Provision with config.Inline entries whose concatenation triggers a strings.Builder.WriteString error (practically only from an out-of-memory/corrupted state; the code path exists as a defensive check).","commonSituations":"Very large inline script blocks causing memory pressure; this is extremely rare — most users will never see it.","solutions":["Retry the build; the condition is usually transient memory pressure.","Reduce the size of the inline script or move it to a file via `scripts`.","Report the issue with the underlying wrapped error if it persists."],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// Go, before Provision: cap inline payload size\nconst maxInline = 1 << 20 // 1 MiB\ntotal := 0\nfor _, c := range cfg.Inline {\n    total += len(c) + 1\n}\nif total > maxInline {\n    return fmt.Errorf(\"inline script too large (%d bytes); use scripts instead\", total)\n}","typeGuard":null,"tryCatchPattern":"// Go\nerr := prov.Provision(ctx, ui, comm, genData)\nif err != nil && strings.Contains(err.Error(), \"failed to wrap script contents\") {\n    // transient; retry once after freeing memory / reducing inline size\n}","preventionTips":["Keep inline scripts small; move long scripts to files via `scripts`.","Treat this as a defect and report to Packer if reproducible — strings.Builder.Write errors are practically impossible.","Log the wrapped cause (`%w`) to confirm the underlying condition."],"tags":["powershell","provisioner","inline-script","internal"],"backgroundTag":"string-builder-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"}