Skip to content

Commit

Permalink
dockerfile: fix parsing syntax directive from JSON
Browse files Browse the repository at this point in the history
Due to regression, parsing would fail if JSON had
other datatypes than strings.

Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Mar 5, 2025
1 parent 21a6d8b commit 4e8bc5f
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 4e8bc5f

Please sign in to comment.