Skip to content

Commit

Permalink
Allow skipping parsing of locals
Browse files Browse the repository at this point in the history
Since `locals` can have side effects, we might run into cases when
we don't need to pasre them. One example is `checkVersionConstraints()`,
which is executed early on and relies on parsing Terraform.

Closes: gruntwork-io#1427
  • Loading branch information
amnk committed Feb 18, 2021
1 parent a350500 commit 7ff9e12
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 32 deletions.
1 change: 1 addition & 0 deletions cli/cli_app.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ func checkVersionConstraints(terragruntOptions *options.TerragruntOptions) error
terragruntOptions,
nil,
[]config.PartialDecodeSectionType{config.TerragruntVersionConstraints},
false,
)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func ParseConfigString(configString string, terragruntOptions *options.Terragrun
}

// Decode just the Base blocks. See the function docs for DecodeBaseBlocks for more info on what base blocks are.
localsAsCty, terragruntInclude, includeForDecode, err := DecodeBaseBlocks(terragruntOptions, parser, file, filename, includeFromChild)
localsAsCty, terragruntInclude, includeForDecode, err := DecodeBaseBlocks(terragruntOptions, parser, file, filename, includeFromChild, true)
if err != nil {
return nil, err
}
Expand Down
27 changes: 18 additions & 9 deletions config/config_partial.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func DecodeBaseBlocks(
hclFile *hcl.File,
filename string,
includeFromChild *IncludeConfig,
parseLocals bool,
) (*cty.Value, *terragruntInclude, *IncludeConfig, error) {
// Decode just the `include` block, and verify that it's allowed here
terragruntInclude, err := decodeAsTerragruntInclude(
Expand All @@ -116,13 +117,18 @@ func DecodeBaseBlocks(

// Evaluate all the expressions in the locals block separately and generate the variables list to use in the
// evaluation context.
locals, err := evaluateLocalsBlock(terragruntOptions, parser, hclFile, filename, includeForDecode)
if err != nil {
return nil, nil, nil, err
}
localsAsCty, err := convertValuesMapToCtyVal(locals)
if err != nil {
return nil, nil, nil, err
var localsAsCty cty.Value
if parseLocals {
locals, err := evaluateLocalsBlock(terragruntOptions, parser, hclFile, filename, includeForDecode)
if err != nil {
return nil, nil, nil, err
}
localsAsCty, err = convertValuesMapToCtyVal(locals)
if err != nil {
return nil, nil, nil, err
}
} else {
localsAsCty = cty.Value{}
}

return &localsAsCty, terragruntInclude, includeForDecode, nil
Expand All @@ -133,13 +139,14 @@ func PartialParseConfigFile(
terragruntOptions *options.TerragruntOptions,
include *IncludeConfig,
decodeList []PartialDecodeSectionType,
parseLocals bool,
) (*TerragruntConfig, error) {
configString, err := util.ReadFileAsString(filename)
if err != nil {
return nil, err
}

config, err := PartialParseConfigString(configString, terragruntOptions, include, filename, decodeList)
config, err := PartialParseConfigString(configString, terragruntOptions, include, filename, decodeList, parseLocals)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -168,6 +175,7 @@ func PartialParseConfigString(
includeFromChild *IncludeConfig,
filename string,
decodeList []PartialDecodeSectionType,
parseLocals bool,
) (*TerragruntConfig, error) {
// Parse the HCL string into an AST body that can be decoded multiple times later without having to re-parse
parser := hclparse.NewParser()
Expand All @@ -177,7 +185,7 @@ func PartialParseConfigString(
}

// Decode just the Base blocks. See the function docs for DecodeBaseBlocks for more info on what base blocks are.
localsAsCty, terragruntInclude, includeForDecode, err := DecodeBaseBlocks(terragruntOptions, parser, file, filename, includeFromChild)
localsAsCty, terragruntInclude, includeForDecode, err := DecodeBaseBlocks(terragruntOptions, parser, file, filename, includeFromChild, parseLocals)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -321,6 +329,7 @@ func partialParseIncludedConfig(includedConfig *IncludeConfig, terragruntOptions
terragruntOptions,
includedConfig,
decodeList,
true,
)
}

Expand Down
32 changes: 16 additions & 16 deletions config/config_partial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies {
}
`

terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependenciesBlock})
terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependenciesBlock}, true)
require.NoError(t, err)
assert.True(t, terragruntConfig.IsPartial)

Expand Down Expand Up @@ -48,10 +48,10 @@ dependencies {
prevent_destroy = false
`

_, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{TerragruntFlags})
_, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{TerragruntFlags}, true)
assert.NoError(t, err)

_, err = PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependenciesBlock})
_, err = PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependenciesBlock}, true)
assert.Error(t, err)
}

Expand All @@ -67,7 +67,7 @@ prevent_destroy = true
skip = true
`

terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependenciesBlock, TerragruntFlags})
terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependenciesBlock, TerragruntFlags}, true)
require.NoError(t, err)
assert.True(t, terragruntConfig.IsPartial)

Expand All @@ -87,7 +87,7 @@ skip = true
func TestPartialParseOmittedItems(t *testing.T) {
t.Parallel()

terragruntConfig, err := PartialParseConfigString("", mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependenciesBlock, TerragruntFlags})
terragruntConfig, err := PartialParseConfigString("", mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependenciesBlock, TerragruntFlags}, true)
require.NoError(t, err)
assert.True(t, terragruntConfig.IsPartial)
assert.Nil(t, terragruntConfig.Dependencies)
Expand All @@ -103,18 +103,18 @@ func TestPartialParseDoesNotResolveIgnoredBlockEvenInParent(t *testing.T) {
t.Parallel()

opts := mockOptionsForTestWithConfigPath(t, "../test/fixture-partial-parse/ignore-bad-block-in-parent/child/"+DefaultTerragruntConfigPath)
_, err := PartialParseConfigFile(opts.TerragruntConfigPath, opts, nil, []PartialDecodeSectionType{TerragruntFlags})
_, err := PartialParseConfigFile(opts.TerragruntConfigPath, opts, nil, []PartialDecodeSectionType{TerragruntFlags}, true)
assert.NoError(t, err)

_, err = PartialParseConfigFile(opts.TerragruntConfigPath, opts, nil, []PartialDecodeSectionType{DependenciesBlock})
_, err = PartialParseConfigFile(opts.TerragruntConfigPath, opts, nil, []PartialDecodeSectionType{DependenciesBlock}, true)
assert.Error(t, err)
}

func TestPartialParseOnlyInheritsSelectedBlocksFlags(t *testing.T) {
t.Parallel()

opts := mockOptionsForTestWithConfigPath(t, "../test/fixture-partial-parse/partial-inheritance/child/"+DefaultTerragruntConfigPath)
terragruntConfig, err := PartialParseConfigFile(opts.TerragruntConfigPath, opts, nil, []PartialDecodeSectionType{TerragruntFlags})
terragruntConfig, err := PartialParseConfigFile(opts.TerragruntConfigPath, opts, nil, []PartialDecodeSectionType{TerragruntFlags}, true)
require.NoError(t, err)
assert.True(t, terragruntConfig.IsPartial)
assert.Nil(t, terragruntConfig.Dependencies)
Expand All @@ -130,7 +130,7 @@ func TestPartialParseOnlyInheritsSelectedBlocksDependencies(t *testing.T) {
t.Parallel()

opts := mockOptionsForTestWithConfigPath(t, "../test/fixture-partial-parse/partial-inheritance/child/"+DefaultTerragruntConfigPath)
terragruntConfig, err := PartialParseConfigFile(opts.TerragruntConfigPath, opts, nil, []PartialDecodeSectionType{DependenciesBlock})
terragruntConfig, err := PartialParseConfigFile(opts.TerragruntConfigPath, opts, nil, []PartialDecodeSectionType{DependenciesBlock}, true)
require.NoError(t, err)
assert.True(t, terragruntConfig.IsPartial)

Expand All @@ -155,7 +155,7 @@ dependency "vpc" {
}
`

terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependencyBlock})
terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependencyBlock}, true)
require.NoError(t, err)
assert.True(t, terragruntConfig.IsPartial)

Expand All @@ -178,7 +178,7 @@ dependency "sql" {
}
`

terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependencyBlock})
terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependencyBlock}, true)
require.NoError(t, err)
assert.True(t, terragruntConfig.IsPartial)

Expand All @@ -203,7 +203,7 @@ dependency "sql" {
}
`

terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependencyBlock})
terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependencyBlock}, true)
require.NoError(t, err)
assert.True(t, terragruntConfig.IsPartial)

Expand All @@ -229,7 +229,7 @@ dependency "sql" {
}
`

terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependenciesBlock, DependencyBlock})
terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependenciesBlock, DependencyBlock}, true)
require.NoError(t, err)
assert.True(t, terragruntConfig.IsPartial)

Expand All @@ -255,7 +255,7 @@ dependency "sql" {
}
`

terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependencyBlock, DependenciesBlock})
terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependencyBlock, DependenciesBlock}, true)
require.NoError(t, err)
assert.True(t, terragruntConfig.IsPartial)

Expand All @@ -281,7 +281,7 @@ dependency "sql" {
}
`

terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependencyBlock, DependenciesBlock})
terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{DependencyBlock, DependenciesBlock}, true)
require.NoError(t, err)
assert.True(t, terragruntConfig.IsPartial)

Expand All @@ -307,7 +307,7 @@ terraform {
}
`

terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{TerraformSource})
terragruntConfig, err := PartialParseConfigString(config, mockOptionsForTest(t), nil, DefaultTerragruntConfigPath, []PartialDecodeSectionType{TerraformSource}, true)
require.NoError(t, err)
assert.True(t, terragruntConfig.IsPartial)

Expand Down
13 changes: 7 additions & 6 deletions config/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func getDependencyBlockConfigPathsByFilepath(configPath string, terragruntOption
// TerragruntConfig.Dependencies. Note that since we aren't passing in `DependenciesBlock` to the
// PartialDecodeSectionType list, the Dependencies attribute will not include any dependencies specified via the
// dependencies block.
tgConfig, err := PartialParseConfigFile(configPath, terragruntOptions, nil, []PartialDecodeSectionType{DependencyBlock})
tgConfig, err := PartialParseConfigFile(configPath, terragruntOptions, nil, []PartialDecodeSectionType{DependencyBlock}, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -368,6 +368,7 @@ func cloneTerragruntOptionsForDependencyOutput(terragruntOptions *options.Terrag
targetOptions,
nil,
[]PartialDecodeSectionType{TerraformBlock},
true,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -402,10 +403,10 @@ func getTerragruntOutputJson(terragruntOptions *options.TerragruntOptions, targe
return nil, err
}

// First attempt to parse the `remote_state` blocks without parsing/getting dependency outputs. If this is possible,
// proceed to routine that fetches remote state directly. Otherwise, fallback to calling `terragrunt output`
// directly.
remoteStateTGConfig, err := PartialParseConfigFile(targetConfig, targetTGOptions, nil, []PartialDecodeSectionType{RemoteStateBlock, TerragruntFlags})
// First, attempt to parse the `remote_state` blocks without parsing/getting dependency outputs. If this is
// possible, proceed to routine that fetches remote state directly. Otherwise, fallback to calling
// `terragrunt output` directly.
remoteStateTGConfig, err := PartialParseConfigFile(targetConfig, targetTGOptions, nil, []PartialDecodeSectionType{RemoteStateBlock, TerragruntFlags}, true)
if err != nil || !canGetRemoteState(remoteStateTGConfig.RemoteState) {
terragruntOptions.Logger.Warningf("Could not parse remote_state block from target config %s", targetConfig)
terragruntOptions.Logger.Warningf("Falling back to terragrunt output.")
Expand Down Expand Up @@ -435,7 +436,7 @@ func canGetRemoteState(remoteState *remote.RemoteState) bool {
func terragruntAlreadyInit(terragruntOptions *options.TerragruntOptions, configPath string) (bool, string, error) {
// We need to first determine the working directory where the terraform source should be located. This is dependent
// on the source field of the terraform block in the config.
terraformBlockTGConfig, err := PartialParseConfigFile(configPath, terragruntOptions, nil, []PartialDecodeSectionType{TerraformSource})
terraformBlockTGConfig, err := PartialParseConfigFile(configPath, terragruntOptions, nil, []PartialDecodeSectionType{TerraformSource}, true)
if err != nil {
return false, "", err
}
Expand Down
1 change: 1 addition & 0 deletions configstack/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ func resolveTerraformModule(terragruntConfigPath string, terragruntOptions *opti
config.DependenciesBlock,
config.DependencyBlock,
},
true,
)
if err != nil {
return nil, errors.WithStackTrace(ErrorProcessingModule{UnderlyingError: err, HowThisModuleWasFound: howThisModuleWasFound, ModulePath: terragruntConfigPath})
Expand Down

0 comments on commit 7ff9e12

Please sign in to comment.