Skip to content

Commit

Permalink
hcl2template: rework parsing logic
Browse files Browse the repository at this point in the history
In previous versions of Packer, the parser would load a HCL file (in
either HCL or JSON format), and process it in phases.
This process meant that if the file was not valid in the first place,
we'd have some duplicate errors first.

Another thing is that sources and build blocks were not being parsed at
the beginning of the parsing phase at all, but were discovered later in
the process, when the data sources have been executed and variables have
been evaluated.

This commit changes the parsing to happen all at once, so we parse the
files with the parser, which will fail should one file be malformatted,
then we visit the body once using the top-level schema for a Packer
build template, handling misplaced blocks once in the process.

Then, since the builds and sources may contain some dynamic blocks that
need to be expanded once their context is ready (i.e. the
variables/datasources they depend on), we can expand the build block for
them, and populate their respective objects.

This last step should also be adapted for datasources later, as they may
need to support dynamic blocks, and be expanded lazily.
  • Loading branch information
lbajolet-hashicorp committed Sep 19, 2023
1 parent 8922f5d commit 8759aba
Show file tree
Hide file tree
Showing 19 changed files with 438 additions and 311 deletions.
3 changes: 3 additions & 0 deletions hcl2template/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/go-version"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/hashicorp/hcl/v2/hclsyntax"
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
"github.com/hashicorp/packer-plugin-sdk/template/config"
"github.com/hashicorp/packer/builder/null"
Expand Down Expand Up @@ -348,6 +349,7 @@ var cmpOpts = []cmp.Option{
cmpopts.IgnoreUnexported(
PackerConfig{},
Variable{},
BuildBlock{},
SourceBlock{},
DatasourceBlock{},
ProvisionerBlock{},
Expand Down Expand Up @@ -376,6 +378,7 @@ var cmpOpts = []cmp.Option{
cmpopts.IgnoreFields(packer.CoreBuildPostProcessor{},
"HCLConfig",
),
cmpopts.IgnoreTypes(hclsyntax.Body{}),
cmpopts.IgnoreTypes(hcl2template.MockBuilder{}),
cmpopts.IgnoreTypes(HCL2Ref{}),
cmpopts.IgnoreTypes([]*LocalBlock{}),
Expand Down
Loading

0 comments on commit 8759aba

Please sign in to comment.