Skip to content

Commit 5f49924

Browse files
committed
add normailization to args and fix parsing
1 parent ef922da commit 5f49924

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

pkg/build/buildopts.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/containerd/platforms"
2727
"github.com/moby/buildkit/frontend/dockerfile/instructions"
2828
"github.com/moby/buildkit/frontend/dockerfile/parser"
29+
"github.com/moby/buildkit/frontend/dockerfile/shell"
2930
"github.com/moby/buildkit/util/progress/progresswriter"
3031
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
3132

@@ -160,6 +161,16 @@ func NewBuildOpts(ctx context.Context, basePath string, contextMap map[string][]
160161
bps = append(bps, platforms.DefaultSpec())
161162
}
162163

164+
normalizeBuildArg := func(arg string) string {
165+
arg = strings.TrimSpace(arg)
166+
if len(arg) >= 2 {
167+
if (arg[0] == '"' && arg[len(arg)-1] == '"') || (arg[0] == '\'' && arg[len(arg)-1] == '\'') {
168+
arg = arg[1 : len(arg)-1]
169+
}
170+
}
171+
return arg
172+
}
173+
163174
pls, err := func() ([]ocispecs.Platform, error) {
164175
pls := []ocispecs.Platform{}
165176
values, ok := contextMap[KeyPlatforms]
@@ -251,9 +262,16 @@ func NewBuildOpts(ctx context.Context, basePath string, contextMap map[string][]
251262
for _, metaArg := range metaArgs {
252263
for _, arg := range metaArg.Args {
253264
// Only use the dockerfile meta arg if the user did not overwrite it
254-
if _, ok := buildArgs[arg.Key]; !ok {
255-
buildArgs[arg.Key] = arg.ValueString()
265+
if _, ok := buildArgs[arg.Key]; ok {
266+
continue
267+
}
268+
// Expand with prior args and strip shell quotes
269+
resolved, err := shell.NewLex('\\').ProcessWordWithMatches(arg.ValueString(), utils.NewMapGetter(buildArgs))
270+
if err != nil {
271+
return nil, err
256272
}
273+
// Save the resolved value for later use
274+
buildArgs[arg.Key] = normalizeBuildArg(resolved.Result)
257275
}
258276
}
259277

0 commit comments

Comments
 (0)