From 09774aaeb825b07ff752a389d6fb94c7a2eb66a6 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Tue, 3 Sep 2024 15:10:17 -0400 Subject: [PATCH] internal/dag: adapt Validate to not check for Root The implementation of the DAG as extracted from Terraform relied on a Root vertex being injected into the graph as the last node to visit. This is used as a sanity check for Terraform, but doesn't apply to our use-case for now, as we are always executing everything and have no need for this root node. Instead, we change how Validate operates so it does not error in case there is no valid root node for the graph, but enables us calling it to check for self-referencing edges, and circular dependencies. --- hcl2template/parser.go | 4 ++++ internal/dag/dag.go | 7 +------ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/hcl2template/parser.go b/hcl2template/parser.go index 82372bac1..a11c5dd15 100644 --- a/hcl2template/parser.go +++ b/hcl2template/parser.go @@ -420,6 +420,10 @@ func (cfg *PackerConfig) buildPrereqsDAG() (*dag.AcyclicGraph, error) { } } + if err := retGraph.Validate(); err != nil { + return nil, err + } + return &retGraph, nil } diff --git a/internal/dag/dag.go b/internal/dag/dag.go index 118d4bf1b..e212135a8 100644 --- a/internal/dag/dag.go +++ b/internal/dag/dag.go @@ -127,13 +127,8 @@ func (g *AcyclicGraph) TransitiveReduction() { } } -// Validate validates the DAG. A DAG is valid if it has a single root -// with no cycles. +// Validate validates the DAG. A DAG is valid if it has no cycles or self-referencing vertex. func (g *AcyclicGraph) Validate() error { - if _, err := g.Root(); err != nil { - return err - } - // Look for cycles of more than 1 component var err error cycles := g.Cycles()