From 586762564baa2d2915e6ced578d92e2fa8033c24 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Fri, 30 Aug 2024 14:13:51 -0400 Subject: [PATCH] hcl2template: intro and add UseSequential init opt Following up on the DAG work, this commit adds a new option for initialisation that disables DAG on request. By default we are going to use the DAG approach, with an option to fallback to using the older algorithm for evaluation in case users end-up in an edge-case that prevents them from building a template. --- hcl2template/parser.go | 9 +++++++-- packer/run_interfaces.go | 8 ++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/hcl2template/parser.go b/hcl2template/parser.go index 1c3430dea..82372bac1 100644 --- a/hcl2template/parser.go +++ b/hcl2template/parser.go @@ -474,8 +474,13 @@ func (cfg *PackerConfig) evaluateBuildPrereqs(skipDatasources bool) hcl.Diagnost func (cfg *PackerConfig) Initialize(opts packer.InitializeOptions) hcl.Diagnostics { diags := cfg.InputVariables.ValidateValues() - diags = append(diags, cfg.evaluateDatasources(opts.SkipDatasourcesExecution)...) - diags = append(diags, cfg.evaluateLocalVariables(cfg.LocalBlocks)...) + + if opts.UseSequential { + diags = diags.Extend(cfg.evaluateDatasources(opts.SkipDatasourcesExecution)) + diags = diags.Extend(cfg.evaluateLocalVariables(cfg.LocalBlocks)) + } else { + diags = diags.Extend(cfg.evaluateBuildPrereqs(opts.SkipDatasourcesExecution)) + } filterVarsFromLogs(cfg.InputVariables) filterVarsFromLogs(cfg.LocalVariables) diff --git a/packer/run_interfaces.go b/packer/run_interfaces.go index 07829e629..3b7170899 100644 --- a/packer/run_interfaces.go +++ b/packer/run_interfaces.go @@ -38,6 +38,14 @@ type InitializeOptions struct { // When set, the execution of datasources will be skipped and the datasource will provide // an output spec that will be used for validation only. SkipDatasourcesExecution bool + // UseSequential changes the way data sources and locals are evaluated. + // + // In this mode, instead of using two separate phases to evaluate datasources first, then + // local variables, here we instead use a DAG so both are evaluated at once, based on the + // dependencies between them. + // + // This is optional and defaults to false for now, but this may become a default later. + UseSequential bool } type PluginBinaryDetector interface {