compose-spec / compose-go

Reference library for parsing and loading Compose YAML files
https://compose-spec.io
Apache License 2.0
355 stars 112 forks source link

templates cannot have default values containing new lines #653

Open panzi opened 3 months ago

panzi commented 3 months ago

The default value of a variable cannot contain a new line (\n) character because .* doesn't match those. I.e. if you have this in your .env file:

FOO="${BAR:-multi
line}"

You get this error mesage:

2024/07/02 02:49:54 Invalid template: "${BAR:-multi\nline}"

This is a bit confusing, because this doesn't look like an invalid template. Because of the quotes the whole string is correctly parsed, the template has a valid name and does end with }. To fix this you need to change this regular expression:

https://github.com/compose-spec/compose-go/blob/9d0d133e13d0955e27520c6317d08822b7c5de5f/template/template.go#L31

To this:

var substitutionBraced = "[_a-z][_a-z0-9]*(?::?[-+?]((?:.|\n)*))?"

(I don't know if there is another way to match anything, including a \n, in Go.)