Skip to content

Commit

Permalink
Add fix for cyclical dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasjackson committed Mar 5, 2024
1 parent 6d01460 commit cc6b8c9
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
16 changes: 14 additions & 2 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -887,8 +887,8 @@ func TestParserGeneratesChecksums(t *testing.T) {
require.Equal(t, r3.Metadata().Checksum.Parsed, c3.Metadata().Checksum.Parsed)
}

func TestParserHandlesCyclicalReference(t *testing.T) {
f, pathErr := filepath.Abs("./test_fixtures/cyclical/cyclical.hcl")
func TestParserCyclicalReferenceReturnsError(t *testing.T) {
f, pathErr := filepath.Abs("./test_fixtures/cyclical/fail/cyclical.hcl")
if pathErr != nil {
t.Fatal(pathErr)
}
Expand All @@ -901,6 +901,18 @@ func TestParserHandlesCyclicalReference(t *testing.T) {
require.ErrorContains(t, err, "'resource.container.one' depends on 'resource.network.two'")
}

func TestParserNoCyclicalReferenceReturns(t *testing.T) {
f, pathErr := filepath.Abs("./test_fixtures/cyclical/pass/cyclical.hcl")
if pathErr != nil {
t.Fatal(pathErr)
}

p := setupParser(t)

_, err := p.ParseFile(f)
require.NoError(t, err)
}

func TestParseDirectoryReturnsConfigErrorWhenParseDirectoryFails(t *testing.T) {
f, pathErr := filepath.Abs("./test_fixtures/invalid")
if pathErr != nil {
Expand Down
18 changes: 12 additions & 6 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1166,8 +1166,14 @@ func getDependentResources(b *hclsyntax.Block, ctx *hcl.EvalContext, c *Config,
if err == nil {
// check the deps on the linked resource
for _, cdep := range d.Metadata().Links {
fqdn, err := resources.ParseFQRN(cdep)
fqdn.Attribute = ""

fqrn, err := resources.ParseFQRN(cdep)
fqrn.Attribute = ""

// append the parent module to the link as they are relative
if d.Metadata().Module != "" {
fqrn.Module = d.Metadata().Module
}

if err != nil {
pe := errors.ParserError{}
Expand All @@ -1179,15 +1185,15 @@ func getDependentResources(b *hclsyntax.Block, ctx *hcl.EvalContext, c *Config,
return nil, &pe
}

if me.Metadata().Name == fqdn.Resource &&
me.Metadata().Type == fqdn.Type &&
me.Metadata().Module == fqdn.Module {
if me.Metadata().Name == fqrn.Resource &&
me.Metadata().Type == fqrn.Type &&
me.Metadata().Module == fqrn.Module {

pe := errors.ParserError{}
pe.Column = b.Body.SrcRange.Start.Column
pe.Line = b.Body.SrcRange.Start.Line
pe.Filename = b.Body.SrcRange.Filename
pe.Message = fmt.Sprintf("'%s' depends on '%s' which creates a cyclical dependency, remove the dependency from one of the resources", fqdn.String(), d.Metadata().ID)
pe.Message = fmt.Sprintf("'%s' depends on '%s' which creates a cyclical dependency, remove the dependency from one of the resources", fqrn.String(), d.Metadata().ID)
pe.Level = errors.ParserErrorLevelError

return nil, &pe
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions test_fixtures/cyclical/pass/cyclical.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module "cyclical" {
source = "./module"
}

resource "network" "one" {
subnet = module.cyclical.output.network.subnet
}
7 changes: 7 additions & 0 deletions test_fixtures/cyclical/pass/module/cyclical.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "network" "one" {
subnet = "10.0.0.2/16"
}

output "network" {
value = resource.network.one
}

0 comments on commit cc6b8c9

Please sign in to comment.