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).
This commit is contained in:
Lucas Bajolet 2024-02-02 10:02:40 -05:00 committed by Lucas Bajolet
parent 5bcf6dacca
commit e099c5c661
3 changed files with 27 additions and 4 deletions

View file

@ -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{

View file

@ -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

View file

@ -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 {