From 123517aec1dead4a005345ad1c53f58350fe0ec7 Mon Sep 17 00:00:00 2001 From: Adrien Delorme Date: Fri, 5 Mar 2021 15:13:57 +0100 Subject: [PATCH] add warning with what to when calling init on an implicitly required plugin --- command/init.go | 32 ++++++++++++++++++++++++++++++-- hcl2template/plugin.go | 1 + packer/plugin-getter/plugins.go | 3 +++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/command/init.go b/command/init.go index eab38b648..de150ed59 100644 --- a/command/init.go +++ b/command/init.go @@ -125,9 +125,37 @@ func (c *InitCommand) RunContext(buildCtx context.Context, cla *InitArgs) int { ret = 1 } if newInstall != nil { - msg := fmt.Sprintf("Installed plugin %s %s in %q", pluginRequirement.Identifier, newInstall.Version, newInstall.BinaryPath) - ui.Say(msg) + if pluginRequirement.Implicit { + msg := fmt.Sprintf("Installed implicitly required plugin %s %s in %q", pluginRequirement.Identifier, newInstall.Version, newInstall.BinaryPath) + ui.Say(msg) + + warn := fmt.Sprintf(` +Warning, init with implicitly required plugin is always going to install the +latest possible plugin, if a latest version is backward incompatible with your +config file or your version of Packer, a build will fail. To avoid this, lock +the plugin version by pasting the following to your configuration: + +packer { + required_plugins { + %s = { + source = "%s" + version = "~> %s" + } + } +} +`, + pluginRequirement.Identifier.Type, + pluginRequirement.Identifier, + newInstall.Version, + ) + ui.Error(warn) + } else { + msg := fmt.Sprintf("Installed plugin %s %s in %q", pluginRequirement.Identifier, newInstall.Version, newInstall.BinaryPath) + ui.Say(msg) + } + } + } return ret } diff --git a/hcl2template/plugin.go b/hcl2template/plugin.go index b43abdeb5..7f5a5206d 100644 --- a/hcl2template/plugin.go +++ b/hcl2template/plugin.go @@ -41,6 +41,7 @@ func (cfg *PackerConfig) PluginRequirements() (plugingetter.Requirements, hcl.Di Accessor: name, Identifier: block.Type, VersionConstraints: block.Requirement.Required, + Implicit: block.PluginDependencyReason == PluginDependencyImplicit, }) uniq[name] = block } diff --git a/packer/plugin-getter/plugins.go b/packer/plugin-getter/plugins.go index 9595b67a0..94046c38d 100644 --- a/packer/plugin-getter/plugins.go +++ b/packer/plugin-getter/plugins.go @@ -38,6 +38,9 @@ type Requirement struct { // VersionConstraints as defined by user. Empty ( to be avoided ) means // highest found version. VersionConstraints version.Constraints + + // was this require implicitly guessed ? + Implicit bool } type BinaryInstallationOptions struct {