hashicorp/packer · error

could not create final plugin binary file: %w

Error message

could not create final plugin binary file: %w

What it means

After creating the destination plugin folder, InstallLatest opens the final plugin binary file (os.OpenFile(outputFileName, O_RDWR|O_CREATE|O_TRUNC, 0755)) to install the verified binary. Failure to open/create this file is wrapped as "could not create final plugin binary file: %w" and returned in the multierror, aborting the install of that plugin binary.

Source

Thrown at packer/plugin-getter/plugins.go:914

					if err := checkVersion(tmpBinFileName, pr.Identifier.String(), version); err != nil {
						errs = multierror.Append(errs, err)
						var continuableError *ContinuableInstallError
						if errors.As(err, &continuableError) {
							continue
						}
						return nil, errs
					}

					// create directories if need be
					if err := os.MkdirAll(outputFolder, 0755); err != nil {
						err := fmt.Errorf("could not create plugin folder %q: %w", outputFolder, err)
						errs = multierror.Append(errs, err)
						log.Printf("[TRACE] %s", err.Error())
						return nil, errs
					}
					outputFile, err := os.OpenFile(outputFileName, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
					if err != nil {
						err = fmt.Errorf("could not create final plugin binary file: %w", err)
						errs = multierror.Append(errs, err)
						return nil, errs
					}
					if _, err := outputFile.Write(outputFileData.Bytes()); err != nil {
						err = fmt.Errorf("could not write final plugin binary file: %w", err)
						errs = multierror.Append(errs, err)
						return nil, errs
					}
					outputFile.Close()

					cs, err = checksum.Checksummer.Sum(&outputFileData)
					if err != nil {
						err := fmt.Errorf("failed to checksum binary file: %s", err)
						errs = multierror.Append(errs, err)
						log.Printf("[WARNING] %v, ignoring", err)
					}
					if err := os.WriteFile(outputFileName+checksum.Checksummer.FileExt(), []byte(hex.EncodeToString(cs)), 0644); err != nil {
						err := fmt.Errorf("failed to write local binary checksum file: %s", err)

View on GitHub (pinned to eb36e3c3e4)

Solutions

  1. Check ownership of the existing binary (ls -l <outputFileName>) and chown/chmod, or remove it, so the current user can overwrite.
  2. Avoid mixing sudo and non-sudo Packer runs; reinstall the plugin as the regular user.
  3. If security software locks plugin executables, add an exclusion for the Packer plugin directory.
  4. Ensure the plugin volume is mounted read-write.
  5. Retry the install after fixing permissions; the wrapped OS error names the exact cause.

Example fix

// before: binary owned by root, cannot overwrite
sudo rm ~/.packer.d/plugins/github.com/hashicorp/consul/packer-plugin-consul_v1.0.0_x5.0_linux_amd64
// after: install succeeds
packer plugins install github.com/hashicorp/consul
Defensive patterns

Strategy: validation

Validate before calling

// before install: ensure any existing binary at the target can be overwritten
existing, err := os.Stat(targetBinaryPath)
if err == nil && !existing.IsDir() {
    if err := os.Chmod(targetBinaryPath, 0o755); err != nil {
        return fmt.Errorf("existing plugin binary not overwritable: %w", err)
    }
    // actual write-permission check
    f, err := os.OpenFile(targetBinaryPath, os.O_WRONLY, 0o755)
    if err != nil { return fmt.Errorf("cannot overwrite existing binary: %w", err) }
    f.Close()
}

Try / catch

err := getter.InstallLatest(ctx, req)
if err != nil && strings.Contains(err.Error(), "could not create final plugin binary file") {
    // drop the locked/owned-by-other-user binary and retry
    os.RemoveAll(existingPluginDir)
    return getter.InstallLatest(ctx, req)
}

Prevention

When it happens

Trigger: InstallLatest when os.OpenFile on the final output path fails — existing binary file not writable by the current user, outputFileName exists as a directory, or the parent folder was removed between MkdirAll and OpenFile.

Common situations: An existing plugin binary installed by root (sudo) that the current user cannot truncate; antivirus/EDR locking the executable; a directory unexpectedly named like the binary; read-only mounted plugin volume.

Related errors


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