Skip to content

bake: keep escaped interpolation in print output - #3097

Merged
tonistiigi merged 1 commit into
docker:masterfrom
crazy-max:bake-esc-interpolation
Apr 8, 2025
Merged

tonistiigi merged 1 commit into
docker:masterfrom
crazy-max:bake-esc-interpolation

Conversation

@crazy-max

Copy link
Copy Markdown
Member

fixes #2613

The interpolation and directive introductions are escaped by doubling their leading characters. The ${ sequence is escaped as $${ and the %{ sequence is escaped as %%{.

When the definition is parsed, the escape sequence is removed but we need to keep it as we expect a valid input when printing the definition with --print.

Comment thread bake/bake.go
Comment on lines +737 to +770
func (t *Target) MarshalJSON() ([]byte, error) {
tgt := *t
esc := func(s string) string {
return strings.ReplaceAll(strings.ReplaceAll(s, "${", "$${"), "%{", "%%{")
}

tgt.Annotations = slices.Clone(t.Annotations)
for i, v := range tgt.Annotations {
tgt.Annotations[i] = esc(v)
}

if tgt.DockerfileInline != nil {
escaped := esc(*tgt.DockerfileInline)
tgt.DockerfileInline = &escaped
}

tgt.Labels = make(map[string]*string, len(t.Labels))
for k, v := range t.Labels {
if v != nil {
escaped := esc(*v)
tgt.Labels[k] = &escaped
}
}

tgt.Args = make(map[string]*string, len(t.Args))
for k, v := range t.Args {
if v != nil {
escaped := esc(*v)
tgt.Args[k] = &escaped
}
}

return json.Marshal(tgt)
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like it but didn't find a way to achieve that during context evaluation with hcl.

@crazy-max
crazy-max force-pushed the bake-esc-interpolation branch from 900f2a2 to 19bd077 Compare April 8, 2025 15:22
@crazy-max
crazy-max marked this pull request as ready for review April 8, 2025 15:22
Comment thread bake/bake.go Resolved
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
@crazy-max
crazy-max force-pushed the bake-esc-interpolation branch from 19bd077 to a91db7c Compare April 8, 2025 15:30
@tonistiigi
tonistiigi merged commit 1f44971 into docker:master Apr 8, 2025
@crazy-max
crazy-max deleted the bake-esc-interpolation branch April 8, 2025 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

docker-bake.json applying HCL string templates

2 participants