Plugin not being loaded via PACKER_PLUGIN_PATH from root

Hey,

I am just following the documentation and build a super simple “hello world” plugin for packer. Here is the codebase: GitHub - lzap/packer-plugin-image-builder: WIP version of the new ibpacker plugin

The plugin itself is in a subfolder cmd/plugin: packer-plugin-image-builder/cmd/plugin/plugin.go at main · lzap/packer-plugin-image-builder · GitHub

All the builder does is just print a message for now.

To build the binary I do:

make build

The plugin describe works fine:

$ ./packer-plugin-image-builder describe | json_reformat 
{
    "version": "0.0.0",
    "sdk_version": "0.5.2",
    "api_version": "x5.0",
    "builders": [
        "-packer-default-plugin-name-"
    ],
    "post_processors": [

    ],
    "provisioners": [

    ],
    "datasources": [

    ]
}

The problem is, when I try to build the following example, it does not load the plugin for some reason and I am not sure how to debug this:

source "image-builder" "example" {
}

build {
    sources = [ "source.image-builder.example" ]
}

The command I use is executed from the project folder where the plugin binary is present:

$ PACKER_PLUGIN_PATH=$PWD packer build empty.pkr.hcl
Error: Unknown source type image-builder

  on empty.pkr.hcl line 4:
  (source code not available)

The source image-builder is unknown by Packer, and is likely part of a plugin
that is not installed.
You may find the needed plugin along with installation instructions documented
on the Packer integrations page.

https://developer.hashicorp.com/packer/integrations?filter=image

I am on SDK 0.5.2 because of SDK issue no 187 and the OS is Fedora 41 with the official Packer build version Packer v1.11.2. Thanks for help.

Logs:

2025/01/28 08:46:45 [INFO] Packer version: 1.11.2 [go1.21.12 linux amd64]
2025/01/28 08:46:45 [INFO] PACKER_CONFIG env var not set; checking the default config file path
2025/01/28 08:46:45 [INFO] PACKER_CONFIG env var set; attempting to open config file: /home/lzap/.packerconfig
2025/01/28 08:46:45 [WARN] Config file doesn't exist: /home/lzap/.packerconfig
2025/01/28 08:46:45 [INFO] Setting cache directory: /home/lzap/.cache/packer
2025/01/28 08:46:45 [TRACE] listing potential installations for <nil> that match "". plugingetter.ListInstallationsOptions{PluginDirectory:"/home/lzap/packer-plugin-image-builder", BinaryInstallationOptions:plugingetter.BinaryInstallationOptions{APIVersionMajor:"5", APIVersionMinor:"0", OS:"linux", ARCH:"amd64", Ext:"", Checksummers:[]plugingetter.Checksummer{plugingetter.Checksummer{Type:"sha256", Hash:(*sha256.digest)(0xc0008dc080)}}, ReleasesOnly:false}}
2025/01/28 08:46:45 ui error: Error: Unknown source type image-builder

When I install the plugin it works, packer is just not picking it up from the PWD for some reason. I double checked it is executable, I can run it, it reports metadata just fine.

I did research the code logic around loading plugins and it seems that the statement in docs that one can simply load plugins from a current directory by overriding PACKER_PLUGIN_PATH is not correct.

If you want to load a custom build of a plugin the following conditions must match:

  • Plugin name must now contain the API version, OS architecture and name and SHA sum.
  • There must be a SHASUM file next to the plugin file with a suffix
  • Must not be in a root directory that was passed via PACKER_PLUGIN_PATH

Here is an example of a Makefile that builds a plugin and prepares the environment in a way that one can execute it via make start:

NAME=image-builder
ROOT_DIR:=$(dir $(realpath $(lastword $(MAKEFILE_LIST))))
BUILD_DIR=build
PLUGIN_DIR=${BUILD_DIR}/plugins
BINARY=packer-plugin-${NAME}_v0.0.0_x5.0_linux_amd64
# https://github.com/hashicorp/packer-plugin-sdk/issues/187
HASHICORP_PACKER_PLUGIN_SDK_VERSION?="v0.5.2"
PLUGIN_FQN=$(shell grep -E '^module' <go.mod | sed -E 's/module \s*//')
PLUGIN_PATH=./cmd/plugin

.PHONY: build
build:
	@mkdir -p ${PLUGIN_DIR}
	@go build -ldflags="-X '${PLUGIN_FQN}/main.Version=$(shell git describe --tags --abbrev=0)'" -o ${PLUGIN_DIR}/${BINARY} ${PLUGIN_PATH}
	@sha256sum < ${PLUGIN_DIR}/${BINARY} > ${PLUGIN_DIR}/${BINARY}_SHA256SUM

.PHONY: clean
clean:
	@rm -rf ${BUILD_DIR}

.PHONY: start
start: build
	PACKER_PLUGIN_PATH=${ROOT_DIR}${BUILD_DIR} PACKER_LOG=1 PACKER_LOG_PATH=packer.log packer build empty.pkr.hcl

The important trick is to create subdirectory ./build/plugins and build the executable there into correct form with underscores and then creating SHA256SUM suffixed file as well. Only then packer appears to load it.