hashicorp/packer-plugin-scaffolding

Plugins don't have access to their own version numbers

Closed this issue · 4 comments

Attempting to import "github.com/hashicorp/packer-plugin-x" from any Builder, Provisioner, PostProcessor or Datasource will fail because it results in a circular import. Thus, it is impossible for code in a plugin to obtain it's own version number, if it follows this scaffold.

There should be a way for plugin code to obtain its own version through Prepare() or some other call.

Hey @serverwentdown, could you give an example of the circular import?
One option to access the version from within a package is by creating a version package and assigning the main version to it.
Here's an example: https://github.com/hashicorp/packer-plugin-ucloud/blob/main/main.go#L33
Does this help with your version case?

For example, in hashicorp/packer-plugin-amazon#82, I have a pending PR that does something similar to packer-plugin-ucloud, but instead I added a version/version.go file (following terraform-provider-aws) with the version number and imported it.

I think the scaffold should instead have this capability built-it.

Without creating version/version.go

builder/common/access_config.go

import (
  ...
  plugin "github.com/hashicorp/packer-plugin-amazon"
)

func Foo() {
  v := plugin.Version
  ...
}

The above import will result in a circular import.

With version/version.go

version/version.go

...
var Version = "0.0.2

builder/common/access_config.go

import (
  ...
  pluginversion "github.com/hashicorp/packer-plugin-amazon/version"
)

func Foo() {
  v := pluginversion.Version
  ...
}

Good that you shared https://github.com/hashicorp/packer-plugin-amazon/pull/82/files because I noticed the new version format breaks the versioning automation for the plugin release. That's why ucloud is made that way different from what you did on amazon. But that's ok, we can fix that.

We can either update the goreleaser to point to the correct package https://github.com/hashicorp/packer-plugin-scaffolding/blob/main/.goreleaser.yml#L39 or change the plugin to work like ucloud. I'll go for the first one and keep your changes.

hashicorp/packer-plugin-amazon#86 here's the fix for the amazon plugin.

I'll be super happy to review a PR against this scaffolding updating the version structure to be the same as the amazon plugin one. Feel free to do it if you want or we can also do it. 😃