Skip to content

bake: keep escaped interpolation in print output #3097

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

Merged
merged 1 commit into from
Apr 8, 2025

Conversation

crazy-max
Copy link
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.

bake/bake.go Outdated
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
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
@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
140 checks passed
@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