Skip to content

Factories fail when empty YaML files are passed #1794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
juliocc opened this issue Oct 23, 2023 · 21 comments
Closed

Factories fail when empty YaML files are passed #1794

juliocc opened this issue Oct 23, 2023 · 21 comments
Assignees

Comments

@juliocc
Copy link
Collaborator

juliocc commented Oct 23, 2023

Factories fail when empty YaML files are passed.
Some factories mitigate the problem by using try(yamldecode()), which however masks potential parse errors.

original message below:

getting the following error when applying 02-networking stage with empty YAML files for firewall rules
example files:
2-networking-b-vpn/data/hierarchical-ingress-rules.yaml
2-networking-b-vpn/data/firewall-rules/dev/rules.yaml

cc @aaarel

@sruffilli sruffilli self-assigned this Oct 25, 2023
@sruffilli
Copy link
Collaborator

WIP tests log:

Works if empty
2-networking-b-vpn/data/hierarchical-ingress-rules.yaml

Fails if empty
2-networking-b-vpn/data/firewall-rules/dev/rules.yaml

@ludoo
Copy link
Collaborator

ludoo commented Oct 25, 2023

Yep, problem is yamldecode not liking empty/comment only YAML files. We had try around the yamldecode in some places and not others, then Terraform was supposedly fixed and we removed it, and then it was added back in some places.

Discussed it the other day with Julio, and as long as we're consistent and users know what to expect, either solution works each with its own drawbacks

  • try makes comment-only files work but hides errors
  • no try exposes errors but then it should be a surprise that a comment-only file breaks

@sruffilli
Copy link
Collaborator

Came here with the same comments.

I'd be tempted to remove try everywhere, at which point users have two options:

  • Create a valid YaML empty map {} (which can be a comment documenting the behavior at the top of each sample file)
  • Remove the factory configuration altogether.

Explicit is better than implicit

Wdyt?

@ludoo
Copy link
Collaborator

ludoo commented Oct 25, 2023

Agree, I would also drop a comment in our example files, stating that the file should either be valid or be removed altogether. So users know what to expect.

@juliocc
Copy link
Collaborator Author

juliocc commented Oct 25, 2023

After a couple of tests, a empty file can be "fixed" with ---

> yamldecode("")
╷
│ Error: Error in function call
│
│   on <console-input> line 1:
│   (source code not available)
│
│ Call to function "yamldecode" failed: on line 1, column 1: missing start of document.
╵


> yamldecode("#")
╷
│ Error: Error in function call
│
│   on <console-input> line 1:
│   (source code not available)
│
│ Call to function "yamldecode" failed: on line 1, column 1: missing start of document.
╵


> yamldecode("---")
null
> yamldecode("#\n---")
null

@ludoo
Copy link
Collaborator

ludoo commented Oct 25, 2023

That's genius. So we just append \n--- to any file before passing to yamldecode?

@ludoo
Copy link
Collaborator

ludoo commented Oct 25, 2023

nope, does not work

> yamldecode("foo: {}\n---")
╷
│ Error: Error in function call
│ 
│   on <console-input> line 1:
│   (source code not available)
│ 
│ Call to function "yamldecode" failed: on line 1, column 1: unexpected extra content after value.
╵


> yamldecode("foo: {}\n")
{
  "foo" = {}
}

@sruffilli
Copy link
Collaborator

you got it reversed - should be

> yamldecode("---\n{}")
{}

@ludoo
Copy link
Collaborator

ludoo commented Oct 25, 2023

> yamldecode("--\nfoo: {}\n")
╷
│ Error: Error in function call
│ 
│   on <console-input> line 1:
│   (source code not available)
│ 
│ Call to function "yamldecode" failed: on line 2, column 1: mapping values are not allowed in this context.

@juliocc
Copy link
Collaborator Author

juliocc commented Oct 25, 2023

That's genius. So we just append \n--- to any file before passing to yamldecode?

I was thinking we can add that to any empty file

# boilerplate
---

@ludoo
Copy link
Collaborator

ludoo commented Oct 25, 2023

I would go with removing try and adding comments to warn users. The --- trick seems fragile, and more magical/implicit than having a try IMHO.

@ludoo
Copy link
Collaborator

ludoo commented Oct 25, 2023

That's genius. So we just append \n--- to any file before passing to yamldecode?

I was thinking we can add that to any empty file

# boilerplate
---

harder to understand than an explicit comment saying something like

# Terraform will be unable to decode this file if it does not contain valid YAML (empty, or comments only)

@wiktorn
Copy link
Collaborator

wiktorn commented Oct 25, 2023

> yamldecode("---\nfoo: {}")
{
  "foo" = {}
}

You missed on - @ludoo

+1 from me to:

I'd be tempted to remove try everywhere, at which point users have two options:

  • Create a valid YaML empty map {} (which can be a comment documenting the behavior at the top of each sample file)
  • Remove the factory configuration altogether.

Explicit is better than implicit

Wdyt?

@juliocc
Copy link
Collaborator Author

juliocc commented Oct 25, 2023

It's part of the yaml spec, so not really a trick.

But agreed, it's more obscure than just a comment

@ludoo
Copy link
Collaborator

ludoo commented Oct 25, 2023

It's part of the yaml spec, so not really a trick.

But agreed, it's more obscure than just a comment

Absolutely, the whole issue is yamldecode barfing, which was supposed to be fixed as you noted the other day.

@ludoo
Copy link
Collaborator

ludoo commented Oct 25, 2023

You missed on - @ludoo

🤦

@sruffilli
Copy link
Collaborator

That's genius. So we just append \n--- to any file before passing to yamldecode?

I was thinking we can add that to any empty file

# boilerplate
---

harder to understand than an explicit comment saying something like

# Terraform will be unable to decode this file if it does not contain valid YAML (empty, or comments only)

IMHO we should do both.
--- signals the beginning of a YaML document, so it's fair game to bake it into our files - and we could add the comment @ludoo wrote following/preceding it.

@ludoo
Copy link
Collaborator

ludoo commented Oct 25, 2023

+1 Simo

@ludoo
Copy link
Collaborator

ludoo commented Oct 25, 2023

And we do this for all modules exposing a factory, and add the comment to YAMLs in the doc examples too, right? Otherwise we'll have this same discussion all over again in a few months...

@sruffilli
Copy link
Collaborator

SG. Editing the original post to extend scope, and will TAL later today.

@sruffilli sruffilli changed the title error when applying 02-networking stage with an empty YAML Factories fail when empty YaML files are passed Oct 25, 2023
@sruffilli
Copy link
Collaborator

Fixed by #1812

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants