From e099c5c6617dd3ba36377eea085fcb249e7dfb2b Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Fri, 2 Feb 2024 10:02:40 -0500 Subject: [PATCH] main: move Discover to DetectPluginBinaries When Packer is loaded, we used to perform plugin discovery. This was done for every call to Packer, including when it is executed as a plugin, arguably against what the comments document. Doing this as early in the loading process makes it harder to change this behaviour, as we'd need to introduce flags aside from the rest, and handle them manually, which is not optimal. Therefore, we change this: now when Packer starts executing, it will not attempt to discover installed plugins anymore, and instead will only try to load them when a configuration has been parsed, and is being used to perform actions (typically build/validate). --- hcl2template/plugin.go | 11 +++++++++++ main.go | 7 ++++--- packer/core.go | 13 ++++++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/hcl2template/plugin.go b/hcl2template/plugin.go index 15358573d..6a66aa610 100644 --- a/hcl2template/plugin.go +++ b/hcl2template/plugin.go @@ -55,6 +55,17 @@ func (cfg *PackerConfig) PluginRequirements() (plugingetter.Requirements, hcl.Di } func (cfg *PackerConfig) DetectPluginBinaries() hcl.Diagnostics { + // Do first pass to discover all the installed plugins + err := cfg.parser.PluginConfig.Discover() + if err != nil { + return (hcl.Diagnostics{}).Append(&hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Failed to discover installed plugins", + Detail: err.Error(), + }) + } + + // Then we can apply any constraint from the template, if any opts := plugingetter.ListInstallationsOptions{ PluginDirectory: cfg.parser.PluginConfig.PluginDirectory, BinaryInstallationOptions: plugingetter.BinaryInstallationOptions{ diff --git a/main.go b/main.go index bb1d741d6..553c173f4 100644 --- a/main.go +++ b/main.go @@ -339,9 +339,10 @@ func loadConfig() (*config, error) { PluginMinPort: 10000, PluginMaxPort: 25000, PluginDirectory: pluginDir, - } - if err := config.Plugins.Discover(); err != nil { - return nil, err + Builders: packer.MapOfBuilder{}, + Provisioners: packer.MapOfProvisioner{}, + PostProcessors: packer.MapOfPostProcessor{}, + DataSources: packer.MapOfDatasource{}, } // Finally, try to use an internal plugin. Note that this will not override diff --git a/packer/core.go b/packer/core.go index 7a56dc06c..0f3ea9c9c 100644 --- a/packer/core.go +++ b/packer/core.go @@ -136,7 +136,18 @@ func NewCore(c *CoreConfig) *Core { // DetectPluginBinaries is used to load required plugins from the template, // since it is unsupported in JSON, this is essentially a no-op. func (c *Core) DetectPluginBinaries() hcl.Diagnostics { - return nil + var diags hcl.Diagnostics + + err := c.components.PluginConfig.Discover() + if err != nil { + diags = diags.Append(&hcl.Diagnostic{ + Severity: hcl.DiagError, + Summary: "Failed to discover installed plugins", + Detail: err.Error(), + }) + } + + return diags } func (c *Core) Initialize(_ InitializeOptions) hcl.Diagnostics {