Skip to content

Commit

Permalink
Merge pull request moby#5815 from tonistiigi/fix-json-directive
Browse files Browse the repository at this point in the history
dockerfile: fix parsing syntax directive from JSON
  • Loading branch information
crazy-max authored Mar 6, 2025
2 parents 1c84aa4 + 4e8bc5f commit e62f1ed
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
16 changes: 9 additions & 7 deletions frontend/dockerfile/parser/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,16 @@ func parseDirective(key string, dt []byte, anyFormat bool) (string, string, []Ra
}

// use json directive, and search for { "key": "..." }
jsonDirective := map[string]string{}
jsonDirective := map[string]any{}
if err := json.Unmarshal(dt, &jsonDirective); err == nil {
if v, ok := jsonDirective[key]; ok {
loc := []Range{{
Start: Position{Line: line},
End: Position{Line: line},
}}
return v, v, loc, true
if vAny, ok := jsonDirective[key]; ok {
if v, ok := vAny.(string); ok {
loc := []Range{{
Start: Position{Line: line},
End: Position{Line: line},
}}
return v, v, loc, true
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion frontend/dockerfile/parser/directives_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ RUN ls
require.True(t, ok)
require.Equal(t, "x", ref)

dt = `{"syntax": "foo"}`
dt = `{"syntax": "foo", "bar": ["abc"]}`
ref, _, _, ok = DetectSyntax([]byte(dt))
require.True(t, ok)
require.Equal(t, "foo", ref)
Expand Down

0 comments on commit e62f1ed

Please sign in to comment.