hashicorp/packer · error

failed to wrap script contents: %w

Error message

failed to wrap script contents: %w

What it means

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.

Source

Thrown at provisioner/powershell/provisioner.go:317

}

// Takes the inline scripts, adds a wrapper around the inline scripts, concatenates them into a temporary file and
// returns a string containing the location of said file.
func extractInlineScript(p *Provisioner) (string, error) {
	temp, err := tmp.File("powershell-provisioner")
	if err != nil {
		return "", err
	}

	defer temp.Close()

	var commandBuilder strings.Builder

	// we concatenate all the inline commands
	for _, command := range p.config.Inline {
		log.Printf("Found command: %s", command)
		if _, err := commandBuilder.WriteString(command + "\n\t"); err != nil {
			return "", fmt.Errorf("failed to wrap script contents: %w", err)
		}
	}

	// injecting all the variables in the string
	ctxData := p.generatedData
	ctxData["Vars"] = p.createFlattenedEnvVars(p.config.ElevatedUser != "")
	ctxData["Payload"] = commandBuilder.String()
	ctxData["DebugMode"] = p.config.DebugMode
	p.config.ctx.Data = ctxData

	data, err := interpolate.Render(wrapPowershellString, &p.config.ctx)
	if err != nil {
		return "", fmt.Errorf("Error building powershell wrapper: %w", err)
	}

	log.Printf("Writing PowerShell script to file: %s", temp.Name())
	if _, err := temp.WriteString(data); err != nil {
		return "", fmt.Errorf("Error writing PowerShell script: %w", err)

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Retry the build; the condition is usually transient memory pressure.
  2. Reduce the size of the inline script or move it to a file via `scripts`.
  3. Report the issue with the underlying wrapped error if it persists.
Defensive patterns

Strategy: retry

Validate before calling

// Go, before Provision: cap inline payload size
const maxInline = 1 << 20 // 1 MiB
total := 0
for _, c := range cfg.Inline {
    total += len(c) + 1
}
if total > maxInline {
    return fmt.Errorf("inline script too large (%d bytes); use scripts instead", total)
}

Try / catch

// Go
err := prov.Provision(ctx, ui, comm, genData)
if err != nil && strings.Contains(err.Error(), "failed to wrap script contents") {
    // transient; retry once after freeing memory / reducing inline size
}

Prevention

When it happens

Trigger: 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).

Common situations: Very large inline script blocks causing memory pressure; this is extremely rare — most users will never see it.

Related errors


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